blob: 7a0cbda1f244911fd7418596dd10d08634b15328 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/OperatorKinds.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000023#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000025using namespace clang;
26
27/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000028/// may either be a top level namespace or a block-level namespace alias. If
29/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000030///
31/// namespace-definition: [C++ 7.3: basic.namespace]
32/// named-namespace-definition
33/// unnamed-namespace-definition
34///
35/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000036/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000037///
38/// named-namespace-definition:
39/// original-namespace-definition
40/// extension-namespace-definition
41///
42/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000043/// 'inline'[opt] 'namespace' identifier attributes[opt]
44/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000045///
46/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' original-namespace-name
48/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000049///
Chris Lattner8f08cb72007-08-25 06:57:03 +000050/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
51/// 'namespace' identifier '=' qualified-namespace-specifier ';'
52///
John McCalld226f652010-08-21 09:40:31 +000053Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000054 SourceLocation &DeclEnd,
55 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000056 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000057 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000058 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000059
Douglas Gregor49f40bd2009-09-18 19:03:04 +000060 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000061 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000062 cutOffParsing();
63 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000064 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 SourceLocation IdentLoc;
67 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000068 std::vector<SourceLocation> ExtraIdentLoc;
69 std::vector<IdentifierInfo*> ExtraIdent;
70 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000071
72 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner04d66662007-10-09 17:33:22 +000074 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000075 Ident = Tok.getIdentifierInfo();
76 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000077 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
78 ExtraNamespaceLoc.push_back(ConsumeToken());
79 ExtraIdent.push_back(Tok.getIdentifierInfo());
80 ExtraIdentLoc.push_back(ConsumeToken());
81 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000082 }
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner8f08cb72007-08-25 06:57:03 +000084 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000085 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086 if (Tok.is(tok::kw___attribute)) {
87 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000088 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000089 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Douglas Gregor6a588dd2009-06-17 19:49:00 +000091 if (Tok.is(tok::equal)) {
Nico Webere1bb3292012-10-27 23:44:27 +000092 if (Ident == 0) {
93 Diag(Tok, diag::err_expected_ident);
94 // Skip to end of the definition and eat the ';'.
95 SkipUntil(tok::semi);
96 return 0;
97 }
John McCall7f040a92010-12-24 02:08:15 +000098 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000099 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +0000100 if (InlineLoc.isValid())
101 Diag(InlineLoc, diag::err_inline_namespace_alias)
102 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000103 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Richard Trieuf858bd82011-05-26 20:11:09 +0000106
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000107 BalancedDelimiterTracker T(*this, tok::l_brace);
108 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000109 if (!ExtraIdent.empty()) {
110 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
111 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000114 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000115 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor23c94db2010-07-02 17:43:08 +0000118 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
119 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
120 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000121 if (!ExtraIdent.empty()) {
122 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
123 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
124 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000125 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000126 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000127 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000128 }
129
Richard Trieuf858bd82011-05-26 20:11:09 +0000130 if (!ExtraIdent.empty()) {
131 TentativeParsingAction TPA(*this);
132 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
133 Token rBraceToken = Tok;
134 TPA.Revert();
135
136 if (!rBraceToken.is(tok::r_brace)) {
137 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
138 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
139 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000140 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000141 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
142 E = ExtraIdent.end(); I != E; ++I) {
143 NamespaceFix += " { namespace ";
144 NamespaceFix += (*I)->getName();
145 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000146
Richard Trieuf858bd82011-05-26 20:11:09 +0000147 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000148 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000149 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000150
Richard Trieuf858bd82011-05-26 20:11:09 +0000151 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
152 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
153 ExtraIdentLoc.back()),
154 NamespaceFix)
155 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
156 }
157 }
158
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000159 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000160 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000161 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000162 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000163
Chris Lattner51448322009-03-29 14:02:43 +0000164 // Enter a scope for the namespace.
165 ParseScope NamespaceScope(this, Scope::DeclScope);
166
John McCalld226f652010-08-21 09:40:31 +0000167 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000168 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000169 IdentLoc, Ident, T.getOpenLocation(),
170 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000171
John McCallf312b1e2010-08-26 23:41:50 +0000172 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
173 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Richard Trieuf858bd82011-05-26 20:11:09 +0000175 // Parse the contents of the namespace. This includes parsing recovery on
176 // any improperly nested namespaces.
177 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000178 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner51448322009-03-29 14:02:43 +0000180 // Leave the namespace scope.
181 NamespaceScope.Exit();
182
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000183 DeclEnd = T.getCloseLocation();
184 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000185
186 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000187}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000188
Richard Trieuf858bd82011-05-26 20:11:09 +0000189/// ParseInnerNamespace - Parse the contents of a namespace.
190void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
191 std::vector<IdentifierInfo*>& Ident,
192 std::vector<SourceLocation>& NamespaceLoc,
193 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000194 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000195 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000196 if (index == Ident.size()) {
197 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
198 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000199 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000200 MaybeParseMicrosoftAttributes(attrs);
201 ParseExternalDeclaration(attrs);
202 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000203
204 // The caller is what called check -- we are simply calling
205 // the close for it.
206 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000207
208 return;
209 }
210
211 // Parse improperly nested namespaces.
212 ParseScope NamespaceScope(this, Scope::DeclScope);
213 Decl *NamespcDecl =
214 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
215 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000216 Ident[index], Tracker.getOpenLocation(),
217 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000218
219 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000220 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000221
222 NamespaceScope.Exit();
223
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000224 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000225}
226
Anders Carlssonf67606a2009-03-28 04:07:16 +0000227/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
228/// alias definition.
229///
John McCalld226f652010-08-21 09:40:31 +0000230Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000231 SourceLocation AliasLoc,
232 IdentifierInfo *Alias,
233 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000234 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Anders Carlssonf67606a2009-03-28 04:07:16 +0000236 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000238 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000239 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000240 cutOffParsing();
241 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000243
Anders Carlssonf67606a2009-03-28 04:07:16 +0000244 CXXScopeSpec SS;
245 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000246 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000247
248 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
249 Diag(Tok, diag::err_expected_namespace_name);
250 // Skip to end of the definition and eat the ';'.
251 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000252 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000253 }
254
255 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000256 IdentifierInfo *Ident = Tok.getIdentifierInfo();
257 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Anders Carlssonf67606a2009-03-28 04:07:16 +0000259 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000260 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000261 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
262 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000265 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000266}
267
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000268/// ParseLinkage - We know that the current token is a string_literal
269/// and just before that, that extern was seen.
270///
271/// linkage-specification: [C++ 7.5p2: dcl.link]
272/// 'extern' string-literal '{' declaration-seq[opt] '}'
273/// 'extern' string-literal declaration
274///
Chris Lattner7d642712010-11-09 20:15:55 +0000275Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000276 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000277 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000278 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000280 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000281 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000282
Richard Smith99831e42012-03-06 03:21:47 +0000283 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
284 // phase 7, so string-literal concatenation is supposed to occur.
285 // extern "" "C" "" "+" "+" { } is legal.
286 if (Tok.hasUDSuffix())
287 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000288 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000289
Douglas Gregor074149e2009-01-05 19:45:36 +0000290 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000291 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000292 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000293 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000294 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000295 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000296 : SourceLocation());
297
John McCall0b7e6782011-03-24 11:26:52 +0000298 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000299 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000300 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000301
Douglas Gregor074149e2009-01-05 19:45:36 +0000302 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000303 // Reset the source range in DS, as the leading "extern"
304 // does not really belong to the inner declaration ...
305 DS.SetRangeStart(SourceLocation());
306 DS.SetRangeEnd(SourceLocation());
307 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000308 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000309 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000310 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000311 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000312 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000313
Douglas Gregor63a01132010-02-07 08:38:28 +0000314 DS.abort();
315
John McCall7f040a92010-12-24 02:08:15 +0000316 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000317
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000318 BalancedDelimiterTracker T(*this, tok::l_brace);
319 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000320 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000321 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000322 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000323 MaybeParseMicrosoftAttributes(attrs);
324 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000325 }
326
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000327 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000328 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000329 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000330}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000331
Douglas Gregorf780abc2008-12-30 03:27:21 +0000332/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
333/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000334Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000335 const ParsedTemplateInfo &TemplateInfo,
336 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000337 ParsedAttributesWithRange &attrs,
338 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000339 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000340 ObjCDeclContextSwitch ObjCDC(*this);
341
Douglas Gregorf780abc2008-12-30 03:27:21 +0000342 // Eat 'using'.
343 SourceLocation UsingLoc = ConsumeToken();
344
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000345 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000346 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000347 cutOffParsing();
348 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000349 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000350
John McCall78b81052010-11-10 02:40:36 +0000351 // 'using namespace' means this is a using-directive.
352 if (Tok.is(tok::kw_namespace)) {
353 // Template parameters are always an error here.
354 if (TemplateInfo.Kind) {
355 SourceRange R = TemplateInfo.getSourceRange();
356 Diag(UsingLoc, diag::err_templated_using_directive)
357 << R << FixItHint::CreateRemoval(R);
358 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000359
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000360 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000361 }
362
Richard Smith162e1c12011-04-15 14:24:37 +0000363 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000364
365 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000366 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000367
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000368 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000369 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000370}
371
372/// ParseUsingDirective - Parse C++ using-directive, assumes
373/// that current token is 'namespace' and 'using' was already parsed.
374///
375/// using-directive: [C++ 7.3.p4: namespace.udir]
376/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
377/// namespace-name ;
378/// [GNU] using-directive:
379/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
380/// namespace-name attributes[opt] ;
381///
John McCalld226f652010-08-21 09:40:31 +0000382Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000383 SourceLocation UsingLoc,
384 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000385 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000386 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
387
388 // Eat 'namespace'.
389 SourceLocation NamespcLoc = ConsumeToken();
390
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000391 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000392 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000393 cutOffParsing();
394 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000395 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000396
Douglas Gregorf780abc2008-12-30 03:27:21 +0000397 CXXScopeSpec SS;
398 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000399 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000400
Douglas Gregorf780abc2008-12-30 03:27:21 +0000401 IdentifierInfo *NamespcName = 0;
402 SourceLocation IdentLoc = SourceLocation();
403
404 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000405 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000406 Diag(Tok, diag::err_expected_namespace_name);
407 // If there was invalid namespace name, skip to end of decl, and eat ';'.
408 SkipUntil(tok::semi);
409 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000410 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattner823c44e2009-01-06 07:27:21 +0000413 // Parse identifier.
414 NamespcName = Tok.getIdentifierInfo();
415 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner823c44e2009-01-06 07:27:21 +0000417 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000418 bool GNUAttr = false;
419 if (Tok.is(tok::kw___attribute)) {
420 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000421 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner823c44e2009-01-06 07:27:21 +0000424 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000425 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000426 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000427 GNUAttr ? diag::err_expected_semi_after_attribute_list
428 : diag::err_expected_semi_after_namespace_name,
429 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000430
Douglas Gregor23c94db2010-07-02 17:43:08 +0000431 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000432 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000433}
434
Richard Smith162e1c12011-04-15 14:24:37 +0000435/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
436/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000437///
438/// using-declaration: [C++ 7.3.p3: namespace.udecl]
439/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000440/// unqualified-id
441/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000442///
Richard Smithd03de6a2013-01-29 10:02:16 +0000443/// alias-declaration: C++11 [dcl.dcl]p1
444/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smith162e1c12011-04-15 14:24:37 +0000445///
John McCalld226f652010-08-21 09:40:31 +0000446Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000447 const ParsedTemplateInfo &TemplateInfo,
448 SourceLocation UsingLoc,
449 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000450 AccessSpecifier AS,
451 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000452 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000453 SourceLocation TypenameLoc;
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000454 bool HasTypenameKeyword = false;
Richard Smith6b3d3e52013-02-20 19:22:51 +0000455 ParsedAttributesWithRange Attrs(AttrFactory);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000456
457 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000458 MaybeParseCXX11Attributes(Attrs);
459 ProhibitAttributes(Attrs);
460 Attrs.clear();
461 Attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000462
463 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000464 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000465 if (Tok.is(tok::kw_typename)) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000466 TypenameLoc = ConsumeToken();
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000467 HasTypenameKeyword = true;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000468 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000469
470 // Parse nested-name-specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000471 IdentifierInfo *LastII = 0;
472 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
473 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
474 /*LastII=*/&LastII);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000475
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000476 // Check nested-name specifier.
477 if (SS.isInvalid()) {
478 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000479 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000480 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000481
Richard Smith2db075b2013-03-26 01:15:19 +0000482 SourceLocation TemplateKWLoc;
483 UnqualifiedId Name;
484
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000485 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000486 // destructor names and allow the action module to diagnose any semantic
487 // errors.
Richard Smith2db075b2013-03-26 01:15:19 +0000488 //
489 // C++11 [class.qual]p2:
490 // [...] in a using-declaration that is a member-declaration, if the name
491 // specified after the nested-name-specifier is the same as the identifier
492 // or the simple-template-id's template-name in the last component of the
493 // nested-name-specifier, the name is [...] considered to name the
494 // constructor.
495 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
496 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
497 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
498 !SS.getScopeRep()->getAsNamespace() &&
499 !SS.getScopeRep()->getAsNamespaceAlias()) {
500 SourceLocation IdLoc = ConsumeToken();
501 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
502 Name.setConstructorName(Type, IdLoc, IdLoc);
503 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
504 /*AllowDestructorName=*/ true,
505 /*AllowConstructorName=*/ true, ParsedType(),
506 TemplateKWLoc, Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000507 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000508 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000509 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000510
Richard Smith6b3d3e52013-02-20 19:22:51 +0000511 MaybeParseCXX11Attributes(Attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000512
513 // Maybe this is an alias-declaration.
514 bool IsAliasDecl = Tok.is(tok::equal);
515 TypeResult TypeAlias;
516 if (IsAliasDecl) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000517 // TODO: Can GNU attributes appear here?
Richard Smith162e1c12011-04-15 14:24:37 +0000518 ConsumeToken();
519
Richard Smith80ad52f2013-01-02 11:42:31 +0000520 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000521 diag::warn_cxx98_compat_alias_declaration :
522 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000523
Richard Smith3e4c6c42011-05-05 21:57:07 +0000524 // Type alias templates cannot be specialized.
525 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000526 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
527 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000528 SpecKind = 0;
529 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
530 SpecKind = 1;
531 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
532 SpecKind = 2;
533 if (SpecKind != -1) {
534 SourceRange Range;
535 if (SpecKind == 0)
536 Range = SourceRange(Name.TemplateId->LAngleLoc,
537 Name.TemplateId->RAngleLoc);
538 else
539 Range = TemplateInfo.getSourceRange();
540 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
541 << SpecKind << Range;
542 SkipUntil(tok::semi);
543 return 0;
544 }
545
Richard Smith162e1c12011-04-15 14:24:37 +0000546 // Name must be an identifier.
547 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
548 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
549 // No removal fixit: can't recover from this.
550 SkipUntil(tok::semi);
551 return 0;
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000552 } else if (HasTypenameKeyword)
Richard Smith162e1c12011-04-15 14:24:37 +0000553 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
554 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
555 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
556 else if (SS.isNotEmpty())
557 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
558 << FixItHint::CreateRemoval(SS.getRange());
559
Richard Smith3e4c6c42011-05-05 21:57:07 +0000560 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
561 Declarator::AliasTemplateContext :
Richard Smith6b3d3e52013-02-20 19:22:51 +0000562 Declarator::AliasDeclContext, AS, OwnedType,
563 &Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000564 } else {
565 // C++11 attributes are not allowed on a using-declaration, but GNU ones
566 // are.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000567 ProhibitAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000568
Richard Smith162e1c12011-04-15 14:24:37 +0000569 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith6b3d3e52013-02-20 19:22:51 +0000570 MaybeParseGNUAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000573 // Eat ';'.
574 DeclEnd = Tok.getLocation();
575 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000576 !Attrs.empty() ? "attributes list" :
Richard Smith162e1c12011-04-15 14:24:37 +0000577 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000578 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000579
John McCall78b81052010-11-10 02:40:36 +0000580 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000581 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000582 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000583 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000584 SourceRange R = TemplateInfo.getSourceRange();
585 Diag(UsingLoc, diag::err_templated_using_declaration)
586 << R << FixItHint::CreateRemoval(R);
587
588 // Unfortunately, we have to bail out instead of recovering by
589 // ignoring the parameters, just in case the nested name specifier
590 // depends on the parameters.
591 return 0;
592 }
593
Douglas Gregor480b53c2011-09-26 14:30:28 +0000594 // "typename" keyword is allowed for identifiers only,
595 // because it may be a type definition.
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000596 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor480b53c2011-09-26 14:30:28 +0000597 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
598 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000599 // Proceed parsing, but reset the HasTypenameKeyword flag.
600 HasTypenameKeyword = false;
Douglas Gregor480b53c2011-09-26 14:30:28 +0000601 }
602
Richard Smith3e4c6c42011-05-05 21:57:07 +0000603 if (IsAliasDecl) {
604 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000605 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000606 TemplateParams ? TemplateParams->data() : 0,
607 TemplateParams ? TemplateParams->size() : 0);
608 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000609 UsingLoc, Name, Attrs.getList(),
610 TypeAlias);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000611 }
Richard Smith162e1c12011-04-15 14:24:37 +0000612
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000613 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
614 /* HasUsingKeyword */ true, UsingLoc,
615 SS, Name, Attrs.getList(),
616 HasTypenameKeyword, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000617}
618
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000619/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000620///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000621/// [C++0x] static_assert-declaration:
622/// static_assert ( constant-expression , string-literal ) ;
623///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000624/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000625/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000626///
John McCalld226f652010-08-21 09:40:31 +0000627Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000628 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
629 "Not a static_assert declaration");
630
David Blaikie4e4d0842012-03-11 07:00:24 +0000631 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000632 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000633 if (Tok.is(tok::kw_static_assert))
634 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000635
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000636 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000638 BalancedDelimiterTracker T(*this, tok::l_paren);
639 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000640 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000641 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000642 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000643 }
Mike Stump1eb44332009-09-09 15:08:12 +0000644
John McCall60d7b3a2010-08-24 06:29:42 +0000645 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000646 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000647 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000648 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000649 }
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Anders Carlssonad5f9602009-03-13 23:29:20 +0000651 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000652 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000653
Richard Smith0cc323c2012-03-05 23:20:05 +0000654 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000655 Diag(Tok, diag::err_expected_string_literal)
656 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000657 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000658 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000659 }
Mike Stump1eb44332009-09-09 15:08:12 +0000660
John McCall60d7b3a2010-08-24 06:29:42 +0000661 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000662 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000663 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000664 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000665 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000666
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000667 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Chris Lattner97144fc2009-04-02 04:16:50 +0000669 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000670 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000671
John McCall9ae2f072010-08-23 23:25:46 +0000672 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
673 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000674 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000675 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000676}
677
Richard Smitha2c36462013-04-26 16:15:35 +0000678/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000679///
680/// 'decltype' ( expression )
Richard Smitha2c36462013-04-26 16:15:35 +0000681/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000682///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000683SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
684 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
685 && "Not a decltype specifier");
686
David Blaikie42d6d0c2011-12-04 05:04:18 +0000687 ExprResult Result;
688 SourceLocation StartLoc = Tok.getLocation();
689 SourceLocation EndLoc;
690
691 if (Tok.is(tok::annot_decltype)) {
692 Result = getExprAnnotation(Tok);
693 EndLoc = Tok.getAnnotationEndLoc();
694 ConsumeToken();
695 if (Result.isInvalid()) {
696 DS.SetTypeSpecError();
697 return EndLoc;
698 }
699 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000700 if (Tok.getIdentifierInfo()->isStr("decltype"))
701 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000702
David Blaikie42d6d0c2011-12-04 05:04:18 +0000703 ConsumeToken();
704
705 BalancedDelimiterTracker T(*this, tok::l_paren);
706 if (T.expectAndConsume(diag::err_expected_lparen_after,
707 "decltype", tok::r_paren)) {
708 DS.SetTypeSpecError();
709 return T.getOpenLocation() == Tok.getLocation() ?
710 StartLoc : T.getOpenLocation();
711 }
712
Richard Smitha2c36462013-04-26 16:15:35 +0000713 // Check for C++1y 'decltype(auto)'.
714 if (Tok.is(tok::kw_auto)) {
715 // No need to disambiguate here: an expression can't start with 'auto',
716 // because the typename-specifier in a function-style cast operation can't
717 // be 'auto'.
718 Diag(Tok.getLocation(),
719 getLangOpts().CPlusPlus1y
720 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
721 : diag::ext_decltype_auto_type_specifier);
722 ConsumeToken();
723 } else {
724 // Parse the expression
David Blaikie42d6d0c2011-12-04 05:04:18 +0000725
Richard Smitha2c36462013-04-26 16:15:35 +0000726 // C++11 [dcl.type.simple]p4:
727 // The operand of the decltype specifier is an unevaluated operand.
728 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
729 0, /*IsDecltype=*/true);
730 Result = ParseExpression();
731 if (Result.isInvalid()) {
732 DS.SetTypeSpecError();
733 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true,
734 /*DontConsume=*/true)) {
735 EndLoc = ConsumeParen();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000736 } else {
Richard Smitha2c36462013-04-26 16:15:35 +0000737 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
738 // Backtrack to get the location of the last token before the semi.
739 PP.RevertCachedTokens(2);
740 ConsumeToken(); // the semi.
741 EndLoc = ConsumeAnyToken();
742 assert(Tok.is(tok::semi));
743 } else {
744 EndLoc = Tok.getLocation();
745 }
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000746 }
Richard Smitha2c36462013-04-26 16:15:35 +0000747 return EndLoc;
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000748 }
Richard Smitha2c36462013-04-26 16:15:35 +0000749
750 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie42d6d0c2011-12-04 05:04:18 +0000751 }
752
753 // Match the ')'
754 T.consumeClose();
755 if (T.getCloseLocation().isInvalid()) {
756 DS.SetTypeSpecError();
757 // FIXME: this should return the location of the last token
758 // that was consumed (by "consumeClose()")
759 return T.getCloseLocation();
760 }
761
Richard Smith76f3f692012-02-22 02:04:18 +0000762 if (Result.isInvalid()) {
763 DS.SetTypeSpecError();
764 return T.getCloseLocation();
765 }
766
David Blaikie42d6d0c2011-12-04 05:04:18 +0000767 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000768 }
Richard Smitha2c36462013-04-26 16:15:35 +0000769 assert(!Result.isInvalid());
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000771 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000772 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000773 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smitha2c36462013-04-26 16:15:35 +0000774 if (Result.get()
775 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
776 DiagID, Result.release())
777 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
778 DiagID)) {
John McCallfec54012009-08-03 20:12:06 +0000779 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000780 DS.SetTypeSpecError();
781 }
782 return EndLoc;
783}
784
785void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
786 SourceLocation StartLoc,
787 SourceLocation EndLoc) {
788 // make sure we have a token we can turn into an annotation token
789 if (PP.isBacktrackEnabled())
790 PP.RevertCachedTokens(1);
791 else
792 PP.EnterToken(Tok);
793
794 Tok.setKind(tok::annot_decltype);
Richard Smitha2c36462013-04-26 16:15:35 +0000795 setExprAnnotation(Tok,
796 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
797 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
798 ExprError());
David Blaikie42d6d0c2011-12-04 05:04:18 +0000799 Tok.setAnnotationEndLoc(EndLoc);
800 Tok.setLocation(StartLoc);
801 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000802}
803
Sean Huntdb5d44b2011-05-19 05:37:45 +0000804void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
805 assert(Tok.is(tok::kw___underlying_type) &&
806 "Not an underlying type specifier");
807
808 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000809 BalancedDelimiterTracker T(*this, tok::l_paren);
810 if (T.expectAndConsume(diag::err_expected_lparen_after,
811 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000812 return;
813 }
814
815 TypeResult Result = ParseTypeName();
816 if (Result.isInvalid()) {
817 SkipUntil(tok::r_paren);
818 return;
819 }
820
821 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000822 T.consumeClose();
823 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000824 return;
825
826 const char *PrevSpec = 0;
827 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000828 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000829 DiagID, Result.release()))
830 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanella2d776342013-07-06 18:54:58 +0000831 DS.setTypeofParensRange(T.getRange());
Sean Huntdb5d44b2011-05-19 05:37:45 +0000832}
833
David Blaikie09048df2011-10-25 15:01:20 +0000834/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
835/// class name or decltype-specifier. Note that we only check that the result
836/// names a type; semantic analysis will need to verify that the type names a
837/// class. The result is either a type or null, depending on whether a type
838/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000839///
Richard Smith05321402013-02-19 23:47:15 +0000840/// base-type-specifier: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000841/// class-or-decltype
Richard Smith05321402013-02-19 23:47:15 +0000842/// class-or-decltype: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000843/// nested-name-specifier[opt] class-name
844/// decltype-specifier
Richard Smith05321402013-02-19 23:47:15 +0000845/// class-name: [C++ class.name]
Douglas Gregor42a552f2008-11-05 20:51:48 +0000846/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000847/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000848///
Richard Smith05321402013-02-19 23:47:15 +0000849/// In C++98, instead of base-type-specifier, we have:
850///
851/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie22216eb2011-10-25 17:10:12 +0000852Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
853 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000854 // Ignore attempts to use typename
855 if (Tok.is(tok::kw_typename)) {
856 Diag(Tok, diag::err_expected_class_name_not_template)
857 << FixItHint::CreateRemoval(Tok.getLocation());
858 ConsumeToken();
859 }
860
David Blaikie152aa4b2011-10-25 18:17:58 +0000861 // Parse optional nested-name-specifier
862 CXXScopeSpec SS;
863 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
864
865 BaseLoc = Tok.getLocation();
866
David Blaikie22216eb2011-10-25 17:10:12 +0000867 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000868 // tok == kw_decltype is just error recovery, it can only happen when SS
869 // isn't empty
870 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000871 if (SS.isNotEmpty())
872 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
873 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000874 // Fake up a Declarator to use with ActOnTypeName.
875 DeclSpec DS(AttrFactory);
876
David Blaikieb5777572011-12-08 04:53:15 +0000877 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000878
879 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
880 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
881 }
882
Douglas Gregor7f43d672009-02-25 23:52:28 +0000883 // Check whether we have a template-id that names a type.
884 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000885 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000886 if (TemplateId->Kind == TNK_Type_template ||
887 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000888 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000889
890 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000891 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000892 EndLocation = Tok.getAnnotationEndLoc();
893 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000894
895 if (Type)
896 return Type;
897 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000898 }
899
900 // Fall through to produce an error below.
901 }
902
Douglas Gregor42a552f2008-11-05 20:51:48 +0000903 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000904 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000905 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000906 }
907
Douglas Gregor84d0a192010-01-12 21:28:44 +0000908 IdentifierInfo *Id = Tok.getIdentifierInfo();
909 SourceLocation IdLoc = ConsumeToken();
910
911 if (Tok.is(tok::less)) {
912 // It looks the user intended to write a template-id here, but the
913 // template-name was wrong. Try to fix that.
914 TemplateNameKind TNK = TNK_Type_template;
915 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000916 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000917 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000918 Diag(IdLoc, diag::err_unknown_template_name)
919 << Id;
920 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000921
Douglas Gregor84d0a192010-01-12 21:28:44 +0000922 if (!Template)
923 return true;
924
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000925 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000926 UnqualifiedId TemplateName;
927 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000928
Douglas Gregor84d0a192010-01-12 21:28:44 +0000929 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000930 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
931 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000932 return true;
933 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000934 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000935
Douglas Gregor84d0a192010-01-12 21:28:44 +0000936 // If we didn't end up with a typename token, there's nothing more we
937 // can do.
938 if (Tok.isNot(tok::annot_typename))
939 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000940
Douglas Gregor84d0a192010-01-12 21:28:44 +0000941 // Retrieve the type from the annotation token, consume that token, and
942 // return.
943 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000944 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000945 ConsumeToken();
946 return Type;
947 }
948
Douglas Gregor42a552f2008-11-05 20:51:48 +0000949 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000950 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000951 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000952 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000953 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000954 /*NonTrivialTypeSourceInfo=*/true,
955 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000956 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000957 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000958 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000959 }
960
961 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000962 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000963
964 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000965 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000966 DS.SetRangeStart(IdLoc);
967 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000968 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000969
970 const char *PrevSpec = 0;
971 unsigned DiagID;
972 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
973
974 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
975 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000976}
977
John McCallc052dbb2012-05-22 21:28:12 +0000978void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
979 while (Tok.is(tok::kw___single_inheritance) ||
980 Tok.is(tok::kw___multiple_inheritance) ||
981 Tok.is(tok::kw___virtual_inheritance)) {
982 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
983 SourceLocation AttrNameLoc = ConsumeToken();
984 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000985 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000986 }
987}
988
Richard Smithc9f35172012-06-25 21:37:02 +0000989/// Determine whether the following tokens are valid after a type-specifier
990/// which could be a standalone declaration. This will conservatively return
991/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000992bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000993 // This switch enumerates the valid "follow" set for type-specifiers.
994 switch (Tok.getKind()) {
995 default: break;
996 case tok::semi: // struct foo {...} ;
997 case tok::star: // struct foo {...} * P;
998 case tok::amp: // struct foo {...} & R = ...
Richard Smithba65f502013-01-19 03:48:05 +0000999 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-06-25 21:37:02 +00001000 case tok::identifier: // struct foo {...} V ;
1001 case tok::r_paren: //(struct foo {...} ) {4}
1002 case tok::annot_cxxscope: // struct foo {...} a:: b;
1003 case tok::annot_typename: // struct foo {...} a ::b;
1004 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1005 case tok::l_paren: // struct foo {...} ( x);
1006 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smithba65f502013-01-19 03:48:05 +00001007 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smithc9f35172012-06-25 21:37:02 +00001008 return true;
Richard Smith139be702012-07-02 19:14:01 +00001009 case tok::colon:
1010 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +00001011 // Type qualifiers
1012 case tok::kw_const: // struct foo {...} const x;
1013 case tok::kw_volatile: // struct foo {...} volatile x;
1014 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smithba65f502013-01-19 03:48:05 +00001015 // Function specifiers
1016 // Note, no 'explicit'. An explicit function must be either a conversion
1017 // operator or a constructor. Either way, it can't have a return type.
1018 case tok::kw_inline: // struct foo inline f();
1019 case tok::kw_virtual: // struct foo virtual f();
1020 case tok::kw_friend: // struct foo friend f();
Richard Smithc9f35172012-06-25 21:37:02 +00001021 // Storage-class specifiers
1022 case tok::kw_static: // struct foo {...} static x;
1023 case tok::kw_extern: // struct foo {...} extern x;
1024 case tok::kw_typedef: // struct foo {...} typedef x;
1025 case tok::kw_register: // struct foo {...} register x;
1026 case tok::kw_auto: // struct foo {...} auto x;
1027 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smithba65f502013-01-19 03:48:05 +00001028 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-06-25 21:37:02 +00001029 case tok::kw_constexpr: // struct foo {...} constexpr x;
1030 // As shown above, type qualifiers and storage class specifiers absolutely
1031 // can occur after class specifiers according to the grammar. However,
1032 // almost no one actually writes code like this. If we see one of these,
1033 // it is much more likely that someone missed a semi colon and the
1034 // type/storage class specifier we're seeing is part of the *next*
1035 // intended declaration, as in:
1036 //
1037 // struct foo { ... }
1038 // typedef int X;
1039 //
1040 // We'd really like to emit a missing semicolon error instead of emitting
1041 // an error on the 'int' saying that you can't have two type specifiers in
1042 // the same declaration of X. Because of this, we look ahead past this
1043 // token to see if it's a type specifier. If so, we know the code is
1044 // otherwise invalid, so we can produce the expected semi error.
1045 if (!isKnownToBeTypeSpecifier(NextToken()))
1046 return true;
1047 break;
1048 case tok::r_brace: // struct bar { struct foo {...} }
1049 // Missing ';' at end of struct is accepted as an extension in C mode.
1050 if (!getLangOpts().CPlusPlus)
1051 return true;
1052 break;
Richard Smithba65f502013-01-19 03:48:05 +00001053 // C++11 attributes
1054 case tok::l_square: // enum E [[]] x
1055 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1056 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith8338a9d2013-01-29 04:13:32 +00001057 case tok::greater:
1058 // template<class T = class X>
1059 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001060 }
1061 return false;
1062}
1063
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001064/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1065/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1066/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001067/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001068///
1069/// class-specifier: [C++ class]
1070/// class-head '{' member-specification[opt] '}'
1071/// class-head '{' member-specification[opt] '}' attributes[opt]
1072/// class-head:
1073/// class-key identifier[opt] base-clause[opt]
1074/// class-key nested-name-specifier identifier base-clause[opt]
1075/// class-key nested-name-specifier[opt] simple-template-id
1076/// base-clause[opt]
1077/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001078/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001079/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001080/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001081/// simple-template-id base-clause[opt]
1082/// class-key:
1083/// 'class'
1084/// 'struct'
1085/// 'union'
1086///
1087/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001088/// class-key ::[opt] nested-name-specifier[opt] identifier
1089/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1090/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001091///
1092/// Note that the C++ class-specifier and elaborated-type-specifier,
1093/// together, subsume the C99 struct-or-union-specifier:
1094///
1095/// struct-or-union-specifier: [C99 6.7.2.1]
1096/// struct-or-union identifier[opt] '{' struct-contents '}'
1097/// struct-or-union identifier
1098/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1099/// '}' attributes[opt]
1100/// [GNU] struct-or-union attributes[opt] identifier
1101/// struct-or-union:
1102/// 'struct'
1103/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001104void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1105 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001106 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001107 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001108 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001109 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001110 DeclSpec::TST TagType;
1111 if (TagTokKind == tok::kw_struct)
1112 TagType = DeclSpec::TST_struct;
1113 else if (TagTokKind == tok::kw___interface)
1114 TagType = DeclSpec::TST_interface;
1115 else if (TagTokKind == tok::kw_class)
1116 TagType = DeclSpec::TST_class;
1117 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001118 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1119 TagType = DeclSpec::TST_union;
1120 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001121
Douglas Gregor374929f2009-09-18 15:37:17 +00001122 if (Tok.is(tok::code_completion)) {
1123 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001124 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001125 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001126 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001127
Chandler Carruth926c4b42010-06-28 08:39:25 +00001128 // C++03 [temp.explicit] 14.7.2/8:
1129 // The usual access checking rules do not apply to names used to specify
1130 // explicit instantiations.
1131 //
1132 // As an extension we do not perform access checking on the names used to
1133 // specify explicit specializations either. This is important to allow
1134 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001135 //
1136 // Note that we don't suppress if this turns out to be an elaborated
1137 // type specifier.
1138 bool shouldDelayDiagsInTag =
1139 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1140 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1141 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001142
Sean Hunt2edf0a22012-06-23 05:07:58 +00001143 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001144 // If attributes exist after tag, parse them.
1145 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001146 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001147
Steve Narofff59e17e2008-12-24 20:59:21 +00001148 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001149 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001150 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001151
John McCallc052dbb2012-05-22 21:28:12 +00001152 // Parse inheritance specifiers.
1153 if (Tok.is(tok::kw___single_inheritance) ||
1154 Tok.is(tok::kw___multiple_inheritance) ||
1155 Tok.is(tok::kw___virtual_inheritance))
1156 ParseMicrosoftInheritanceClassAttributes(attrs);
1157
Sean Huntbbd37c62009-11-21 08:43:09 +00001158 // If C++0x attributes exist here, parse them.
1159 // FIXME: Are we consistent with the ordering of parsing of different
1160 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001161 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Michael Han07fc1ba2013-01-07 16:57:11 +00001163 // Source location used by FIXIT to insert misplaced
1164 // C++11 attributes
1165 SourceLocation AttrFixitLoc = Tok.getLocation();
1166
John Wiegley20c0da72011-04-27 23:09:49 +00001167 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001168 !Tok.is(tok::identifier) &&
1169 Tok.getIdentifierInfo() &&
1170 (Tok.is(tok::kw___is_arithmetic) ||
1171 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001172 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001173 Tok.is(tok::kw___is_floating_point) ||
1174 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001175 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001176 Tok.is(tok::kw___is_integral) ||
1177 Tok.is(tok::kw___is_member_function_pointer) ||
1178 Tok.is(tok::kw___is_member_pointer) ||
1179 Tok.is(tok::kw___is_pod) ||
1180 Tok.is(tok::kw___is_pointer) ||
1181 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001182 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001183 Tok.is(tok::kw___is_signed) ||
1184 Tok.is(tok::kw___is_unsigned) ||
1185 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001186 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001187 // name of struct templates, but some are keywords in GCC >= 4.3
1188 // and Clang. Therefore, when we see the token sequence "struct
1189 // X", make X into a normal identifier rather than a keyword, to
1190 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001191 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001192 Tok.setKind(tok::identifier);
1193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001195 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001196 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001197 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001198 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1199 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001200
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001201 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001202 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001203 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001204 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1205 Diag(Tok, diag::err_expected_ident);
1206 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001207
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001208 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1209
Douglas Gregorcc636682009-02-17 23:15:12 +00001210 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001211 IdentifierInfo *Name = 0;
1212 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001213 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001214 if (Tok.is(tok::identifier)) {
1215 Name = Tok.getIdentifierInfo();
1216 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001217
David Blaikie4e4d0842012-03-11 07:00:24 +00001218 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001219 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001220 // Eat the template argument list and try to continue parsing this as
1221 // a class (or template thereof).
1222 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001223 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001224 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001225 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001226 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001227 // We couldn't parse the template argument list at all, so don't
1228 // try to give any location information for the list.
1229 LAngleLoc = RAngleLoc = SourceLocation();
1230 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001231
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001232 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001233 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1234 << (TagType == DeclSpec::TST_class? 0
1235 : TagType == DeclSpec::TST_struct? 1
1236 : TagType == DeclSpec::TST_interface? 2
1237 : 3)
1238 << Name
1239 << SourceRange(LAngleLoc, RAngleLoc);
1240
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001241 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001242 // we've removed its template argument list.
1243 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1244 if (TemplateParams && TemplateParams->size() > 1) {
1245 TemplateParams->pop_back();
1246 } else {
1247 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001248 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001249 = ParsedTemplateInfo::NonTemplate;
1250 }
1251 } else if (TemplateInfo.Kind
1252 == ParsedTemplateInfo::ExplicitInstantiation) {
1253 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001254 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001255 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001256 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001257 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001258 = SourceLocation();
1259 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1260 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001261 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001262 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001263 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001264 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001265 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001266
Douglas Gregor059101f2011-03-02 00:47:37 +00001267 if (TemplateId->Kind != TNK_Type_template &&
1268 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001269 // The template-name in the simple-template-id refers to
1270 // something other than a class template. Give an appropriate
1271 // error message and skip to the ';'.
1272 SourceRange Range(NameLoc);
1273 if (SS.isNotEmpty())
1274 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001275
Douglas Gregor39a8de12009-02-25 19:37:18 +00001276 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu6e91f4b2013-06-19 22:25:01 +00001277 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Douglas Gregor39a8de12009-02-25 19:37:18 +00001279 DS.SetTypeSpecError();
1280 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001281 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001282 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001283 }
1284
Richard Smith7796eb52012-03-12 08:56:40 +00001285 // There are four options here.
1286 // - If we are in a trailing return type, this is always just a reference,
1287 // and we must not try to parse a definition. For instance,
1288 // [] () -> struct S { };
1289 // does not define a type.
1290 // - If we have 'struct foo {...', 'struct foo :...',
1291 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1292 // - If we have 'struct foo;', then this is either a forward declaration
1293 // or a friend declaration, which have to be treated differently.
1294 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001295 //
1296 // We also detect these erroneous cases to provide better diagnostic for
1297 // C++11 attributes parsing.
1298 // - attributes follow class name:
1299 // struct foo [[]] {};
1300 // - attributes appear before or after 'final':
1301 // struct foo [[]] final [[]] {};
1302 //
Richard Smith69730c12012-03-12 07:56:15 +00001303 // However, in type-specifier-seq's, things look like declarations but are
1304 // just references, e.g.
1305 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001306 // or
Richard Smith69730c12012-03-12 07:56:15 +00001307 // &T::operator struct s;
1308 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001309
1310 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001311 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001312
John McCallf312b1e2010-08-26 23:41:50 +00001313 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001314 if (DSC == DSC_trailing)
1315 TUK = Sema::TUK_Reference;
1316 else if (Tok.is(tok::l_brace) ||
1317 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001318 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001319 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001320 if (DS.isFriendSpecified()) {
1321 // C++ [class.friend]p2:
1322 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001323 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001324 << SourceRange(DS.getFriendSpecLoc());
1325
1326 // Skip everything up to the semicolon, so that this looks like a proper
1327 // friend class (or template thereof) declaration.
1328 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001329 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001330 } else {
1331 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001332 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001333 }
Richard Smith150d8532013-02-22 06:46:23 +00001334 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1335 NextToken().is(tok::kw_alignas))) {
Michael Han2e397132012-11-26 22:54:45 +00001336 // We can't tell if this is a definition or reference
1337 // until we skipped the 'final' and C++11 attribute specifiers.
1338 TentativeParsingAction PA(*this);
1339
1340 // Skip the 'final' keyword.
1341 ConsumeToken();
1342
1343 // Skip C++11 attribute specifiers.
1344 while (true) {
1345 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1346 ConsumeBracket();
1347 if (!SkipUntil(tok::r_square))
1348 break;
Richard Smith150d8532013-02-22 06:46:23 +00001349 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han2e397132012-11-26 22:54:45 +00001350 ConsumeToken();
1351 ConsumeParen();
1352 if (!SkipUntil(tok::r_paren))
1353 break;
1354 } else {
1355 break;
1356 }
1357 }
1358
1359 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1360 TUK = Sema::TUK_Definition;
1361 else
1362 TUK = Sema::TUK_Reference;
1363
1364 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001365 } else if (DSC != DSC_type_specifier &&
1366 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001367 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001368 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001369 if (Tok.isNot(tok::semi)) {
1370 // A semicolon was missing after this declaration. Diagnose and recover.
1371 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1372 DeclSpec::getSpecifierName(TagType));
1373 PP.EnterToken(Tok);
1374 Tok.setKind(tok::semi);
1375 }
Richard Smithc9f35172012-06-25 21:37:02 +00001376 } else
John McCallf312b1e2010-08-26 23:41:50 +00001377 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001378
Michael Han2e397132012-11-26 22:54:45 +00001379 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1380 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001381 if (TUK != Sema::TUK_Reference) {
1382 // If this is not a reference, then the only possible
1383 // valid place for C++11 attributes to appear here
1384 // is between class-key and class-name. If there are
1385 // any attributes after class-name, we try a fixit to move
1386 // them to the right place.
1387 SourceRange AttrRange = Attributes.Range;
1388 if (AttrRange.isValid()) {
1389 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1390 << AttrRange
1391 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1392 CharSourceRange(AttrRange, true))
1393 << FixItHint::CreateRemoval(AttrRange);
1394
1395 // Recover by adding misplaced attributes to the attribute list
1396 // of the class so they can be applied on the class later.
1397 attrs.takeAllFrom(Attributes);
1398 }
1399 }
Michael Han2e397132012-11-26 22:54:45 +00001400
John McCall13489672012-05-07 06:16:58 +00001401 // If this is an elaborated type specifier, and we delayed
1402 // diagnostics before, just merge them into the current pool.
1403 if (shouldDelayDiagsInTag) {
1404 diagsFromTag.done();
1405 if (TUK == Sema::TUK_Reference)
1406 diagsFromTag.redelay();
1407 }
1408
John McCall207014e2010-07-30 06:26:29 +00001409 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001410 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001411 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1412 // We have a declaration or reference to an anonymous class.
1413 Diag(StartLoc, diag::err_anon_type_definition)
1414 << DeclSpec::getSpecifierName(TagType);
1415 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001416
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001417 SkipUntil(tok::comma, true);
1418 return;
1419 }
1420
Douglas Gregorddc29e12009-02-06 22:42:48 +00001421 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001422 DeclResult TagOrTempResult = true; // invalid
1423 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001424
Douglas Gregor402abb52009-05-28 23:31:59 +00001425 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001426 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001427 // Explicit specialization, class template partial specialization,
1428 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001429 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001430 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001431 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001432 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001433 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001434 ProhibitAttributes(attrs);
1435
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001436 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001437 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001438 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001439 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001440 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001441 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001442 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001443 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001444 TemplateId->TemplateNameLoc,
1445 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001446 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001447 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001448 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001449
1450 // Friend template-ids are treated as references unless
1451 // they have template headers, in which case they're ill-formed
1452 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1453 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001454 } else if (TUK == Sema::TUK_Reference ||
1455 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001456 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001457 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001458 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001459 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001460 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001461 TemplateId->Template,
1462 TemplateId->TemplateNameLoc,
1463 TemplateId->LAngleLoc,
1464 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001465 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001466 } else {
1467 // This is an explicit specialization or a class template
1468 // partial specialization.
1469 TemplateParameterLists FakedParamLists;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001470 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1471 // This looks like an explicit instantiation, because we have
1472 // something like
1473 //
1474 // template class Foo<X>
1475 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001476 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001477 // meant to be an explicit specialization, but the user forgot
1478 // the '<>' after 'template'.
Larisse Voufo49854292013-06-22 13:56:11 +00001479 // It this is friend declaration however, since it cannot have a
1480 // template header, it is most likely that the user meant to
1481 // remove the 'template' keyword.
1482 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
1483 "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001484
Larisse Voufo49854292013-06-22 13:56:11 +00001485 if (TUK == Sema::TUK_Friend) {
1486 Diag(DS.getFriendSpecLoc(),
1487 diag::err_friend_explicit_instantiation);
1488 TemplateParams = 0;
1489 } else {
1490 SourceLocation LAngleLoc
1491 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1492 Diag(TemplateId->TemplateNameLoc,
1493 diag::err_explicit_instantiation_with_definition)
1494 << SourceRange(TemplateInfo.TemplateLoc)
1495 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1496
1497 // Create a fake template parameter list that contains only
1498 // "template<>", so that we treat this construct as a class
1499 // template specialization.
1500 FakedParamLists.push_back(
1501 Actions.ActOnTemplateParameterList(0, SourceLocation(),
1502 TemplateInfo.TemplateLoc,
1503 LAngleLoc,
1504 0, 0,
1505 LAngleLoc));
1506 TemplateParams = &FakedParamLists;
1507 }
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001508 }
1509
1510 // Build the class template specialization.
1511 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001512 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001513 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001514 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001515 TemplateId->TemplateNameLoc,
1516 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001517 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001518 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001519 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001520 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001521 TemplateParams? &(*TemplateParams)[0] : 0,
1522 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001523 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001524 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001525 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001526 // Explicit instantiation of a member of a class template
1527 // specialization, e.g.,
1528 //
1529 // template struct Outer<int>::Inner;
1530 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001531 ProhibitAttributes(attrs);
1532
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001533 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001534 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001535 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001536 TemplateInfo.TemplateLoc,
1537 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001538 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001539 } else if (TUK == Sema::TUK_Friend &&
1540 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001541 ProhibitAttributes(attrs);
1542
John McCall9a34edb2010-10-19 01:40:49 +00001543 TagOrTempResult =
1544 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1545 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001546 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001547 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001548 TemplateParams? &(*TemplateParams)[0] : 0,
1549 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001550 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001551 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1552 ProhibitAttributes(attrs);
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001553
1554 if (TUK == Sema::TUK_Definition &&
1555 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1556 // If the declarator-id is not a template-id, issue a diagnostic and
1557 // recover by ignoring the 'template' keyword.
1558 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1559 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufo49854292013-06-22 13:56:11 +00001560 TemplateParams = 0;
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001561 }
Sean Hunt2edf0a22012-06-23 05:07:58 +00001562
John McCallc4e70192009-09-11 04:59:25 +00001563 bool IsDependent = false;
1564
John McCalla25c4082010-10-19 18:40:57 +00001565 // Don't pass down template parameter lists if this is just a tag
1566 // reference. For example, we don't need the template parameters here:
1567 // template <class T> class A *makeA(T t);
1568 MultiTemplateParamsArg TParams;
1569 if (TUK != Sema::TUK_Reference && TemplateParams)
1570 TParams =
1571 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1572
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001573 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001574 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001575 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001576 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001577 TParams, Owned, IsDependent,
1578 SourceLocation(), false,
1579 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001580
1581 // If ActOnTag said the type was dependent, try again with the
1582 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001583 if (IsDependent) {
1584 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001585 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001586 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001587 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001588 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001589
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001590 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001591 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001592 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001593 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001594 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001595 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001596 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1597 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001598 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001599 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001600 }
1601
John McCallb3d87482010-08-24 05:47:05 +00001602 const char *PrevSpec = 0;
1603 unsigned DiagID;
1604 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001605 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001606 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1607 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001608 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001609 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001610 Result = DS.SetTypeSpecType(TagType, StartLoc,
1611 NameLoc.isValid() ? NameLoc : StartLoc,
1612 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001613 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001614 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001615 return;
1616 }
Mike Stump1eb44332009-09-09 15:08:12 +00001617
John McCallb3d87482010-08-24 05:47:05 +00001618 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001619 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001620
Chris Lattner4ed5d912010-02-02 01:23:29 +00001621 // At this point, we've successfully parsed a class-specifier in 'definition'
1622 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1623 // going to look at what comes after it to improve error recovery. If an
1624 // impossible token occurs next, we assume that the programmer forgot a ; at
1625 // the end of the declaration and recover that way.
1626 //
Richard Smithc9f35172012-06-25 21:37:02 +00001627 // Also enforce C++ [temp]p3:
1628 // In a template-declaration which defines a class, no declarator
1629 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001630 if (TUK == Sema::TUK_Definition &&
1631 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001632 if (Tok.isNot(tok::semi)) {
1633 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1634 DeclSpec::getSpecifierName(TagType));
1635 // Push this token back into the preprocessor and change our current token
1636 // to ';' so that the rest of the code recovers as though there were an
1637 // ';' after the definition.
1638 PP.EnterToken(Tok);
1639 Tok.setKind(tok::semi);
1640 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001641 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001642}
1643
Mike Stump1eb44332009-09-09 15:08:12 +00001644/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001645///
1646/// base-clause : [C++ class.derived]
1647/// ':' base-specifier-list
1648/// base-specifier-list:
1649/// base-specifier '...'[opt]
1650/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001651void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001652 assert(Tok.is(tok::colon) && "Not a base clause");
1653 ConsumeToken();
1654
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001655 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001656 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001657
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001658 while (true) {
1659 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001660 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001661 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001662 // Skip the rest of this base specifier, up until the comma or
1663 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001664 SkipUntil(tok::comma, tok::l_brace, true, true);
1665 } else {
1666 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001667 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001668 }
1669
1670 // If the next token is a comma, consume it and keep reading
1671 // base-specifiers.
1672 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001674 // Consume the comma.
1675 ConsumeToken();
1676 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001677
1678 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001679 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001680}
1681
1682/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1683/// one entry in the base class list of a class specifier, for example:
1684/// class foo : public bar, virtual private baz {
1685/// 'public bar' and 'virtual private baz' are each base-specifiers.
1686///
1687/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001688/// attribute-specifier-seq[opt] base-type-specifier
1689/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1690/// base-type-specifier
1691/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1692/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001693Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001694 bool IsVirtual = false;
1695 SourceLocation StartLoc = Tok.getLocation();
1696
Richard Smith05321402013-02-19 23:47:15 +00001697 ParsedAttributesWithRange Attributes(AttrFactory);
1698 MaybeParseCXX11Attributes(Attributes);
1699
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001700 // Parse the 'virtual' keyword.
1701 if (Tok.is(tok::kw_virtual)) {
1702 ConsumeToken();
1703 IsVirtual = true;
1704 }
1705
Richard Smith05321402013-02-19 23:47:15 +00001706 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1707
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001708 // Parse an (optional) access specifier.
1709 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001710 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001711 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Richard Smith05321402013-02-19 23:47:15 +00001713 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1714
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001715 // Parse the 'virtual' keyword (again!), in case it came after the
1716 // access specifier.
1717 if (Tok.is(tok::kw_virtual)) {
1718 SourceLocation VirtualLoc = ConsumeToken();
1719 if (IsVirtual) {
1720 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001721 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001722 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001723 }
1724
1725 IsVirtual = true;
1726 }
1727
Richard Smith05321402013-02-19 23:47:15 +00001728 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1729
Douglas Gregor42a552f2008-11-05 20:51:48 +00001730 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001731 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001732 SourceLocation BaseLoc;
1733 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001734 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001735 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001737 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1738 // actually part of the base-specifier-list grammar productions, but we
1739 // parse it here for convenience.
1740 SourceLocation EllipsisLoc;
1741 if (Tok.is(tok::ellipsis))
1742 EllipsisLoc = ConsumeToken();
1743
Mike Stump1eb44332009-09-09 15:08:12 +00001744 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001745 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001746
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001747 // Notify semantic analysis that we have parsed a complete
1748 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001749 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1750 Access, BaseType.get(), BaseLoc,
1751 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001752}
1753
1754/// getAccessSpecifierIfPresent - Determine whether the next token is
1755/// a C++ access-specifier.
1756///
1757/// access-specifier: [C++ class.derived]
1758/// 'private'
1759/// 'protected'
1760/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001761AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001762 switch (Tok.getKind()) {
1763 default: return AS_none;
1764 case tok::kw_private: return AS_private;
1765 case tok::kw_protected: return AS_protected;
1766 case tok::kw_public: return AS_public;
1767 }
1768}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001769
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001770/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001771/// delayed, e.g., default arguments, create a late-parsed method declaration
1772/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001773void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1774 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001775 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001776 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001777 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001778 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001779 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001780
Eli Friedmand33133c2009-07-22 21:45:50 +00001781 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1782 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1783 if (!LateMethod) {
1784 // Push this method onto the stack of late-parsed method
1785 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001786 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1787 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001788 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001789
1790 // Add all of the parameters prior to this one (they don't
1791 // have default arguments).
1792 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1793 for (unsigned I = 0; I < ParamIdx; ++I)
1794 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001795 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001796 }
1797
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001798 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001799 // not have a default argument).
1800 LateMethod->DefaultArgs.push_back(
1801 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1802 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1803 }
1804 }
1805}
1806
Richard Smith4e24f0f2013-01-02 12:01:23 +00001807/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001808/// virt-specifier.
1809///
1810/// virt-specifier:
1811/// override
1812/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001813VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001814 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001815 return VirtSpecifiers::VS_None;
1816
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001817 if (Tok.is(tok::identifier)) {
1818 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001819
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001820 // Initialize the contextual keywords.
1821 if (!Ident_final) {
1822 Ident_final = &PP.getIdentifierTable().get("final");
1823 Ident_override = &PP.getIdentifierTable().get("override");
1824 }
1825
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001826 if (II == Ident_override)
1827 return VirtSpecifiers::VS_Override;
1828
1829 if (II == Ident_final)
1830 return VirtSpecifiers::VS_Final;
1831 }
1832
1833 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001834}
1835
Richard Smith4e24f0f2013-01-02 12:01:23 +00001836/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001837///
1838/// virt-specifier-seq:
1839/// virt-specifier
1840/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001841void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001842 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001843 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001844 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001845 if (Specifier == VirtSpecifiers::VS_None)
1846 return;
1847
1848 // C++ [class.mem]p8:
1849 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001850 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001851 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001852 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1853 << PrevSpec
1854 << FixItHint::CreateRemoval(Tok.getLocation());
1855
John McCalle402e722012-09-25 07:32:39 +00001856 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1857 Diag(Tok.getLocation(), diag::err_override_control_interface)
1858 << VirtSpecifiers::getSpecifierName(Specifier);
1859 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00001860 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00001861 diag::warn_cxx98_compat_override_control_keyword :
1862 diag::ext_override_control_keyword)
1863 << VirtSpecifiers::getSpecifierName(Specifier);
1864 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001865 ConsumeToken();
1866 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001867}
1868
Richard Smith4e24f0f2013-01-02 12:01:23 +00001869/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001870/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001871bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001872 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001873 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001874
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001875 if (!Tok.is(tok::identifier))
1876 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001877
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001878 // Initialize the contextual keywords.
1879 if (!Ident_final) {
1880 Ident_final = &PP.getIdentifierTable().get("final");
1881 Ident_override = &PP.getIdentifierTable().get("override");
1882 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001883
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001884 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001885}
1886
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001887/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1888///
1889/// member-declaration:
1890/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1891/// function-definition ';'[opt]
1892/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1893/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001894/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001895/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001896/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001897///
1898/// member-declarator-list:
1899/// member-declarator
1900/// member-declarator-list ',' member-declarator
1901///
1902/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001903/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001904/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001905/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001906/// identifier[opt] ':' constant-expression
1907///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001908/// virt-specifier-seq:
1909/// virt-specifier
1910/// virt-specifier-seq virt-specifier
1911///
1912/// virt-specifier:
1913/// override
1914/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001915///
Sebastian Redle2b68332009-04-12 17:16:29 +00001916/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001917/// '= 0'
1918///
1919/// constant-initializer:
1920/// '=' constant-expression
1921///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001922void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001923 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001924 const ParsedTemplateInfo &TemplateInfo,
1925 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001926 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001927 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001928 Diag(Tok, diag::err_at_defs_cxx);
1929 else
1930 Diag(Tok, diag::err_at_in_class);
1931
1932 ConsumeToken();
1933 SkipUntil(tok::r_brace);
1934 return;
1935 }
1936
John McCall60fa3cf2009-12-11 02:10:03 +00001937 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001938 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001939 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001940 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1941 if (TryAnnotateCXXScopeToken())
1942 MalformedTypeSpec = true;
1943
1944 bool isAccessDecl;
1945 if (Tok.isNot(tok::annot_cxxscope))
1946 isAccessDecl = false;
1947 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001948 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1949 else
1950 isAccessDecl = NextToken().is(tok::kw_operator);
1951
1952 if (isAccessDecl) {
1953 // Collect the scope specifier token we annotated earlier.
1954 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001955 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1956 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001957
1958 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001959 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001960 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001961 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1962 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001963 SkipUntil(tok::semi);
1964 return;
1965 }
1966
1967 // TODO: recover from mistakenly-qualified operator declarations.
1968 if (ExpectAndConsume(tok::semi,
1969 diag::err_expected_semi_after,
1970 "access declaration",
1971 tok::semi))
1972 return;
1973
Douglas Gregor23c94db2010-07-02 17:43:08 +00001974 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00001975 /* HasUsingKeyword */ false,
1976 SourceLocation(),
John McCall60fa3cf2009-12-11 02:10:03 +00001977 SS, Name,
1978 /* AttrList */ 0,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00001979 /* HasTypenameKeyword */ false,
John McCall60fa3cf2009-12-11 02:10:03 +00001980 SourceLocation());
1981 return;
1982 }
1983 }
1984
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001985 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001986 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001987 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001988 SourceLocation DeclEnd;
1989 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001990 return;
1991 }
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Chris Lattner682bf922009-03-29 16:50:03 +00001993 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001994 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001995 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001996 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001997 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001998 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001999 return;
2000 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00002001
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002002 // Handle: member-declaration ::= '__extension__' member-declaration
2003 if (Tok.is(tok::kw___extension__)) {
2004 // __extension__ silences extension warnings in the subexpression.
2005 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2006 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002007 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2008 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002009 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002010
Chris Lattner4ed5d912010-02-02 01:23:29 +00002011 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2012 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002013 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002014
John McCall0b7e6782011-03-24 11:26:52 +00002015 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00002016 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002017 // Optional C++11 attribute-specifier
2018 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00002019 // We need to keep these attributes for future diagnostic
2020 // before they are taken over by declaration specifier.
2021 FnAttrs.addAll(attrs.getList());
2022 FnAttrs.Range = attrs.Range;
2023
John McCall7f040a92010-12-24 02:08:15 +00002024 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002025
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002026 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00002027 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002028
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002029 // Eat 'using'.
2030 SourceLocation UsingLoc = ConsumeToken();
2031
2032 if (Tok.is(tok::kw_namespace)) {
2033 Diag(UsingLoc, diag::err_using_namespace_in_class);
2034 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00002035 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002036 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00002037 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00002038 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2039 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002040 }
2041 return;
2042 }
2043
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002044 // Hold late-parsed attributes so we can attach a Decl to them later.
2045 LateParsedAttrList CommonLateParsedAttrs;
2046
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002047 // decl-specifier-seq:
2048 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00002049 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00002050 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00002051 if (MalformedTypeSpec)
2052 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002053 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2054 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002055
Benjamin Kramer5354e772012-08-23 23:38:35 +00002056 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00002057 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2058 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2059
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002060 if (Tok.is(tok::semi)) {
2061 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002062
2063 if (DS.isFriendSpecified())
2064 ProhibitAttributes(FnAttrs);
2065
John McCalld226f652010-08-21 09:40:31 +00002066 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002067 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002068 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002069 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002070 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002071
John McCall54abf7d2009-11-04 02:18:39 +00002072 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002073 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002074
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002075 // Hold late-parsed attributes so we can attach a Decl to them later.
2076 LateParsedAttrList LateParsedAttrs;
2077
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002078 SourceLocation EqualLoc;
2079 bool HasInitializer = false;
2080 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002081 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002082 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2083 ColonProtectionRAIIObject X(*this);
2084
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002085 // Parse the first declarator.
2086 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002087 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002088 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002089 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002090 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002091 if (Tok.is(tok::semi))
2092 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002093 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002094 }
2095
Richard Smith4e24f0f2013-01-02 12:01:23 +00002096 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002097
John Thompson1b2fc0f2009-11-25 22:58:06 +00002098 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002099 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002100
Francois Pichet6a247472011-05-11 02:14:46 +00002101 // MSVC permits pure specifier on inline functions declared at class scope.
2102 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002103 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002104 DeclaratorInfo.isFunctionDeclarator() &&
2105 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002106 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002107 Init = ParseInitializer();
2108 if (Init.isInvalid())
2109 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002110 else
2111 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002112 }
2113
Douglas Gregor45fa5602011-11-07 20:56:01 +00002114 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002115 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002116 //
2117 // In C++11, a non-function declarator followed by an open brace is a
2118 // braced-init-list for an in-class member initialization, not an
2119 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002120 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002121 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002122 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002123 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002124 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002125 } else if (Tok.is(tok::equal)) {
2126 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002127 if (KW.is(tok::kw_default))
2128 DefinitionKind = FDK_Defaulted;
2129 else if (KW.is(tok::kw_delete))
2130 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002131 }
2132 }
2133
Michael Han52b501c2012-11-28 23:17:40 +00002134 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2135 // to a friend declaration, that declaration shall be a definition.
2136 if (DeclaratorInfo.isFunctionDeclarator() &&
2137 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2138 // Diagnose attributes that appear before decl specifier:
2139 // [[]] friend int foo();
2140 ProhibitAttributes(FnAttrs);
2141 }
2142
Douglas Gregor45fa5602011-11-07 20:56:01 +00002143 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002144 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002145 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002146 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002147 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002148
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002149 // Consume the optional ';'
2150 if (Tok.is(tok::semi))
2151 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002152 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002153 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002154
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002155 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002156 Diag(DeclaratorInfo.getIdentifierLoc(),
2157 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002158
Richard Smith6f9a4452012-11-15 22:54:20 +00002159 // Recover by treating the 'typedef' as spurious.
2160 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002161 }
2162
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002163 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002164 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002165 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002166
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002167 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2168 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2169 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002170 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002171 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002172 }
2173 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002174
2175 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002176 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002177 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002178
Chris Lattner682bf922009-03-29 16:50:03 +00002179 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002180 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002181 }
2182
2183 // member-declarator-list:
2184 // member-declarator
2185 // member-declarator-list ',' member-declarator
2186
Chris Lattner5f9e2722011-07-23 10:55:15 +00002187 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002188 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002189 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002190
2191 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002192 // member-declarator:
2193 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002194 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002195 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002196 if (Tok.is(tok::colon)) {
2197 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002198 BitfieldSize = ParseConstantExpression();
2199 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002200 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002201 }
Mike Stump1eb44332009-09-09 15:08:12 +00002202
Chris Lattnere6563252010-06-13 05:34:18 +00002203 // If a simple-asm-expr is present, parse it.
2204 if (Tok.is(tok::kw_asm)) {
2205 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002206 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002207 if (AsmLabel.isInvalid())
2208 SkipUntil(tok::comma, true, true);
2209
2210 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2211 DeclaratorInfo.SetRangeEnd(Loc);
2212 }
2213
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002214 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002215 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002216
Richard Smith7a614d82011-06-11 17:19:42 +00002217 // FIXME: When g++ adds support for this, we'll need to check whether it
2218 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002219 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002220
Richard Smithca523302012-06-10 03:12:00 +00002221 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002222 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002223 if (BitfieldSize.get()) {
2224 Diag(Tok, diag::err_bitfield_member_init);
2225 SkipUntil(tok::comma, true, true);
2226 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002227 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002228 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2229 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002230 != DeclSpec::SCS_typedef)
2231 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002232 }
2233 }
2234
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002235 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002236 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002237 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002238
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002239 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002240 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002241 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2242 // to a friend declaration, that declaration shall be a definition.
2243 //
2244 // Diagnose attributes appear after friend member function declarator:
2245 // foo [[]] ();
2246 SmallVector<SourceRange, 4> Ranges;
2247 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2248 if (!Ranges.empty()) {
Craig Topper09d19ef2013-07-04 03:08:24 +00002249 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
Michael Han52b501c2012-11-28 23:17:40 +00002250 E = Ranges.end(); I != E; ++I) {
2251 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2252 << *I;
2253 }
2254 }
2255
John McCallbbbcdd92009-09-11 21:02:39 +00002256 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002257 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002258 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002259 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002260 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002261 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002262 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002263 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002264 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002265 if (AccessAttrs)
2266 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2267 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002268 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002269
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002270 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002271 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2272 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2273 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002274 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002275 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002276 }
2277 LateParsedAttrs.clear();
2278
Douglas Gregor147545d2011-10-10 14:49:18 +00002279 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002280 if (HasInClassInit != ICIS_NoInit &&
2281 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2282 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002283 // The initializer was deferred; parse it and cache the tokens.
Richard Smith80ad52f2013-01-02 11:42:31 +00002284 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00002285 diag::warn_cxx98_compat_nonstatic_member_init :
2286 diag::ext_nonstatic_member_init);
2287
Richard Smith7a614d82011-06-11 17:19:42 +00002288 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002289 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2290 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002291 //
2292 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002293 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002294 Diag(Tok, diag::err_incomplete_array_member_init);
2295 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002296 if (ThisDecl)
2297 // Avoid later warnings about a class member of incomplete type.
2298 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002299 } else
2300 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002301 } else if (HasInitializer) {
2302 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002303 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002304 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002305 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2306
Douglas Gregor147545d2011-10-10 14:49:18 +00002307 if (Init.isInvalid())
2308 SkipUntil(tok::comma, true, true);
2309 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002310 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smitha2c36462013-04-26 16:15:35 +00002311 DS.containsPlaceholderType());
Douglas Gregor147545d2011-10-10 14:49:18 +00002312 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2313 // No initializer.
Richard Smitha2c36462013-04-26 16:15:35 +00002314 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
Richard Smith7a614d82011-06-11 17:19:42 +00002315 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002316
2317 if (ThisDecl) {
2318 Actions.FinalizeDeclaration(ThisDecl);
2319 DeclsInGroup.push_back(ThisDecl);
2320 }
2321
Richard Smithe5310012012-04-29 07:31:09 +00002322 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002323 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2324 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002325 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002326 }
2327
2328 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002329
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002330 // If we don't have a comma, it is either the end of the list (a ';')
2331 // or an error, bail out.
2332 if (Tok.isNot(tok::comma))
2333 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002334
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002335 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002336 SourceLocation CommaLoc = ConsumeToken();
2337
2338 if (Tok.isAtStartOfLine() &&
2339 !MightBeDeclarator(Declarator::MemberContext)) {
2340 // This comma was followed by a line-break and something which can't be
2341 // the start of a declarator. The comma was probably a typo for a
2342 // semicolon.
2343 Diag(CommaLoc, diag::err_expected_semi_declaration)
2344 << FixItHint::CreateReplacement(CommaLoc, ";");
2345 ExpectSemi = false;
2346 break;
2347 }
Mike Stump1eb44332009-09-09 15:08:12 +00002348
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002349 // Parse the next declarator.
2350 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002351 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002352 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002353 Init = true;
2354 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002355 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002356
Bill Wendlingad017fa2012-12-20 19:22:21 +00002357 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002358 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002359
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002360 if (Tok.isNot(tok::colon))
2361 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002362 }
2363
Richard Smith1c94c162012-01-09 22:31:44 +00002364 if (ExpectSemi &&
2365 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002366 // Skip to end of block or statement.
2367 SkipUntil(tok::r_brace, true, true);
2368 // If we stopped at a ';', eat it.
2369 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002370 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002371 }
2372
Rafael Espindola4549d7f2013-07-09 12:05:01 +00002373 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002374}
2375
Richard Smith7a614d82011-06-11 17:19:42 +00002376/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2377/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2378/// function definition. The location of the '=', if any, will be placed in
2379/// EqualLoc.
2380///
2381/// pure-specifier:
2382/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002383///
Richard Smith7a614d82011-06-11 17:19:42 +00002384/// brace-or-equal-initializer:
2385/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002386/// braced-init-list
2387///
Richard Smith7a614d82011-06-11 17:19:42 +00002388/// initializer-clause:
2389/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002390/// braced-init-list
2391///
Richard Smith7a614d82011-06-11 17:19:42 +00002392/// defaulted/deleted function-definition:
2393/// '=' 'default'
2394/// '=' 'delete'
2395///
2396/// Prior to C++0x, the assignment-expression in an initializer-clause must
2397/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002398ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002399 SourceLocation &EqualLoc) {
2400 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2401 && "Data member initializer not starting with '=' or '{'");
2402
Douglas Gregor552e2992012-02-21 02:22:07 +00002403 EnterExpressionEvaluationContext Context(Actions,
2404 Sema::PotentiallyEvaluated,
2405 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002406 if (Tok.is(tok::equal)) {
2407 EqualLoc = ConsumeToken();
2408 if (Tok.is(tok::kw_delete)) {
2409 // In principle, an initializer of '= delete p;' is legal, but it will
2410 // never type-check. It's better to diagnose it as an ill-formed expression
2411 // than as an ill-formed deleted non-function member.
2412 // An initializer of '= delete p, foo' will never be parsed, because
2413 // a top-level comma always ends the initializer expression.
2414 const Token &Next = NextToken();
2415 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2416 Next.is(tok::eof)) {
2417 if (IsFunction)
2418 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2419 << 1 /* delete */;
2420 else
2421 Diag(ConsumeToken(), diag::err_deleted_non_function);
2422 return ExprResult();
2423 }
2424 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002425 if (IsFunction)
2426 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2427 << 0 /* default */;
2428 else
2429 Diag(ConsumeToken(), diag::err_default_special_members);
2430 return ExprResult();
2431 }
2432
Sebastian Redl33deb352012-02-22 10:50:08 +00002433 }
2434 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002435}
2436
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002437/// ParseCXXMemberSpecification - Parse the class definition.
2438///
2439/// member-specification:
2440/// member-declaration member-specification[opt]
2441/// access-specifier ':' member-specification[opt]
2442///
Joao Matos17d35c32012-08-31 22:18:20 +00002443void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002444 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002445 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002446 unsigned TagType, Decl *TagDecl) {
2447 assert((TagType == DeclSpec::TST_struct ||
2448 TagType == DeclSpec::TST_interface ||
2449 TagType == DeclSpec::TST_union ||
2450 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2451
John McCallf312b1e2010-08-26 23:41:50 +00002452 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2453 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002454
Douglas Gregor26997fd2010-01-16 20:52:59 +00002455 // Determine whether this is a non-nested class. Note that local
2456 // classes are *not* considered to be nested classes.
2457 bool NonNestedClass = true;
2458 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002459 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002460 if (S->isClassScope()) {
2461 // We're inside a class scope, so this is a nested class.
2462 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002463
2464 // The Microsoft extension __interface does not permit nested classes.
2465 if (getCurrentClass().IsInterface) {
2466 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2467 << /*ErrorType=*/6
2468 << (isa<NamedDecl>(TagDecl)
2469 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2470 : "<anonymous>");
2471 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002472 break;
2473 }
2474
2475 if ((S->getFlags() & Scope::FnScope)) {
2476 // If we're in a function or function template declared in the
2477 // body of a class, then this is a local class rather than a
2478 // nested class.
2479 const Scope *Parent = S->getParent();
2480 if (Parent->isTemplateParamScope())
2481 Parent = Parent->getParent();
2482 if (Parent->isClassScope())
2483 break;
2484 }
2485 }
2486 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002487
2488 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002489 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002490
Douglas Gregor6569d682009-05-27 23:11:45 +00002491 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002492 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2493 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002494
Douglas Gregorddc29e12009-02-06 22:42:48 +00002495 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002496 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002497
Anders Carlssonb184a182011-03-25 14:46:08 +00002498 SourceLocation FinalLoc;
2499
2500 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002501 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00002502 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002503 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002504
John McCalle402e722012-09-25 07:32:39 +00002505 if (TagType == DeclSpec::TST_interface) {
2506 Diag(FinalLoc, diag::err_override_control_interface)
2507 << "final";
2508 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00002509 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00002510 diag::warn_cxx98_compat_override_control_keyword :
2511 diag::ext_override_control_keyword) << "final";
2512 }
Michael Han2e397132012-11-26 22:54:45 +00002513
Michael Han07fc1ba2013-01-07 16:57:11 +00002514 // Parse any C++11 attributes after 'final' keyword.
2515 // These attributes are not allowed to appear here,
2516 // and the only possible place for them to appertain
2517 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002518 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonb184a182011-03-25 14:46:08 +00002519 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002520
John McCallbd0dfa52009-12-19 21:48:58 +00002521 if (Tok.is(tok::colon)) {
2522 ParseBaseClause(TagDecl);
2523
2524 if (!Tok.is(tok::l_brace)) {
2525 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002526
2527 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002528 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002529 return;
2530 }
2531 }
2532
2533 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002534 BalancedDelimiterTracker T(*this, tok::l_brace);
2535 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002536
John McCall42a4f662010-05-28 08:11:17 +00002537 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002538 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002539 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002540
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002541 // C++ 11p3: Members of a class defined with the keyword class are private
2542 // by default. Members of a class defined with the keywords struct or union
2543 // are public by default.
2544 AccessSpecifier CurAS;
2545 if (TagType == DeclSpec::TST_class)
2546 CurAS = AS_private;
2547 else
2548 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002549 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002550
Douglas Gregor07976d22010-06-21 22:31:09 +00002551 if (TagDecl) {
2552 // While we still have something to read, read the member-declarations.
2553 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2554 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002555
David Blaikie4e4d0842012-03-11 07:00:24 +00002556 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002557 Tok.is(tok::kw___if_not_exists))) {
2558 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2559 continue;
2560 }
2561
Douglas Gregor07976d22010-06-21 22:31:09 +00002562 // Check for extraneous top-level semicolon.
2563 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002564 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002565 continue;
2566 }
2567
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002568 if (Tok.is(tok::annot_pragma_vis)) {
2569 HandlePragmaVisibility();
2570 continue;
2571 }
2572
2573 if (Tok.is(tok::annot_pragma_pack)) {
2574 HandlePragmaPack();
2575 continue;
2576 }
2577
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002578 if (Tok.is(tok::annot_pragma_align)) {
2579 HandlePragmaAlign();
2580 continue;
2581 }
2582
Alexey Bataevc6400582013-03-22 06:34:35 +00002583 if (Tok.is(tok::annot_pragma_openmp)) {
2584 ParseOpenMPDeclarativeDirective();
2585 continue;
2586 }
2587
Douglas Gregor07976d22010-06-21 22:31:09 +00002588 AccessSpecifier AS = getAccessSpecifierIfPresent();
2589 if (AS != AS_none) {
2590 // Current token is a C++ access specifier.
2591 CurAS = AS;
2592 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002593 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002594 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002595 AccessAttrs.clear();
2596 MaybeParseGNUAttributes(AccessAttrs);
2597
David Blaikie13f8daf2011-10-13 06:08:43 +00002598 SourceLocation EndLoc;
2599 if (Tok.is(tok::colon)) {
2600 EndLoc = Tok.getLocation();
2601 ConsumeToken();
2602 } else if (Tok.is(tok::semi)) {
2603 EndLoc = Tok.getLocation();
2604 ConsumeToken();
2605 Diag(EndLoc, diag::err_expected_colon)
2606 << FixItHint::CreateReplacement(EndLoc, ":");
2607 } else {
2608 EndLoc = ASLoc.getLocWithOffset(TokLength);
2609 Diag(EndLoc, diag::err_expected_colon)
2610 << FixItHint::CreateInsertion(EndLoc, ":");
2611 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002612
John McCalle402e722012-09-25 07:32:39 +00002613 // The Microsoft extension __interface does not permit non-public
2614 // access specifiers.
2615 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2616 Diag(ASLoc, diag::err_access_specifier_interface)
2617 << (CurAS == AS_protected);
2618 }
2619
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002620 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2621 AccessAttrs.getList())) {
2622 // found another attribute than only annotations
2623 AccessAttrs.clear();
2624 }
2625
Douglas Gregor07976d22010-06-21 22:31:09 +00002626 continue;
2627 }
2628
2629 // FIXME: Make sure we don't have a template here.
2630
2631 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002632 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002633 }
2634
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002635 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002636 } else {
2637 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002638 }
Mike Stump1eb44332009-09-09 15:08:12 +00002639
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002640 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002641 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002642 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002643
John McCall42a4f662010-05-28 08:11:17 +00002644 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002645 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002646 T.getOpenLocation(),
2647 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002648 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002649
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002650 // C++11 [class.mem]p2:
2651 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002652 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002653 // brace-or-equal-initializers for non-static data members (including such
2654 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002655 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002656 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002657 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002658 // declarations and the lexed inline method definitions, along with any
2659 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002660 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002661 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002662 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002663
2664 // We've finished with all pending member declarations.
2665 Actions.ActOnFinishCXXMemberDecls();
2666
Richard Smith7a614d82011-06-11 17:19:42 +00002667 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002668 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002669 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002670 }
2671
John McCall42a4f662010-05-28 08:11:17 +00002672 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002673 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2674 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002675
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002676 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002677 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002678 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002679}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002680
2681/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2682/// which explicitly initializes the members or base classes of a
2683/// class (C++ [class.base.init]). For example, the three initializers
2684/// after the ':' in the Derived constructor below:
2685///
2686/// @code
2687/// class Base { };
2688/// class Derived : Base {
2689/// int x;
2690/// float f;
2691/// public:
2692/// Derived(float f) : Base(), x(17), f(f) { }
2693/// };
2694/// @endcode
2695///
Mike Stump1eb44332009-09-09 15:08:12 +00002696/// [C++] ctor-initializer:
2697/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002698///
Mike Stump1eb44332009-09-09 15:08:12 +00002699/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002700/// mem-initializer ...[opt]
2701/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002702void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002703 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2704
John Wiegley28bbe4b2011-04-28 01:08:34 +00002705 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2706 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002707 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002708
Chris Lattner5f9e2722011-07-23 10:55:15 +00002709 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002710 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002711
Douglas Gregor7ad83902008-11-05 04:29:56 +00002712 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002713 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko572cf582013-06-23 22:58:02 +00002714 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2715 MemInitializers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002716 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002717 } else {
2718 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2719 if (!MemInit.isInvalid())
2720 MemInitializers.push_back(MemInit.get());
2721 else
2722 AnyErrors = true;
2723 }
2724
Douglas Gregor7ad83902008-11-05 04:29:56 +00002725 if (Tok.is(tok::comma))
2726 ConsumeToken();
2727 else if (Tok.is(tok::l_brace))
2728 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002729 // If the next token looks like a base or member initializer, assume that
2730 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002731 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2732 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2733 Diag(Loc, diag::err_ctor_init_missing_comma)
2734 << FixItHint::CreateInsertion(Loc, ", ");
2735 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002736 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002737 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002738 SkipUntil(tok::l_brace, true, true);
2739 break;
2740 }
2741 } while (true);
2742
David Blaikie93c86172013-01-17 05:26:25 +00002743 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002744 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002745}
2746
2747/// ParseMemInitializer - Parse a C++ member initializer, which is
2748/// part of a constructor initializer that explicitly initializes one
2749/// member or base class (C++ [class.base.init]). See
2750/// ParseConstructorInitializer for an example.
2751///
2752/// [C++] mem-initializer:
2753/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002754/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002755///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002756/// [C++] mem-initializer-id:
2757/// '::'[opt] nested-name-specifier[opt] class-name
2758/// identifier
John McCalld226f652010-08-21 09:40:31 +00002759Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002760 // parse '::'[opt] nested-name-specifier[opt]
2761 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002762 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002763 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002764 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002765 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002766 if (TemplateId->Kind == TNK_Type_template ||
2767 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002768 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002769 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002770 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002771 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002772 }
David Blaikief2116622012-01-24 06:03:59 +00002773 // Uses of decltype will already have been converted to annot_decltype by
2774 // ParseOptionalCXXScopeSpecifier at this point.
2775 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2776 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002777 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002778 return true;
2779 }
Mike Stump1eb44332009-09-09 15:08:12 +00002780
David Blaikief2116622012-01-24 06:03:59 +00002781 IdentifierInfo *II = 0;
2782 DeclSpec DS(AttrFactory);
2783 SourceLocation IdLoc = Tok.getLocation();
2784 if (Tok.is(tok::annot_decltype)) {
2785 // Get the decltype expression, if there is one.
2786 ParseDecltypeSpecifier(DS);
2787 } else {
2788 if (Tok.is(tok::identifier))
2789 // Get the identifier. This may be a member name or a class name,
2790 // but we'll let the semantic analysis determine which it is.
2791 II = Tok.getIdentifierInfo();
2792 ConsumeToken();
2793 }
2794
Douglas Gregor7ad83902008-11-05 04:29:56 +00002795
2796 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002797 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002798 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2799
Sebastian Redl6df65482011-09-24 17:48:25 +00002800 ExprResult InitList = ParseBraceInitializer();
2801 if (InitList.isInvalid())
2802 return true;
2803
2804 SourceLocation EllipsisLoc;
2805 if (Tok.is(tok::ellipsis))
2806 EllipsisLoc = ConsumeToken();
2807
2808 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002809 TemplateTypeTy, DS, IdLoc,
2810 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002811 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002812 BalancedDelimiterTracker T(*this, tok::l_paren);
2813 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002814
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002815 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002816 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002817 CommaLocsTy CommaLocs;
2818 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2819 SkipUntil(tok::r_paren);
2820 return true;
2821 }
2822
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002823 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002824
2825 SourceLocation EllipsisLoc;
2826 if (Tok.is(tok::ellipsis))
2827 EllipsisLoc = ConsumeToken();
2828
2829 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002830 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenkoa36bbac2013-05-09 23:51:52 +00002831 T.getOpenLocation(), ArgExprs,
2832 T.getCloseLocation(), EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002833 }
2834
Richard Smith80ad52f2013-01-02 11:42:31 +00002835 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002836 : diag::err_expected_lparen);
2837 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002838}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002839
Sebastian Redl7acafd02011-03-05 14:45:16 +00002840/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002841///
Douglas Gregora4745612008-12-01 18:00:20 +00002842/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002843/// dynamic-exception-specification
2844/// noexcept-specification
2845///
2846/// noexcept-specification:
2847/// 'noexcept'
2848/// 'noexcept' '(' constant-expression ')'
2849ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002850Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002851 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002852 SmallVectorImpl<ParsedType> &DynamicExceptions,
2853 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002854 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002855 ExceptionSpecificationType Result = EST_None;
2856
2857 // See if there's a dynamic specification.
2858 if (Tok.is(tok::kw_throw)) {
2859 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2860 DynamicExceptions,
2861 DynamicExceptionRanges);
2862 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2863 "Produced different number of exception types and ranges.");
2864 }
2865
2866 // If there's no noexcept specification, we're done.
2867 if (Tok.isNot(tok::kw_noexcept))
2868 return Result;
2869
Richard Smith841804b2011-10-17 23:06:20 +00002870 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2871
Sebastian Redl7acafd02011-03-05 14:45:16 +00002872 // If we already had a dynamic specification, parse the noexcept for,
2873 // recovery, but emit a diagnostic and don't store the results.
2874 SourceRange NoexceptRange;
2875 ExceptionSpecificationType NoexceptType = EST_None;
2876
2877 SourceLocation KeywordLoc = ConsumeToken();
2878 if (Tok.is(tok::l_paren)) {
2879 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002880 BalancedDelimiterTracker T(*this, tok::l_paren);
2881 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002882 NoexceptType = EST_ComputedNoexcept;
2883 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002884 // The argument must be contextually convertible to bool. We use
2885 // ActOnBooleanCondition for this purpose.
2886 if (!NoexceptExpr.isInvalid())
2887 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2888 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002889 T.consumeClose();
2890 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002891 } else {
2892 // There is no argument.
2893 NoexceptType = EST_BasicNoexcept;
2894 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2895 }
2896
2897 if (Result == EST_None) {
2898 SpecificationRange = NoexceptRange;
2899 Result = NoexceptType;
2900
2901 // If there's a dynamic specification after a noexcept specification,
2902 // parse that and ignore the results.
2903 if (Tok.is(tok::kw_throw)) {
2904 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2905 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2906 DynamicExceptionRanges);
2907 }
2908 } else {
2909 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2910 }
2911
2912 return Result;
2913}
2914
Richard Smith79f4bb72013-06-13 02:02:51 +00002915static void diagnoseDynamicExceptionSpecification(
2916 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2917 if (P.getLangOpts().CPlusPlus11) {
2918 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2919 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2920 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2921 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2922 }
2923}
2924
Sebastian Redl7acafd02011-03-05 14:45:16 +00002925/// ParseDynamicExceptionSpecification - Parse a C++
2926/// dynamic-exception-specification (C++ [except.spec]).
2927///
2928/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002929/// 'throw' '(' type-id-list [opt] ')'
2930/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002931///
Douglas Gregora4745612008-12-01 18:00:20 +00002932/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002933/// type-id ... [opt]
2934/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002935///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002936ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2937 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002938 SmallVectorImpl<ParsedType> &Exceptions,
2939 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002940 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002941
Sebastian Redl7acafd02011-03-05 14:45:16 +00002942 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002943 BalancedDelimiterTracker T(*this, tok::l_paren);
2944 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002945 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2946 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002947 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002948 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002949
Douglas Gregora4745612008-12-01 18:00:20 +00002950 // Parse throw(...), a Microsoft extension that means "this function
2951 // can throw anything".
2952 if (Tok.is(tok::ellipsis)) {
2953 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002954 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002955 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002956 T.consumeClose();
2957 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00002958 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redl60618fa2011-03-12 11:50:43 +00002959 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002960 }
2961
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002962 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002963 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002964 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002965 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002966
Douglas Gregora04426c2010-12-20 23:57:46 +00002967 if (Tok.is(tok::ellipsis)) {
2968 // C++0x [temp.variadic]p5:
2969 // - In a dynamic-exception-specification (15.4); the pattern is a
2970 // type-id.
2971 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002972 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002973 if (!Res.isInvalid())
2974 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2975 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002976
Sebastian Redlef65f062009-05-29 18:02:33 +00002977 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002978 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002979 Ranges.push_back(Range);
2980 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002981
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002982 if (Tok.is(tok::comma))
2983 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002984 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002985 break;
2986 }
2987
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002988 T.consumeClose();
2989 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00002990 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
2991 Exceptions.empty());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002992 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002993}
Douglas Gregor6569d682009-05-27 23:11:45 +00002994
Douglas Gregordab60ad2010-10-01 18:44:50 +00002995/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2996/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002997TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002998 assert(Tok.is(tok::arrow) && "expected arrow");
2999
3000 ConsumeToken();
3001
Richard Smith7796eb52012-03-12 08:56:40 +00003002 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00003003}
3004
Douglas Gregor6569d682009-05-27 23:11:45 +00003005/// \brief We have just started parsing the definition of a new class,
3006/// so push that class onto our stack of classes that is currently
3007/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00003008Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00003009Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3010 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00003011 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00003012 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00003013 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00003014 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00003015}
3016
3017/// \brief Deallocate the given parsed class and all of its nested
3018/// classes.
3019void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00003020 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3021 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00003022 delete Class;
3023}
3024
3025/// \brief Pop the top class of the stack of classes that are
3026/// currently being parsed.
3027///
3028/// This routine should be called when we have finished parsing the
3029/// definition of a class, but have not yet popped the Scope
3030/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00003031void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003032 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00003033
John McCalleee1d542011-02-14 07:13:47 +00003034 Actions.PopParsingClass(state);
3035
Douglas Gregor6569d682009-05-27 23:11:45 +00003036 ParsingClass *Victim = ClassStack.top();
3037 ClassStack.pop();
3038 if (Victim->TopLevelClass) {
3039 // Deallocate all of the nested classes of this class,
3040 // recursively: we don't need to keep any of this information.
3041 DeallocateParsedClasses(Victim);
3042 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003043 }
Douglas Gregor6569d682009-05-27 23:11:45 +00003044 assert(!ClassStack.empty() && "Missing top-level class?");
3045
Douglas Gregord54eb442010-10-12 16:25:54 +00003046 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003047 // The victim is a nested class, but we will not need to perform
3048 // any processing after the definition of this class since it has
3049 // no members whose handling was delayed. Therefore, we can just
3050 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00003051 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00003052 return;
3053 }
3054
3055 // This nested class has some members that will need to be processed
3056 // after the top-level class is completely defined. Therefore, add
3057 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003058 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00003059 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00003060 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00003061}
Sean Huntbbd37c62009-11-21 08:43:09 +00003062
Richard Smithc56298d2012-04-10 03:25:07 +00003063/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3064///
3065/// \return the parsed identifier on success, and 0 if the next token is not an
3066/// attribute-token.
3067///
3068/// C++11 [dcl.attr.grammar]p3:
3069/// If a keyword or an alternative token that satisfies the syntactic
3070/// requirements of an identifier is contained in an attribute-token,
3071/// it is considered an identifier.
3072IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3073 switch (Tok.getKind()) {
3074 default:
3075 // Identifiers and keywords have identifier info attached.
3076 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3077 Loc = ConsumeToken();
3078 return II;
3079 }
3080 return 0;
3081
3082 case tok::ampamp: // 'and'
3083 case tok::pipe: // 'bitor'
3084 case tok::pipepipe: // 'or'
3085 case tok::caret: // 'xor'
3086 case tok::tilde: // 'compl'
3087 case tok::amp: // 'bitand'
3088 case tok::ampequal: // 'and_eq'
3089 case tok::pipeequal: // 'or_eq'
3090 case tok::caretequal: // 'xor_eq'
3091 case tok::exclaim: // 'not'
3092 case tok::exclaimequal: // 'not_eq'
3093 // Alternative tokens do not have identifier info, but their spelling
3094 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003095 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003096 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003097 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003098 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003099 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003100 }
3101 return 0;
3102 }
3103}
3104
Michael Han6880f492012-10-03 01:56:22 +00003105static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3106 IdentifierInfo *ScopeName) {
3107 switch (AttributeList::getKind(AttrName, ScopeName,
3108 AttributeList::AS_CXX11)) {
3109 case AttributeList::AT_CarriesDependency:
3110 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003111 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003112 return true;
3113 }
3114
3115 default:
3116 return false;
3117 }
3118}
3119
Richard Smithc56298d2012-04-10 03:25:07 +00003120/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003121/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003122///
Richard Smith6ee326a2012-04-10 01:32:12 +00003123/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003124/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003125/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003126///
Richard Smith6ee326a2012-04-10 01:32:12 +00003127/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003128/// attribute[opt]
3129/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003130/// attribute '...'
3131/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003132///
Richard Smith6ee326a2012-04-10 01:32:12 +00003133/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003134/// attribute-token attribute-argument-clause[opt]
3135///
Richard Smith6ee326a2012-04-10 01:32:12 +00003136/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003137/// identifier
3138/// attribute-scoped-token
3139///
Richard Smith6ee326a2012-04-10 01:32:12 +00003140/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003141/// attribute-namespace '::' identifier
3142///
Richard Smith6ee326a2012-04-10 01:32:12 +00003143/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003144/// identifier
3145///
Richard Smith6ee326a2012-04-10 01:32:12 +00003146/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003147/// '(' balanced-token-seq ')'
3148///
Richard Smith6ee326a2012-04-10 01:32:12 +00003149/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003150/// balanced-token
3151/// balanced-token-seq balanced-token
3152///
Richard Smith6ee326a2012-04-10 01:32:12 +00003153/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003154/// '(' balanced-token-seq ')'
3155/// '[' balanced-token-seq ']'
3156/// '{' balanced-token-seq '}'
3157/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003158void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003159 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003160 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003161 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003162 ParseAlignmentSpecifier(attrs, endLoc);
3163 return;
3164 }
3165
Sean Huntbbd37c62009-11-21 08:43:09 +00003166 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003167 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003168
Richard Smith41be6732011-10-14 20:48:27 +00003169 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3170
Sean Huntbbd37c62009-11-21 08:43:09 +00003171 ConsumeBracket();
3172 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003173
Richard Smithcd8ab512013-01-17 01:30:42 +00003174 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3175
Richard Smithc56298d2012-04-10 03:25:07 +00003176 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003177 // attribute not present
3178 if (Tok.is(tok::comma)) {
3179 ConsumeToken();
3180 continue;
3181 }
3182
Richard Smithc56298d2012-04-10 03:25:07 +00003183 SourceLocation ScopeLoc, AttrLoc;
3184 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3185
3186 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3187 if (!AttrName)
3188 // Break out to the "expected ']'" diagnostic.
3189 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003190
Sean Huntbbd37c62009-11-21 08:43:09 +00003191 // scoped attribute
3192 if (Tok.is(tok::coloncolon)) {
3193 ConsumeToken();
3194
Richard Smithc56298d2012-04-10 03:25:07 +00003195 ScopeName = AttrName;
3196 ScopeLoc = AttrLoc;
3197
3198 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3199 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003200 Diag(Tok.getLocation(), diag::err_expected_ident);
3201 SkipUntil(tok::r_square, tok::comma, true, true);
3202 continue;
3203 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003204 }
3205
Michael Han6880f492012-10-03 01:56:22 +00003206 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003207 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003208
Richard Smithcd8ab512013-01-17 01:30:42 +00003209 if (StandardAttr &&
3210 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3211 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3212 << AttrName << SourceRange(SeenAttrs[AttrName]);
3213
Michael Han6880f492012-10-03 01:56:22 +00003214 // Parse attribute arguments
3215 if (Tok.is(tok::l_paren)) {
3216 if (ScopeName && ScopeName->getName() == "gnu") {
3217 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3218 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3219 AttrParsed = true;
3220 } else {
3221 if (StandardAttr)
3222 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3223 << AttrName->getName();
3224
3225 // FIXME: handle other formats of c++11 attribute arguments
3226 ConsumeParen();
3227 SkipUntil(tok::r_paren, false);
3228 }
3229 }
3230
3231 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003232 attrs.addNew(AttrName,
3233 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3234 AttrLoc),
3235 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003236 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003237
Richard Smithc56298d2012-04-10 03:25:07 +00003238 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003239 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003240
3241 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3242 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003243 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003244 }
3245
3246 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3247 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003248 if (endLoc)
3249 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003250 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3251 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003252}
Sean Huntbbd37c62009-11-21 08:43:09 +00003253
Sean Hunt2edf0a22012-06-23 05:07:58 +00003254/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003255///
3256/// attribute-specifier-seq:
3257/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003258void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003259 SourceLocation *endLoc) {
Richard Smith672edb02013-02-22 09:15:49 +00003260 assert(getLangOpts().CPlusPlus11);
3261
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003262 SourceLocation StartLoc = Tok.getLocation(), Loc;
3263 if (!endLoc)
3264 endLoc = &Loc;
3265
Douglas Gregor8828ee72011-10-07 20:35:25 +00003266 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003267 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003268 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003269
3270 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003271}
3272
Francois Pichet334d47e2010-10-11 12:59:39 +00003273/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3274///
3275/// [MS] ms-attribute:
3276/// '[' token-seq ']'
3277///
3278/// [MS] ms-attribute-seq:
3279/// ms-attribute[opt]
3280/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003281void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3282 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003283 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3284
3285 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003286 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003287 ConsumeBracket();
3288 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003289 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003290 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3291 }
3292}
Francois Pichet563a6452011-05-25 10:19:49 +00003293
3294void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3295 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003296 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003297 if (ParseMicrosoftIfExistsCondition(Result))
3298 return;
3299
Douglas Gregor3896fc52011-10-24 22:31:10 +00003300 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3301 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003302 Diag(Tok, diag::err_expected_lbrace);
3303 return;
3304 }
Francois Pichet563a6452011-05-25 10:19:49 +00003305
Douglas Gregor3896fc52011-10-24 22:31:10 +00003306 switch (Result.Behavior) {
3307 case IEB_Parse:
3308 // Parse the declarations below.
3309 break;
3310
3311 case IEB_Dependent:
3312 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3313 << Result.IsIfExists;
3314 // Fall through to skip.
3315
3316 case IEB_Skip:
3317 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003318 return;
3319 }
3320
Douglas Gregor3896fc52011-10-24 22:31:10 +00003321 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003322 // __if_exists, __if_not_exists can nest.
3323 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3324 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3325 continue;
3326 }
3327
3328 // Check for extraneous top-level semicolon.
3329 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003330 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003331 continue;
3332 }
3333
3334 AccessSpecifier AS = getAccessSpecifierIfPresent();
3335 if (AS != AS_none) {
3336 // Current token is a C++ access specifier.
3337 CurAS = AS;
3338 SourceLocation ASLoc = Tok.getLocation();
3339 ConsumeToken();
3340 if (Tok.is(tok::colon))
3341 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3342 else
3343 Diag(Tok, diag::err_expected_colon);
3344 ConsumeToken();
3345 continue;
3346 }
3347
3348 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003349 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003350 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003351
3352 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003353}