blob: 9ba1314171229e27439be44d6d3feba3ea9ebac8 [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor423984d2008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Basic/OperatorKinds.h"
Chris Lattner60f36222009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
John McCalldb632ac2012-09-25 07:32:39 +000023#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Chris Lattnera5235172007-08-25 06:57:03 +000025using namespace clang;
26
27/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-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 Lattnera5235172007-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 Redl67667942010-08-27 23:12:46 +000036/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000037///
38/// named-namespace-definition:
39/// original-namespace-definition
40/// extension-namespace-definition
41///
42/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000043/// 'inline'[opt] 'namespace' identifier attributes[opt]
44/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000045///
46/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' original-namespace-name
48/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000049///
Chris Lattnera5235172007-08-25 06:57:03 +000050/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
51/// 'namespace' identifier '=' qualified-namespace-specifier ';'
52///
John McCall48871652010-08-21 09:40:31 +000053Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000054 SourceLocation &DeclEnd,
55 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000056 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000057 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000058 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000059
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000060 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000061 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000062 cutOffParsing();
63 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000064 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000065
Chris Lattnera5235172007-08-25 06:57:03 +000066 SourceLocation IdentLoc;
67 IdentifierInfo *Ident = 0;
Richard Trieu61384cb2011-05-26 20:11:09 +000068 std::vector<SourceLocation> ExtraIdentLoc;
69 std::vector<IdentifierInfo*> ExtraIdent;
70 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000071
72 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000073
Chris Lattner76c72282007-10-09 17:33:22 +000074 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000075 Ident = Tok.getIdentifierInfo();
76 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-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 Lattnera5235172007-08-25 06:57:03 +000082 }
Mike Stump11289f42009-09-09 15:08:12 +000083
Chris Lattnera5235172007-08-25 06:57:03 +000084 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000085 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000086 if (Tok.is(tok::kw___attribute)) {
87 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000088 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000089 }
Mike Stump11289f42009-09-09 15:08:12 +000090
Douglas Gregor6b6bba42009-06-17 19:49:00 +000091 if (Tok.is(tok::equal)) {
Nico Weber729f1e22012-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 McCall53fa7142010-12-24 02:08:15 +000098 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +000099 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000100 if (InlineLoc.isValid())
101 Diag(InlineLoc, diag::err_inline_namespace_alias)
102 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000103 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000104 }
Mike Stump11289f42009-09-09 15:08:12 +0000105
Richard Trieu61384cb2011-05-26 20:11:09 +0000106
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000107 BalancedDelimiterTracker T(*this, tok::l_brace);
108 if (T.consumeOpen()) {
Richard Trieu61384cb2011-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 Stump11289f42009-09-09 15:08:12 +0000113 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner4de55aa2009-03-29 14:02:43 +0000114 diag::err_expected_ident_lbrace);
John McCall48871652010-08-21 09:40:31 +0000115 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000116 }
Mike Stump11289f42009-09-09 15:08:12 +0000117
Douglas Gregor0be31a22010-07-02 17:43:08 +0000118 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
119 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
120 getCurScope()->getFnParent()) {
Richard Trieu61384cb2011-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 Gregore7a8e3b2011-10-12 16:37:45 +0000125 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor05cfc292010-05-14 05:08:22 +0000126 SkipUntil(tok::r_brace, false);
John McCall48871652010-08-21 09:40:31 +0000127 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000128 }
129
Richard Trieu61384cb2011-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 Kramerf546f412011-05-26 21:32:30 +0000140 std::string NamespaceFix;
Richard Trieu61384cb2011-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 Kramerf546f412011-05-26 21:32:30 +0000146
Richard Trieu61384cb2011-05-26 20:11:09 +0000147 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000148 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000149 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000150
Richard Trieu61384cb2011-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 Redl5a5f2c72010-08-31 00:36:45 +0000159 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000160 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000161 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000162 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000163
Chris Lattner4de55aa2009-03-29 14:02:43 +0000164 // Enter a scope for the namespace.
165 ParseScope NamespaceScope(this, Scope::DeclScope);
166
John McCall48871652010-08-21 09:40:31 +0000167 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000168 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000169 IdentLoc, Ident, T.getOpenLocation(),
170 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000171
John McCallfaf5fb42010-08-26 23:41:50 +0000172 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
173 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000174
Richard Trieu61384cb2011-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 Gregore7a8e3b2011-10-12 16:37:45 +0000178 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000179
Chris Lattner4de55aa2009-03-29 14:02:43 +0000180 // Leave the namespace scope.
181 NamespaceScope.Exit();
182
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000183 DeclEnd = T.getCloseLocation();
184 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000185
186 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000187}
Chris Lattner38376f12008-01-12 07:05:38 +0000188
Richard Trieu61384cb2011-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 Trieu61384cb2011-05-26 20:11:09 +0000194 ParsedAttributes& attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000195 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-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 Smith89645bc2013-01-02 12:01:23 +0000199 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000200 MaybeParseMicrosoftAttributes(attrs);
201 ParseExternalDeclaration(attrs);
202 }
Douglas Gregore7a8e3b2011-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 Trieu61384cb2011-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 Gregore7a8e3b2011-10-12 16:37:45 +0000216 Ident[index], Tracker.getOpenLocation(),
217 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000218
219 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000220 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000221
222 NamespaceScope.Exit();
223
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000224 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000225}
226
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000227/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
228/// alias definition.
229///
John McCall48871652010-08-21 09:40:31 +0000230Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000231 SourceLocation AliasLoc,
232 IdentifierInfo *Alias,
233 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000234 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000235
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000236 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000237
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000238 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000239 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000240 cutOffParsing();
241 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000243
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000244 CXXScopeSpec SS;
245 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000246 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-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 McCall48871652010-08-21 09:40:31 +0000252 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000253 }
254
255 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000256 IdentifierInfo *Ident = Tok.getIdentifierInfo();
257 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000258
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000259 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000260 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000261 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
262 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000263
Douglas Gregor0be31a22010-07-02 17:43:08 +0000264 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000265 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000266}
267
Chris Lattner38376f12008-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 Lattner8ea64422010-11-09 20:15:55 +0000275Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000276 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000277 SmallString<8> LangBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000278 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000279 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000280 if (Invalid)
John McCall48871652010-08-21 09:40:31 +0000281 return 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000282
Richard Smithd67aea22012-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 Lattner38376f12008-01-12 07:05:38 +0000288 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000289
Douglas Gregor07665a62009-01-05 19:45:36 +0000290 ParseScope LinkageScope(this, Scope::DeclScope);
John McCall48871652010-08-21 09:40:31 +0000291 Decl *LinkageSpec
Douglas Gregor0be31a22010-07-02 17:43:08 +0000292 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000293 DS.getSourceRange().getBegin(),
Benjamin Kramerbebee842010-05-03 13:08:54 +0000294 Loc, Lang,
Abramo Bagnaraea947882011-03-08 16:41:52 +0000295 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000296 : SourceLocation());
297
John McCall084e83d2011-03-24 11:26:52 +0000298 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000299 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000300 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000301
Douglas Gregor07665a62009-01-05 19:45:36 +0000302 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-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 Bagnaraed5b6892010-07-30 16:47:02 +0000308 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000309 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000310 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000311 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000312 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000313
Douglas Gregorb65a9132010-02-07 08:38:28 +0000314 DS.abort();
315
John McCall53fa7142010-12-24 02:08:15 +0000316 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000317
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000318 BalancedDelimiterTracker T(*this, tok::l_brace);
319 T.consumeOpen();
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000320 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall084e83d2011-03-24 11:26:52 +0000321 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000322 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000323 MaybeParseMicrosoftAttributes(attrs);
324 ParseExternalDeclaration(attrs);
Chris Lattner38376f12008-01-12 07:05:38 +0000325 }
326
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000327 T.consumeClose();
Chris Lattner8ea64422010-11-09 20:15:55 +0000328 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000329 T.getCloseLocation());
Chris Lattner38376f12008-01-12 07:05:38 +0000330}
Douglas Gregor556877c2008-04-13 21:30:24 +0000331
Douglas Gregord7c4d982008-12-30 03:27:21 +0000332/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
333/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000334Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000335 const ParsedTemplateInfo &TemplateInfo,
336 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000337 ParsedAttributesWithRange &attrs,
338 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000339 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000340 ObjCDeclContextSwitch ObjCDC(*this);
341
Douglas Gregord7c4d982008-12-30 03:27:21 +0000342 // Eat 'using'.
343 SourceLocation UsingLoc = ConsumeToken();
344
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000345 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000346 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000347 cutOffParsing();
348 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000349 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000350
John McCall9b72f892010-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 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000359
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000360 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000361 }
362
Richard Smithdda56e42011-04-15 14:24:37 +0000363 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000364
365 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000366 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000367
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000368 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000369 AS_none, OwnedType);
Douglas Gregord7c4d982008-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 McCall48871652010-08-21 09:40:31 +0000382Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000383 SourceLocation UsingLoc,
384 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000385 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-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 Gregor7e90c6d2009-09-18 19:03:04 +0000391 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000392 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000393 cutOffParsing();
394 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000395 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000396
Douglas Gregord7c4d982008-12-30 03:27:21 +0000397 CXXScopeSpec SS;
398 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000399 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000400
Douglas Gregord7c4d982008-12-30 03:27:21 +0000401 IdentifierInfo *NamespcName = 0;
402 SourceLocation IdentLoc = SourceLocation();
403
404 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000405 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-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 McCall48871652010-08-21 09:40:31 +0000410 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000411 }
Mike Stump11289f42009-09-09 15:08:12 +0000412
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000413 // Parse identifier.
414 NamespcName = Tok.getIdentifierInfo();
415 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000416
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000417 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000418 bool GNUAttr = false;
419 if (Tok.is(tok::kw___attribute)) {
420 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000421 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000424 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000425 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000426 ExpectAndConsume(tok::semi,
Douglas Gregor45d6bdf2010-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 Gregord7c4d982008-12-30 03:27:21 +0000430
Douglas Gregor0be31a22010-07-02 17:43:08 +0000431 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000432 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000433}
434
Richard Smithdda56e42011-04-15 14:24:37 +0000435/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
436/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000437///
438/// using-declaration: [C++ 7.3.p3: namespace.udecl]
439/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000440/// unqualified-id
441/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000442///
Richard Smith810ad3e2013-01-29 10:02:16 +0000443/// alias-declaration: C++11 [dcl.dcl]p1
444/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000445///
John McCall48871652010-08-21 09:40:31 +0000446Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000447 const ParsedTemplateInfo &TemplateInfo,
448 SourceLocation UsingLoc,
449 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000450 AccessSpecifier AS,
451 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000452 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000453 SourceLocation TypenameLoc;
Richard Smith54ecd982013-02-20 19:22:51 +0000454 bool IsTypeName = false;
455 ParsedAttributesWithRange Attrs(AttrFactory);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000456
457 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith54ecd982013-02-20 19:22:51 +0000458 MaybeParseCXX11Attributes(Attrs);
459 ProhibitAttributes(Attrs);
460 Attrs.clear();
461 Attrs.Range = SourceRange();
Douglas Gregorfec52632009-06-20 00:51:54 +0000462
463 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000464 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000465 if (Tok.is(tok::kw_typename)) {
Richard Smith54ecd982013-02-20 19:22:51 +0000466 TypenameLoc = ConsumeToken();
Douglas Gregorfec52632009-06-20 00:51:54 +0000467 IsTypeName = true;
468 }
Douglas Gregorfec52632009-06-20 00:51:54 +0000469
470 // Parse nested-name-specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000471 IdentifierInfo *LastII = 0;
472 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
473 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
474 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000475
Douglas Gregorfec52632009-06-20 00:51:54 +0000476 // Check nested-name specifier.
477 if (SS.isInvalid()) {
478 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000479 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000480 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000481
Richard Smith7447af42013-03-26 01:15:19 +0000482 SourceLocation TemplateKWLoc;
483 UnqualifiedId Name;
484
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000485 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000486 // destructor names and allow the action module to diagnose any semantic
487 // errors.
Richard Smith7447af42013-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 Gregorfec52632009-06-20 00:51:54 +0000507 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000508 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000509 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000510
Richard Smith54ecd982013-02-20 19:22:51 +0000511 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-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 Smith54ecd982013-02-20 19:22:51 +0000517 // TODO: Can GNU attributes appear here?
Richard Smithdda56e42011-04-15 14:24:37 +0000518 ConsumeToken();
519
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000520 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000521 diag::warn_cxx98_compat_alias_declaration :
522 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000523
Richard Smith3f1b5d02011-05-05 21:57:07 +0000524 // Type alias templates cannot be specialized.
525 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000526 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
527 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-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 Smithdda56e42011-04-15 14:24:37 +0000546 // Name must be an identifier.
547 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
548 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
549 // No removal fixit: can't recover from this.
550 SkipUntil(tok::semi);
551 return 0;
552 } else if (IsTypeName)
553 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
554 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
555 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
556 else if (SS.isNotEmpty())
557 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
558 << FixItHint::CreateRemoval(SS.getRange());
559
Richard Smith3f1b5d02011-05-05 21:57:07 +0000560 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
561 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000562 Declarator::AliasDeclContext, AS, OwnedType,
563 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000564 } else {
565 // C++11 attributes are not allowed on a using-declaration, but GNU ones
566 // are.
Richard Smith54ecd982013-02-20 19:22:51 +0000567 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000568
Richard Smithdda56e42011-04-15 14:24:37 +0000569 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000570 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000571 }
Mike Stump11289f42009-09-09 15:08:12 +0000572
Douglas Gregorfec52632009-06-20 00:51:54 +0000573 // Eat ';'.
574 DeclEnd = Tok.getLocation();
575 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith54ecd982013-02-20 19:22:51 +0000576 !Attrs.empty() ? "attributes list" :
Richard Smithdda56e42011-04-15 14:24:37 +0000577 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor220f4272009-11-04 16:30:06 +0000578 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000579
John McCall9b72f892010-11-10 02:40:36 +0000580 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000581 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000582 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000583 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-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 Gregor882a61a2011-09-26 14:30:28 +0000594 // "typename" keyword is allowed for identifiers only,
595 // because it may be a type definition.
596 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
597 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
598 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
599 // Proceed parsing, but reset the IsTypeName flag.
600 IsTypeName = false;
601 }
602
Richard Smith3f1b5d02011-05-05 21:57:07 +0000603 if (IsAliasDecl) {
604 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000605 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3f1b5d02011-05-05 21:57:07 +0000606 TemplateParams ? TemplateParams->data() : 0,
607 TemplateParams ? TemplateParams->size() : 0);
608 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000609 UsingLoc, Name, Attrs.getList(),
610 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000611 }
Richard Smithdda56e42011-04-15 14:24:37 +0000612
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000613 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
Richard Smith54ecd982013-02-20 19:22:51 +0000614 Name, Attrs.getList(),
John McCall53fa7142010-12-24 02:08:15 +0000615 IsTypeName, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000616}
617
Benjamin Kramere56f3932011-12-23 17:00:35 +0000618/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000619///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000620/// [C++0x] static_assert-declaration:
621/// static_assert ( constant-expression , string-literal ) ;
622///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000623/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000624/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000625///
John McCall48871652010-08-21 09:40:31 +0000626Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000627 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
628 "Not a static_assert declaration");
629
David Blaikiebbafb8a2012-03-11 07:00:24 +0000630 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000631 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000632 if (Tok.is(tok::kw_static_assert))
633 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000634
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000635 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000636
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000637 BalancedDelimiterTracker T(*this, tok::l_paren);
638 if (T.consumeOpen()) {
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000639 Diag(Tok, diag::err_expected_lparen);
Richard Smith76965712012-09-13 19:12:50 +0000640 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000641 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000642 }
Mike Stump11289f42009-09-09 15:08:12 +0000643
John McCalldadc5752010-08-24 06:29:42 +0000644 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000645 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000646 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000647 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000648 }
Mike Stump11289f42009-09-09 15:08:12 +0000649
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000650 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCall48871652010-08-21 09:40:31 +0000651 return 0;
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000652
Richard Smithf506eaf2012-03-05 23:20:05 +0000653 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000654 Diag(Tok, diag::err_expected_string_literal)
655 << /*Source='static_assert'*/1;
Richard Smith76965712012-09-13 19:12:50 +0000656 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000657 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000658 }
Mike Stump11289f42009-09-09 15:08:12 +0000659
John McCalldadc5752010-08-24 06:29:42 +0000660 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smithd67aea22012-03-06 03:21:47 +0000661 if (AssertMessage.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000662 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000663 return 0;
Richard Smithd67aea22012-03-06 03:21:47 +0000664 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000665
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000666 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000667
Chris Lattner49836b42009-04-02 04:16:50 +0000668 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000669 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000670
John McCallb268a282010-08-23 23:25:46 +0000671 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
672 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000673 AssertMessage.take(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000674 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000675}
676
Richard Smith74aeef52013-04-26 16:15:35 +0000677/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000678///
679/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000680/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000681///
David Blaikie15a430a2011-12-04 05:04:18 +0000682SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
683 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
684 && "Not a decltype specifier");
685
David Blaikie15a430a2011-12-04 05:04:18 +0000686 ExprResult Result;
687 SourceLocation StartLoc = Tok.getLocation();
688 SourceLocation EndLoc;
689
690 if (Tok.is(tok::annot_decltype)) {
691 Result = getExprAnnotation(Tok);
692 EndLoc = Tok.getAnnotationEndLoc();
693 ConsumeToken();
694 if (Result.isInvalid()) {
695 DS.SetTypeSpecError();
696 return EndLoc;
697 }
698 } else {
Richard Smith324df552012-02-24 22:30:04 +0000699 if (Tok.getIdentifierInfo()->isStr("decltype"))
700 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000701
David Blaikie15a430a2011-12-04 05:04:18 +0000702 ConsumeToken();
703
704 BalancedDelimiterTracker T(*this, tok::l_paren);
705 if (T.expectAndConsume(diag::err_expected_lparen_after,
706 "decltype", tok::r_paren)) {
707 DS.SetTypeSpecError();
708 return T.getOpenLocation() == Tok.getLocation() ?
709 StartLoc : T.getOpenLocation();
710 }
711
Richard Smith74aeef52013-04-26 16:15:35 +0000712 // Check for C++1y 'decltype(auto)'.
713 if (Tok.is(tok::kw_auto)) {
714 // No need to disambiguate here: an expression can't start with 'auto',
715 // because the typename-specifier in a function-style cast operation can't
716 // be 'auto'.
717 Diag(Tok.getLocation(),
718 getLangOpts().CPlusPlus1y
719 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
720 : diag::ext_decltype_auto_type_specifier);
721 ConsumeToken();
722 } else {
723 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000724
Richard Smith74aeef52013-04-26 16:15:35 +0000725 // C++11 [dcl.type.simple]p4:
726 // The operand of the decltype specifier is an unevaluated operand.
727 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
728 0, /*IsDecltype=*/true);
729 Result = ParseExpression();
730 if (Result.isInvalid()) {
731 DS.SetTypeSpecError();
732 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true,
733 /*DontConsume=*/true)) {
734 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000735 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000736 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
737 // Backtrack to get the location of the last token before the semi.
738 PP.RevertCachedTokens(2);
739 ConsumeToken(); // the semi.
740 EndLoc = ConsumeAnyToken();
741 assert(Tok.is(tok::semi));
742 } else {
743 EndLoc = Tok.getLocation();
744 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000745 }
Richard Smith74aeef52013-04-26 16:15:35 +0000746 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000747 }
Richard Smith74aeef52013-04-26 16:15:35 +0000748
749 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie15a430a2011-12-04 05:04:18 +0000750 }
751
752 // Match the ')'
753 T.consumeClose();
754 if (T.getCloseLocation().isInvalid()) {
755 DS.SetTypeSpecError();
756 // FIXME: this should return the location of the last token
757 // that was consumed (by "consumeClose()")
758 return T.getCloseLocation();
759 }
760
Richard Smithfd555f62012-02-22 02:04:18 +0000761 if (Result.isInvalid()) {
762 DS.SetTypeSpecError();
763 return T.getCloseLocation();
764 }
765
David Blaikie15a430a2011-12-04 05:04:18 +0000766 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000767 }
Richard Smith74aeef52013-04-26 16:15:35 +0000768 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000769
Anders Carlsson74948d02009-06-24 17:47:40 +0000770 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000771 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000772 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000773 if (Result.get()
774 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
775 DiagID, Result.release())
776 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
777 DiagID)) {
John McCall49bfce42009-08-03 20:12:06 +0000778 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000779 DS.SetTypeSpecError();
780 }
781 return EndLoc;
782}
783
784void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
785 SourceLocation StartLoc,
786 SourceLocation EndLoc) {
787 // make sure we have a token we can turn into an annotation token
788 if (PP.isBacktrackEnabled())
789 PP.RevertCachedTokens(1);
790 else
791 PP.EnterToken(Tok);
792
793 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000794 setExprAnnotation(Tok,
795 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
796 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
797 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000798 Tok.setAnnotationEndLoc(EndLoc);
799 Tok.setLocation(StartLoc);
800 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000801}
802
Alexis Hunt4a257072011-05-19 05:37:45 +0000803void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
804 assert(Tok.is(tok::kw___underlying_type) &&
805 "Not an underlying type specifier");
806
807 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000808 BalancedDelimiterTracker T(*this, tok::l_paren);
809 if (T.expectAndConsume(diag::err_expected_lparen_after,
810 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000811 return;
812 }
813
814 TypeResult Result = ParseTypeName();
815 if (Result.isInvalid()) {
816 SkipUntil(tok::r_paren);
817 return;
818 }
819
820 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000821 T.consumeClose();
822 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000823 return;
824
825 const char *PrevSpec = 0;
826 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000827 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Alexis Hunt4a257072011-05-19 05:37:45 +0000828 DiagID, Result.release()))
829 Diag(StartLoc, DiagID) << PrevSpec;
830}
831
David Blaikie00ee7a082011-10-25 15:01:20 +0000832/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
833/// class name or decltype-specifier. Note that we only check that the result
834/// names a type; semantic analysis will need to verify that the type names a
835/// class. The result is either a type or null, depending on whether a type
836/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000837///
Richard Smith4c96e992013-02-19 23:47:15 +0000838/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000839/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000840/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000841/// nested-name-specifier[opt] class-name
842/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000843/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000844/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000845/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000846///
Richard Smith4c96e992013-02-19 23:47:15 +0000847/// In C++98, instead of base-type-specifier, we have:
848///
849/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie1cd50022011-10-25 17:10:12 +0000850Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
851 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000852 // Ignore attempts to use typename
853 if (Tok.is(tok::kw_typename)) {
854 Diag(Tok, diag::err_expected_class_name_not_template)
855 << FixItHint::CreateRemoval(Tok.getLocation());
856 ConsumeToken();
857 }
858
David Blaikieafa155f2011-10-25 18:17:58 +0000859 // Parse optional nested-name-specifier
860 CXXScopeSpec SS;
861 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
862
863 BaseLoc = Tok.getLocation();
864
David Blaikie1cd50022011-10-25 17:10:12 +0000865 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000866 // tok == kw_decltype is just error recovery, it can only happen when SS
867 // isn't empty
868 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000869 if (SS.isNotEmpty())
870 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
871 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000872 // Fake up a Declarator to use with ActOnTypeName.
873 DeclSpec DS(AttrFactory);
874
David Blaikie7491e732011-12-08 04:53:15 +0000875 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000876
877 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
878 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
879 }
880
Douglas Gregord54dfb82009-02-25 23:52:28 +0000881 // Check whether we have a template-id that names a type.
882 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000883 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000884 if (TemplateId->Kind == TNK_Type_template ||
885 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000886 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000887
888 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000889 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000890 EndLocation = Tok.getAnnotationEndLoc();
891 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000892
893 if (Type)
894 return Type;
895 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000896 }
897
898 // Fall through to produce an error below.
899 }
900
Douglas Gregor831c93f2008-11-05 20:51:48 +0000901 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000902 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000903 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000904 }
905
Douglas Gregor18473f32010-01-12 21:28:44 +0000906 IdentifierInfo *Id = Tok.getIdentifierInfo();
907 SourceLocation IdLoc = ConsumeToken();
908
909 if (Tok.is(tok::less)) {
910 // It looks the user intended to write a template-id here, but the
911 // template-name was wrong. Try to fix that.
912 TemplateNameKind TNK = TNK_Type_template;
913 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000914 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000915 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000916 Diag(IdLoc, diag::err_unknown_template_name)
917 << Id;
918 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000919
Douglas Gregor18473f32010-01-12 21:28:44 +0000920 if (!Template)
921 return true;
922
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000923 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000924 UnqualifiedId TemplateName;
925 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000926
Douglas Gregor18473f32010-01-12 21:28:44 +0000927 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000928 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
929 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000930 return true;
931 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000932 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000933
Douglas Gregor18473f32010-01-12 21:28:44 +0000934 // If we didn't end up with a typename token, there's nothing more we
935 // can do.
936 if (Tok.isNot(tok::annot_typename))
937 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000938
Douglas Gregor18473f32010-01-12 21:28:44 +0000939 // Retrieve the type from the annotation token, consume that token, and
940 // return.
941 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000942 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000943 ConsumeToken();
944 return Type;
945 }
946
Douglas Gregor831c93f2008-11-05 20:51:48 +0000947 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000948 IdentifierInfo *CorrectedII = 0;
Douglas Gregore7c20652011-03-02 00:47:37 +0000949 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000950 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +0000951 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000952 /*NonTrivialTypeSourceInfo=*/true,
953 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000954 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000955 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000956 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000957 }
958
959 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000960 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000961
962 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000963 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +0000964 DS.SetRangeStart(IdLoc);
965 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000966 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000967
968 const char *PrevSpec = 0;
969 unsigned DiagID;
970 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
971
972 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
973 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000974}
975
John McCall8d32c052012-05-22 21:28:12 +0000976void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
977 while (Tok.is(tok::kw___single_inheritance) ||
978 Tok.is(tok::kw___multiple_inheritance) ||
979 Tok.is(tok::kw___virtual_inheritance)) {
980 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
981 SourceLocation AttrNameLoc = ConsumeToken();
982 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000983 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCall8d32c052012-05-22 21:28:12 +0000984 }
985}
986
Richard Smith369b9f92012-06-25 21:37:02 +0000987/// Determine whether the following tokens are valid after a type-specifier
988/// which could be a standalone declaration. This will conservatively return
989/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +0000990bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +0000991 // This switch enumerates the valid "follow" set for type-specifiers.
992 switch (Tok.getKind()) {
993 default: break;
994 case tok::semi: // struct foo {...} ;
995 case tok::star: // struct foo {...} * P;
996 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +0000997 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +0000998 case tok::identifier: // struct foo {...} V ;
999 case tok::r_paren: //(struct foo {...} ) {4}
1000 case tok::annot_cxxscope: // struct foo {...} a:: b;
1001 case tok::annot_typename: // struct foo {...} a ::b;
1002 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1003 case tok::l_paren: // struct foo {...} ( x);
1004 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001005 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smith369b9f92012-06-25 21:37:02 +00001006 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001007 case tok::colon:
1008 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001009 // Type qualifiers
1010 case tok::kw_const: // struct foo {...} const x;
1011 case tok::kw_volatile: // struct foo {...} volatile x;
1012 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001013 // Function specifiers
1014 // Note, no 'explicit'. An explicit function must be either a conversion
1015 // operator or a constructor. Either way, it can't have a return type.
1016 case tok::kw_inline: // struct foo inline f();
1017 case tok::kw_virtual: // struct foo virtual f();
1018 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001019 // Storage-class specifiers
1020 case tok::kw_static: // struct foo {...} static x;
1021 case tok::kw_extern: // struct foo {...} extern x;
1022 case tok::kw_typedef: // struct foo {...} typedef x;
1023 case tok::kw_register: // struct foo {...} register x;
1024 case tok::kw_auto: // struct foo {...} auto x;
1025 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001026 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001027 case tok::kw_constexpr: // struct foo {...} constexpr x;
1028 // As shown above, type qualifiers and storage class specifiers absolutely
1029 // can occur after class specifiers according to the grammar. However,
1030 // almost no one actually writes code like this. If we see one of these,
1031 // it is much more likely that someone missed a semi colon and the
1032 // type/storage class specifier we're seeing is part of the *next*
1033 // intended declaration, as in:
1034 //
1035 // struct foo { ... }
1036 // typedef int X;
1037 //
1038 // We'd really like to emit a missing semicolon error instead of emitting
1039 // an error on the 'int' saying that you can't have two type specifiers in
1040 // the same declaration of X. Because of this, we look ahead past this
1041 // token to see if it's a type specifier. If so, we know the code is
1042 // otherwise invalid, so we can produce the expected semi error.
1043 if (!isKnownToBeTypeSpecifier(NextToken()))
1044 return true;
1045 break;
1046 case tok::r_brace: // struct bar { struct foo {...} }
1047 // Missing ';' at end of struct is accepted as an extension in C mode.
1048 if (!getLangOpts().CPlusPlus)
1049 return true;
1050 break;
Richard Smith1ac67d12013-01-19 03:48:05 +00001051 // C++11 attributes
1052 case tok::l_square: // enum E [[]] x
1053 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1054 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith52c5b872013-01-29 04:13:32 +00001055 case tok::greater:
1056 // template<class T = class X>
1057 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001058 }
1059 return false;
1060}
1061
Douglas Gregor556877c2008-04-13 21:30:24 +00001062/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1063/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1064/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001065/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001066///
1067/// class-specifier: [C++ class]
1068/// class-head '{' member-specification[opt] '}'
1069/// class-head '{' member-specification[opt] '}' attributes[opt]
1070/// class-head:
1071/// class-key identifier[opt] base-clause[opt]
1072/// class-key nested-name-specifier identifier base-clause[opt]
1073/// class-key nested-name-specifier[opt] simple-template-id
1074/// base-clause[opt]
1075/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001076/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001077/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001078/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001079/// simple-template-id base-clause[opt]
1080/// class-key:
1081/// 'class'
1082/// 'struct'
1083/// 'union'
1084///
1085/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001086/// class-key ::[opt] nested-name-specifier[opt] identifier
1087/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1088/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001089///
1090/// Note that the C++ class-specifier and elaborated-type-specifier,
1091/// together, subsume the C99 struct-or-union-specifier:
1092///
1093/// struct-or-union-specifier: [C99 6.7.2.1]
1094/// struct-or-union identifier[opt] '{' struct-contents '}'
1095/// struct-or-union identifier
1096/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1097/// '}' attributes[opt]
1098/// [GNU] struct-or-union attributes[opt] identifier
1099/// struct-or-union:
1100/// 'struct'
1101/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001102void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1103 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001104 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001105 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001106 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001107 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001108 DeclSpec::TST TagType;
1109 if (TagTokKind == tok::kw_struct)
1110 TagType = DeclSpec::TST_struct;
1111 else if (TagTokKind == tok::kw___interface)
1112 TagType = DeclSpec::TST_interface;
1113 else if (TagTokKind == tok::kw_class)
1114 TagType = DeclSpec::TST_class;
1115 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001116 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1117 TagType = DeclSpec::TST_union;
1118 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001119
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001120 if (Tok.is(tok::code_completion)) {
1121 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001122 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001123 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001124 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001125
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001126 // C++03 [temp.explicit] 14.7.2/8:
1127 // The usual access checking rules do not apply to names used to specify
1128 // explicit instantiations.
1129 //
1130 // As an extension we do not perform access checking on the names used to
1131 // specify explicit specializations either. This is important to allow
1132 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001133 //
1134 // Note that we don't suppress if this turns out to be an elaborated
1135 // type specifier.
1136 bool shouldDelayDiagsInTag =
1137 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1138 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1139 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001140
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001141 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001142 // If attributes exist after tag, parse them.
1143 if (Tok.is(tok::kw___attribute))
John McCall53fa7142010-12-24 02:08:15 +00001144 ParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001145
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001146 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001147 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001148 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001149
John McCall8d32c052012-05-22 21:28:12 +00001150 // Parse inheritance specifiers.
1151 if (Tok.is(tok::kw___single_inheritance) ||
1152 Tok.is(tok::kw___multiple_inheritance) ||
1153 Tok.is(tok::kw___virtual_inheritance))
1154 ParseMicrosoftInheritanceClassAttributes(attrs);
1155
Alexis Hunt96d5c762009-11-21 08:43:09 +00001156 // If C++0x attributes exist here, parse them.
1157 // FIXME: Are we consistent with the ordering of parsing of different
1158 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001159 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001160
Michael Han309af292013-01-07 16:57:11 +00001161 // Source location used by FIXIT to insert misplaced
1162 // C++11 attributes
1163 SourceLocation AttrFixitLoc = Tok.getLocation();
1164
John Wiegley65497cc2011-04-27 23:09:49 +00001165 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001166 !Tok.is(tok::identifier) &&
1167 Tok.getIdentifierInfo() &&
1168 (Tok.is(tok::kw___is_arithmetic) ||
1169 Tok.is(tok::kw___is_convertible) ||
John Wiegley65497cc2011-04-27 23:09:49 +00001170 Tok.is(tok::kw___is_empty) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001171 Tok.is(tok::kw___is_floating_point) ||
1172 Tok.is(tok::kw___is_function) ||
John Wiegley65497cc2011-04-27 23:09:49 +00001173 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001174 Tok.is(tok::kw___is_integral) ||
1175 Tok.is(tok::kw___is_member_function_pointer) ||
1176 Tok.is(tok::kw___is_member_pointer) ||
1177 Tok.is(tok::kw___is_pod) ||
1178 Tok.is(tok::kw___is_pointer) ||
1179 Tok.is(tok::kw___is_same) ||
Douglas Gregor63180b12011-04-29 01:38:03 +00001180 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001181 Tok.is(tok::kw___is_signed) ||
1182 Tok.is(tok::kw___is_unsigned) ||
1183 Tok.is(tok::kw___is_void))) {
Douglas Gregordf445f02011-07-30 07:01:49 +00001184 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001185 // name of struct templates, but some are keywords in GCC >= 4.3
1186 // and Clang. Therefore, when we see the token sequence "struct
1187 // X", make X into a normal identifier rather than a keyword, to
1188 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001189 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregor119b0c72009-09-04 05:53:02 +00001190 Tok.setKind(tok::identifier);
1191 }
Mike Stump11289f42009-09-09 15:08:12 +00001192
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001193 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001194 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001195 if (getLangOpts().CPlusPlus) {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001196 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1197 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001198
Douglas Gregordf593fb2011-11-07 17:33:42 +00001199 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001200 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001201 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001202 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1203 Diag(Tok, diag::err_expected_ident);
1204 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001205
Douglas Gregor916462b2009-10-30 21:46:58 +00001206 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1207
Douglas Gregor67a65642009-02-17 23:15:12 +00001208 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +00001209 IdentifierInfo *Name = 0;
1210 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +00001211 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +00001212 if (Tok.is(tok::identifier)) {
1213 Name = Tok.getIdentifierInfo();
1214 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001215
David Blaikiebbafb8a2012-03-11 07:00:24 +00001216 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001217 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001218 // Eat the template argument list and try to continue parsing this as
1219 // a class (or template thereof).
1220 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001221 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001222 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001223 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001224 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001225 // We couldn't parse the template argument list at all, so don't
1226 // try to give any location information for the list.
1227 LAngleLoc = RAngleLoc = SourceLocation();
1228 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001229
Douglas Gregor916462b2009-10-30 21:46:58 +00001230 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matose9a3ed42012-08-31 22:18:20 +00001231 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1232 << (TagType == DeclSpec::TST_class? 0
1233 : TagType == DeclSpec::TST_struct? 1
1234 : TagType == DeclSpec::TST_interface? 2
1235 : 3)
1236 << Name
1237 << SourceRange(LAngleLoc, RAngleLoc);
1238
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001239 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001240 // we've removed its template argument list.
1241 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1242 if (TemplateParams && TemplateParams->size() > 1) {
1243 TemplateParams->pop_back();
1244 } else {
1245 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001246 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001247 = ParsedTemplateInfo::NonTemplate;
1248 }
1249 } else if (TemplateInfo.Kind
1250 == ParsedTemplateInfo::ExplicitInstantiation) {
1251 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +00001252 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001253 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001254 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001255 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001256 = SourceLocation();
1257 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1258 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001259 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001260 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001261 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001262 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001263 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001264
Douglas Gregore7c20652011-03-02 00:47:37 +00001265 if (TemplateId->Kind != TNK_Type_template &&
1266 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001267 // The template-name in the simple-template-id refers to
1268 // something other than a class template. Give an appropriate
1269 // error message and skip to the ';'.
1270 SourceRange Range(NameLoc);
1271 if (SS.isNotEmpty())
1272 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001273
Douglas Gregor7f741122009-02-25 19:37:18 +00001274 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001275 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001276
Douglas Gregor7f741122009-02-25 19:37:18 +00001277 DS.SetTypeSpecError();
1278 SkipUntil(tok::semi, false, true);
Douglas Gregor7f741122009-02-25 19:37:18 +00001279 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001280 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001281 }
1282
Richard Smithbfdb1082012-03-12 08:56:40 +00001283 // There are four options here.
1284 // - If we are in a trailing return type, this is always just a reference,
1285 // and we must not try to parse a definition. For instance,
1286 // [] () -> struct S { };
1287 // does not define a type.
1288 // - If we have 'struct foo {...', 'struct foo :...',
1289 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1290 // - If we have 'struct foo;', then this is either a forward declaration
1291 // or a friend declaration, which have to be treated differently.
1292 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001293 //
1294 // We also detect these erroneous cases to provide better diagnostic for
1295 // C++11 attributes parsing.
1296 // - attributes follow class name:
1297 // struct foo [[]] {};
1298 // - attributes appear before or after 'final':
1299 // struct foo [[]] final [[]] {};
1300 //
Richard Smithc5b05522012-03-12 07:56:15 +00001301 // However, in type-specifier-seq's, things look like declarations but are
1302 // just references, e.g.
1303 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001304 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001305 // &T::operator struct s;
1306 // For these, DSC is DSC_type_specifier.
Michael Han9407e502012-11-26 22:54:45 +00001307
1308 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001309 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001310
John McCallfaf5fb42010-08-26 23:41:50 +00001311 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001312 if (DSC == DSC_trailing)
1313 TUK = Sema::TUK_Reference;
1314 else if (Tok.is(tok::l_brace) ||
1315 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001316 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001317 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001318 if (DS.isFriendSpecified()) {
1319 // C++ [class.friend]p2:
1320 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001321 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001322 << SourceRange(DS.getFriendSpecLoc());
1323
1324 // Skip everything up to the semicolon, so that this looks like a proper
1325 // friend class (or template thereof) declaration.
1326 SkipUntil(tok::semi, true, true);
John McCallfaf5fb42010-08-26 23:41:50 +00001327 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001328 } else {
1329 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001330 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001331 }
Richard Smith434516c2013-02-22 06:46:23 +00001332 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1333 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001334 // We can't tell if this is a definition or reference
1335 // until we skipped the 'final' and C++11 attribute specifiers.
1336 TentativeParsingAction PA(*this);
1337
1338 // Skip the 'final' keyword.
1339 ConsumeToken();
1340
1341 // Skip C++11 attribute specifiers.
1342 while (true) {
1343 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1344 ConsumeBracket();
1345 if (!SkipUntil(tok::r_square))
1346 break;
Richard Smith434516c2013-02-22 06:46:23 +00001347 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001348 ConsumeToken();
1349 ConsumeParen();
1350 if (!SkipUntil(tok::r_paren))
1351 break;
1352 } else {
1353 break;
1354 }
1355 }
1356
1357 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1358 TUK = Sema::TUK_Definition;
1359 else
1360 TUK = Sema::TUK_Reference;
1361
1362 PA.Revert();
Richard Smith369b9f92012-06-25 21:37:02 +00001363 } else if (DSC != DSC_type_specifier &&
1364 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001365 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001366 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001367 if (Tok.isNot(tok::semi)) {
1368 // A semicolon was missing after this declaration. Diagnose and recover.
1369 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1370 DeclSpec::getSpecifierName(TagType));
1371 PP.EnterToken(Tok);
1372 Tok.setKind(tok::semi);
1373 }
Richard Smith369b9f92012-06-25 21:37:02 +00001374 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001375 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001376
Michael Han9407e502012-11-26 22:54:45 +00001377 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1378 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001379 if (TUK != Sema::TUK_Reference) {
1380 // If this is not a reference, then the only possible
1381 // valid place for C++11 attributes to appear here
1382 // is between class-key and class-name. If there are
1383 // any attributes after class-name, we try a fixit to move
1384 // them to the right place.
1385 SourceRange AttrRange = Attributes.Range;
1386 if (AttrRange.isValid()) {
1387 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1388 << AttrRange
1389 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1390 CharSourceRange(AttrRange, true))
1391 << FixItHint::CreateRemoval(AttrRange);
1392
1393 // Recover by adding misplaced attributes to the attribute list
1394 // of the class so they can be applied on the class later.
1395 attrs.takeAllFrom(Attributes);
1396 }
1397 }
Michael Han9407e502012-11-26 22:54:45 +00001398
John McCall6347b682012-05-07 06:16:58 +00001399 // If this is an elaborated type specifier, and we delayed
1400 // diagnostics before, just merge them into the current pool.
1401 if (shouldDelayDiagsInTag) {
1402 diagsFromTag.done();
1403 if (TUK == Sema::TUK_Reference)
1404 diagsFromTag.redelay();
1405 }
1406
John McCall413021a2010-07-30 06:26:29 +00001407 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001408 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001409 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1410 // We have a declaration or reference to an anonymous class.
1411 Diag(StartLoc, diag::err_anon_type_definition)
1412 << DeclSpec::getSpecifierName(TagType);
1413 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001414
Douglas Gregor556877c2008-04-13 21:30:24 +00001415 SkipUntil(tok::comma, true);
1416 return;
1417 }
1418
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001419 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001420 DeclResult TagOrTempResult = true; // invalid
1421 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001422
Douglas Gregord6ab8742009-05-28 23:31:59 +00001423 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001424 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001425 // Explicit specialization, class template partial specialization,
1426 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001427 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001428 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001429 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001430 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001431 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001432 ProhibitAttributes(attrs);
1433
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001434 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001435 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001436 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001437 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001438 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001439 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001440 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001441 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001442 TemplateId->TemplateNameLoc,
1443 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001444 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001445 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001446 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001447
1448 // Friend template-ids are treated as references unless
1449 // they have template headers, in which case they're ill-formed
1450 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1451 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001452 } else if (TUK == Sema::TUK_Reference ||
1453 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001454 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001455 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001456 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001457 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001458 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001459 TemplateId->Template,
1460 TemplateId->TemplateNameLoc,
1461 TemplateId->LAngleLoc,
1462 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001463 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001464 } else {
1465 // This is an explicit specialization or a class template
1466 // partial specialization.
1467 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001468 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1469 // This looks like an explicit instantiation, because we have
1470 // something like
1471 //
1472 // template class Foo<X>
1473 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001474 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001475 // meant to be an explicit specialization, but the user forgot
1476 // the '<>' after 'template'.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001477 // It this is friend declaration however, since it cannot have a
1478 // template header, it is most likely that the user meant to
1479 // remove the 'template' keyword.
1480 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
1481 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001482
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001483 if (TUK == Sema::TUK_Friend) {
1484 Diag(DS.getFriendSpecLoc(),
1485 diag::err_friend_explicit_instantiation);
1486 TemplateParams = 0;
1487 } else {
1488 SourceLocation LAngleLoc
1489 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1490 Diag(TemplateId->TemplateNameLoc,
1491 diag::err_explicit_instantiation_with_definition)
1492 << SourceRange(TemplateInfo.TemplateLoc)
1493 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1494
1495 // Create a fake template parameter list that contains only
1496 // "template<>", so that we treat this construct as a class
1497 // template specialization.
1498 FakedParamLists.push_back(
1499 Actions.ActOnTemplateParameterList(0, SourceLocation(),
1500 TemplateInfo.TemplateLoc,
1501 LAngleLoc,
1502 0, 0,
1503 LAngleLoc));
1504 TemplateParams = &FakedParamLists;
1505 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001506 }
1507
1508 // Build the class template specialization.
1509 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001510 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor3c7cd6a2011-09-09 20:53:38 +00001511 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall3e56fd42010-08-23 07:28:44 +00001512 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001513 TemplateId->TemplateNameLoc,
1514 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +00001515 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001516 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001517 attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001518 MultiTemplateParamsArg(
Douglas Gregor67a65642009-02-17 23:15:12 +00001519 TemplateParams? &(*TemplateParams)[0] : 0,
1520 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001521 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001522 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001523 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001524 // Explicit instantiation of a member of a class template
1525 // specialization, e.g.,
1526 //
1527 // template struct Outer<int>::Inner;
1528 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001529 ProhibitAttributes(attrs);
1530
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001531 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001532 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001533 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001534 TemplateInfo.TemplateLoc,
1535 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001536 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001537 } else if (TUK == Sema::TUK_Friend &&
1538 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001539 ProhibitAttributes(attrs);
1540
John McCallace48cd2010-10-19 01:40:49 +00001541 TagOrTempResult =
1542 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1543 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001544 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001545 MultiTemplateParamsArg(
John McCallace48cd2010-10-19 01:40:49 +00001546 TemplateParams? &(*TemplateParams)[0] : 0,
1547 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001548 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001549 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1550 ProhibitAttributes(attrs);
Larisse Voufo725de3e2013-06-21 00:08:46 +00001551
1552 if (TUK == Sema::TUK_Definition &&
1553 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1554 // If the declarator-id is not a template-id, issue a diagnostic and
1555 // recover by ignoring the 'template' keyword.
1556 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1557 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001558 TemplateParams = 0;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001559 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001560
John McCall7f41d982009-09-11 04:59:25 +00001561 bool IsDependent = false;
1562
John McCall32723e92010-10-19 18:40:57 +00001563 // Don't pass down template parameter lists if this is just a tag
1564 // reference. For example, we don't need the template parameters here:
1565 // template <class T> class A *makeA(T t);
1566 MultiTemplateParamsArg TParams;
1567 if (TUK != Sema::TUK_Reference && TemplateParams)
1568 TParams =
1569 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1570
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001571 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001572 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001573 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001574 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001575 TParams, Owned, IsDependent,
1576 SourceLocation(), false,
1577 clang::TypeResult());
John McCall7f41d982009-09-11 04:59:25 +00001578
1579 // If ActOnTag said the type was dependent, try again with the
1580 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001581 if (IsDependent) {
1582 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001583 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001584 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001585 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001586 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001587
Douglas Gregor556877c2008-04-13 21:30:24 +00001588 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001589 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001590 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001591 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001592 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001593 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001594 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1595 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001596 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001597 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001598 }
1599
John McCallba7bf592010-08-24 05:47:05 +00001600 const char *PrevSpec = 0;
1601 unsigned DiagID;
1602 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001603 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001604 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1605 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallba7bf592010-08-24 05:47:05 +00001606 PrevSpec, DiagID, TypeResult.get());
John McCall7f41d982009-09-11 04:59:25 +00001607 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001608 Result = DS.SetTypeSpecType(TagType, StartLoc,
1609 NameLoc.isValid() ? NameLoc : StartLoc,
1610 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCall7f41d982009-09-11 04:59:25 +00001611 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001612 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001613 return;
1614 }
Mike Stump11289f42009-09-09 15:08:12 +00001615
John McCallba7bf592010-08-24 05:47:05 +00001616 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001617 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001618
Chris Lattnercf251412010-02-02 01:23:29 +00001619 // At this point, we've successfully parsed a class-specifier in 'definition'
1620 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1621 // going to look at what comes after it to improve error recovery. If an
1622 // impossible token occurs next, we assume that the programmer forgot a ; at
1623 // the end of the declaration and recover that way.
1624 //
Richard Smith369b9f92012-06-25 21:37:02 +00001625 // Also enforce C++ [temp]p3:
1626 // In a template-declaration which defines a class, no declarator
1627 // is permitted.
Joao Matose9a3ed42012-08-31 22:18:20 +00001628 if (TUK == Sema::TUK_Definition &&
1629 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001630 if (Tok.isNot(tok::semi)) {
1631 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1632 DeclSpec::getSpecifierName(TagType));
1633 // Push this token back into the preprocessor and change our current token
1634 // to ';' so that the rest of the code recovers as though there were an
1635 // ';' after the definition.
1636 PP.EnterToken(Tok);
1637 Tok.setKind(tok::semi);
1638 }
Chris Lattnercf251412010-02-02 01:23:29 +00001639 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001640}
1641
Mike Stump11289f42009-09-09 15:08:12 +00001642/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001643///
1644/// base-clause : [C++ class.derived]
1645/// ':' base-specifier-list
1646/// base-specifier-list:
1647/// base-specifier '...'[opt]
1648/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001649void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001650 assert(Tok.is(tok::colon) && "Not a base clause");
1651 ConsumeToken();
1652
Douglas Gregor29a92472008-10-22 17:49:05 +00001653 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001654 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001655
Douglas Gregor556877c2008-04-13 21:30:24 +00001656 while (true) {
1657 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001658 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001659 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001660 // Skip the rest of this base specifier, up until the comma or
1661 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +00001662 SkipUntil(tok::comma, tok::l_brace, true, true);
1663 } else {
1664 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001665 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001666 }
1667
1668 // If the next token is a comma, consume it and keep reading
1669 // base-specifiers.
1670 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001671
Douglas Gregor556877c2008-04-13 21:30:24 +00001672 // Consume the comma.
1673 ConsumeToken();
1674 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001675
1676 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001677 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001678}
1679
1680/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1681/// one entry in the base class list of a class specifier, for example:
1682/// class foo : public bar, virtual private baz {
1683/// 'public bar' and 'virtual private baz' are each base-specifiers.
1684///
1685/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001686/// attribute-specifier-seq[opt] base-type-specifier
1687/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1688/// base-type-specifier
1689/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1690/// base-type-specifier
John McCall48871652010-08-21 09:40:31 +00001691Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001692 bool IsVirtual = false;
1693 SourceLocation StartLoc = Tok.getLocation();
1694
Richard Smith4c96e992013-02-19 23:47:15 +00001695 ParsedAttributesWithRange Attributes(AttrFactory);
1696 MaybeParseCXX11Attributes(Attributes);
1697
Douglas Gregor556877c2008-04-13 21:30:24 +00001698 // Parse the 'virtual' keyword.
1699 if (Tok.is(tok::kw_virtual)) {
1700 ConsumeToken();
1701 IsVirtual = true;
1702 }
1703
Richard Smith4c96e992013-02-19 23:47:15 +00001704 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1705
Douglas Gregor556877c2008-04-13 21:30:24 +00001706 // Parse an (optional) access specifier.
1707 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001708 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001709 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001710
Richard Smith4c96e992013-02-19 23:47:15 +00001711 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1712
Douglas Gregor556877c2008-04-13 21:30:24 +00001713 // Parse the 'virtual' keyword (again!), in case it came after the
1714 // access specifier.
1715 if (Tok.is(tok::kw_virtual)) {
1716 SourceLocation VirtualLoc = ConsumeToken();
1717 if (IsVirtual) {
1718 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001719 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001720 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001721 }
1722
1723 IsVirtual = true;
1724 }
1725
Richard Smith4c96e992013-02-19 23:47:15 +00001726 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1727
Douglas Gregor831c93f2008-11-05 20:51:48 +00001728 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001729 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001730 SourceLocation BaseLoc;
1731 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001732 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001733 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001734
Douglas Gregor752a5952011-01-03 22:36:02 +00001735 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1736 // actually part of the base-specifier-list grammar productions, but we
1737 // parse it here for convenience.
1738 SourceLocation EllipsisLoc;
1739 if (Tok.is(tok::ellipsis))
1740 EllipsisLoc = ConsumeToken();
1741
Mike Stump11289f42009-09-09 15:08:12 +00001742 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001743 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001744
Douglas Gregor556877c2008-04-13 21:30:24 +00001745 // Notify semantic analysis that we have parsed a complete
1746 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001747 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1748 Access, BaseType.get(), BaseLoc,
1749 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001750}
1751
1752/// getAccessSpecifierIfPresent - Determine whether the next token is
1753/// a C++ access-specifier.
1754///
1755/// access-specifier: [C++ class.derived]
1756/// 'private'
1757/// 'protected'
1758/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001759AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001760 switch (Tok.getKind()) {
1761 default: return AS_none;
1762 case tok::kw_private: return AS_private;
1763 case tok::kw_protected: return AS_protected;
1764 case tok::kw_public: return AS_public;
1765 }
1766}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001767
Douglas Gregor433e0532012-04-16 18:27:27 +00001768/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001769/// delayed, e.g., default arguments, create a late-parsed method declaration
1770/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001771void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1772 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001773 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001774 // has any default arguments, we'll need to parse them later.
Eli Friedman3af2a772009-07-22 21:45:50 +00001775 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001776 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001777 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001778
Eli Friedman3af2a772009-07-22 21:45:50 +00001779 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1780 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1781 if (!LateMethod) {
1782 // Push this method onto the stack of late-parsed method
1783 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001784 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1785 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001786 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001787
1788 // Add all of the parameters prior to this one (they don't
1789 // have default arguments).
1790 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1791 for (unsigned I = 0; I < ParamIdx; ++I)
1792 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001793 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001794 }
1795
Douglas Gregor433e0532012-04-16 18:27:27 +00001796 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001797 // not have a default argument).
1798 LateMethod->DefaultArgs.push_back(
1799 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1800 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1801 }
1802 }
1803}
1804
Richard Smith89645bc2013-01-02 12:01:23 +00001805/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001806/// virt-specifier.
1807///
1808/// virt-specifier:
1809/// override
1810/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001811VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001812 if (!getLangOpts().CPlusPlus)
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001813 return VirtSpecifiers::VS_None;
1814
Anders Carlsson56104902011-01-17 03:05:47 +00001815 if (Tok.is(tok::identifier)) {
1816 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001817
Anders Carlsson428803b2011-01-20 03:47:08 +00001818 // Initialize the contextual keywords.
1819 if (!Ident_final) {
1820 Ident_final = &PP.getIdentifierTable().get("final");
1821 Ident_override = &PP.getIdentifierTable().get("override");
1822 }
1823
Anders Carlsson56104902011-01-17 03:05:47 +00001824 if (II == Ident_override)
1825 return VirtSpecifiers::VS_Override;
1826
1827 if (II == Ident_final)
1828 return VirtSpecifiers::VS_Final;
1829 }
1830
1831 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001832}
1833
Richard Smith89645bc2013-01-02 12:01:23 +00001834/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001835///
1836/// virt-specifier-seq:
1837/// virt-specifier
1838/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001839void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalldb632ac2012-09-25 07:32:39 +00001840 bool IsInterface) {
Anders Carlsson56104902011-01-17 03:05:47 +00001841 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001842 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001843 if (Specifier == VirtSpecifiers::VS_None)
1844 return;
1845
1846 // C++ [class.mem]p8:
1847 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001848 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001849 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001850 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1851 << PrevSpec
1852 << FixItHint::CreateRemoval(Tok.getLocation());
1853
John McCalldb632ac2012-09-25 07:32:39 +00001854 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1855 Diag(Tok.getLocation(), diag::err_override_control_interface)
1856 << VirtSpecifiers::getSpecifierName(Specifier);
1857 } else {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001858 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalldb632ac2012-09-25 07:32:39 +00001859 diag::warn_cxx98_compat_override_control_keyword :
1860 diag::ext_override_control_keyword)
1861 << VirtSpecifiers::getSpecifierName(Specifier);
1862 }
Anders Carlsson56104902011-01-17 03:05:47 +00001863 ConsumeToken();
1864 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001865}
1866
Richard Smith89645bc2013-01-02 12:01:23 +00001867/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlssoncafbab72011-03-25 14:53:29 +00001868/// contextual 'final' keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001869bool Parser::isCXX11FinalKeyword() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001870 if (!getLangOpts().CPlusPlus)
Anders Carlssoncafbab72011-03-25 14:53:29 +00001871 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001872
Anders Carlssoncafbab72011-03-25 14:53:29 +00001873 if (!Tok.is(tok::identifier))
1874 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001875
Anders Carlssoncafbab72011-03-25 14:53:29 +00001876 // Initialize the contextual keywords.
1877 if (!Ident_final) {
1878 Ident_final = &PP.getIdentifierTable().get("final");
1879 Ident_override = &PP.getIdentifierTable().get("override");
1880 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001881
Anders Carlssoncafbab72011-03-25 14:53:29 +00001882 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001883}
1884
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001885/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1886///
1887/// member-declaration:
1888/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1889/// function-definition ';'[opt]
1890/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1891/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001892/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001893/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001894/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001895///
1896/// member-declarator-list:
1897/// member-declarator
1898/// member-declarator-list ',' member-declarator
1899///
1900/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001901/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001902/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00001903/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001904/// identifier[opt] ':' constant-expression
1905///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001906/// virt-specifier-seq:
1907/// virt-specifier
1908/// virt-specifier-seq virt-specifier
1909///
1910/// virt-specifier:
1911/// override
1912/// final
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001913///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001914/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001915/// '= 0'
1916///
1917/// constant-initializer:
1918/// '=' constant-expression
1919///
Douglas Gregor3447e762009-08-20 22:52:58 +00001920void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001921 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00001922 const ParsedTemplateInfo &TemplateInfo,
1923 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001924 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001925 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00001926 Diag(Tok, diag::err_at_defs_cxx);
1927 else
1928 Diag(Tok, diag::err_at_in_class);
1929
1930 ConsumeToken();
1931 SkipUntil(tok::r_brace);
1932 return;
1933 }
1934
John McCalla0097262009-12-11 02:10:03 +00001935 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00001936 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00001937 if (!TemplateInfo.Kind &&
Richard Smith45855df2012-05-09 08:23:23 +00001938 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1939 if (TryAnnotateCXXScopeToken())
1940 MalformedTypeSpec = true;
1941
1942 bool isAccessDecl;
1943 if (Tok.isNot(tok::annot_cxxscope))
1944 isAccessDecl = false;
1945 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00001946 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1947 else
1948 isAccessDecl = NextToken().is(tok::kw_operator);
1949
1950 if (isAccessDecl) {
1951 // Collect the scope specifier token we annotated earlier.
1952 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001953 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1954 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00001955
1956 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001957 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00001958 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001959 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1960 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00001961 SkipUntil(tok::semi);
1962 return;
1963 }
1964
1965 // TODO: recover from mistakenly-qualified operator declarations.
1966 if (ExpectAndConsume(tok::semi,
1967 diag::err_expected_semi_after,
1968 "access declaration",
1969 tok::semi))
1970 return;
1971
Douglas Gregor0be31a22010-07-02 17:43:08 +00001972 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCalla0097262009-12-11 02:10:03 +00001973 false, SourceLocation(),
1974 SS, Name,
1975 /* AttrList */ 0,
1976 /* IsTypeName */ false,
1977 SourceLocation());
1978 return;
1979 }
1980 }
1981
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001982 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001983 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001984 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001985 SourceLocation DeclEnd;
1986 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001987 return;
1988 }
Mike Stump11289f42009-09-09 15:08:12 +00001989
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001990 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001991 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001992 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001993 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001994 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001995 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001996 return;
1997 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001998
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001999 // Handle: member-declaration ::= '__extension__' member-declaration
2000 if (Tok.is(tok::kw___extension__)) {
2001 // __extension__ silences extension warnings in the subexpression.
2002 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2003 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002004 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2005 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002006 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002007
Chris Lattnercf251412010-02-02 01:23:29 +00002008 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2009 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002010 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002011
John McCall084e83d2011-03-24 11:26:52 +00002012 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002013 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002014 // Optional C++11 attribute-specifier
2015 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002016 // We need to keep these attributes for future diagnostic
2017 // before they are taken over by declaration specifier.
2018 FnAttrs.addAll(attrs.getList());
2019 FnAttrs.Range = attrs.Range;
2020
John McCall53fa7142010-12-24 02:08:15 +00002021 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002022
Douglas Gregorfec52632009-06-20 00:51:54 +00002023 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002024 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002025
Douglas Gregorfec52632009-06-20 00:51:54 +00002026 // Eat 'using'.
2027 SourceLocation UsingLoc = ConsumeToken();
2028
2029 if (Tok.is(tok::kw_namespace)) {
2030 Diag(UsingLoc, diag::err_using_namespace_in_class);
2031 SkipUntil(tok::semi, true, true);
Chris Lattner916dbf12010-02-02 00:43:15 +00002032 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002033 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002034 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002035 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2036 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002037 }
2038 return;
2039 }
2040
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002041 // Hold late-parsed attributes so we can attach a Decl to them later.
2042 LateParsedAttrList CommonLateParsedAttrs;
2043
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002044 // decl-specifier-seq:
2045 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002046 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002047 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002048 if (MalformedTypeSpec)
2049 DS.SetTypeSpecError();
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002050 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2051 &CommonLateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002052
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002053 MultiTemplateParamsArg TemplateParams(
John McCall11083da2009-09-16 22:47:08 +00002054 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2055 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2056
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002057 if (Tok.is(tok::semi)) {
2058 ConsumeToken();
Michael Handdc016d2012-11-28 23:17:40 +00002059
2060 if (DS.isFriendSpecified())
2061 ProhibitAttributes(FnAttrs);
2062
John McCall48871652010-08-21 09:40:31 +00002063 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002064 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002065 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002066 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002067 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002068
John McCall28a6aea2009-11-04 02:18:39 +00002069 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002070 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002071
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002072 // Hold late-parsed attributes so we can attach a Decl to them later.
2073 LateParsedAttrList LateParsedAttrs;
2074
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002075 SourceLocation EqualLoc;
2076 bool HasInitializer = false;
2077 ExprResult Init;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002078 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002079 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2080 ColonProtectionRAIIObject X(*this);
2081
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002082 // Parse the first declarator.
2083 ParseDeclarator(DeclaratorInfo);
Richard Smith2331bbf2012-05-02 22:22:32 +00002084 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00002085 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002086 // If so, skip until the semi-colon or a }.
Sebastian Redl83f3b852011-04-24 16:27:48 +00002087 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002088 if (Tok.is(tok::semi))
2089 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002090 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002091 }
2092
Richard Smith89645bc2013-01-02 12:01:23 +00002093 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber24b2a822011-01-28 06:07:34 +00002094
John Thompson5bc5cbe2009-11-25 22:58:06 +00002095 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002096 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson5bc5cbe2009-11-25 22:58:06 +00002097
Francois Pichet3abc9b82011-05-11 02:14:46 +00002098 // MSVC permits pure specifier on inline functions declared at class scope.
2099 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002100 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002101 DeclaratorInfo.isFunctionDeclarator() &&
2102 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002103 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002104 Init = ParseInitializer();
2105 if (Init.isInvalid())
2106 SkipUntil(tok::comma, true, true);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002107 else
2108 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002109 }
2110
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002111 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002112 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002113 //
2114 // In C++11, a non-function declarator followed by an open brace is a
2115 // braced-init-list for an in-class member initialization, not an
2116 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002117 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002118 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002119 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002120 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002121 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002122 } else if (Tok.is(tok::equal)) {
2123 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002124 if (KW.is(tok::kw_default))
2125 DefinitionKind = FDK_Defaulted;
2126 else if (KW.is(tok::kw_delete))
2127 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002128 }
2129 }
2130
Michael Handdc016d2012-11-28 23:17:40 +00002131 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2132 // to a friend declaration, that declaration shall be a definition.
2133 if (DeclaratorInfo.isFunctionDeclarator() &&
2134 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2135 // Diagnose attributes that appear before decl specifier:
2136 // [[]] friend int foo();
2137 ProhibitAttributes(FnAttrs);
2138 }
2139
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002140 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002141 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002142 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002143 ConsumeBrace();
Richard Trieu0d730542012-01-21 02:59:18 +00002144 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Handdc016d2012-11-28 23:17:40 +00002145
Douglas Gregor8a4db832011-01-19 16:41:58 +00002146 // Consume the optional ';'
2147 if (Tok.is(tok::semi))
2148 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002149 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002150 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002151
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002152 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002153 Diag(DeclaratorInfo.getIdentifierLoc(),
2154 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002155
Richard Smith2603b092012-11-15 22:54:20 +00002156 // Recover by treating the 'typedef' as spurious.
2157 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002158 }
2159
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002160 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002161 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002162 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002163
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002164 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2165 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2166 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002167 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002168 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002169 }
2170 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002171
2172 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002173 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002174 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002175
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002176 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002177 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002178 }
2179
2180 // member-declarator-list:
2181 // member-declarator
2182 // member-declarator-list ',' member-declarator
2183
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002184 SmallVector<Decl *, 8> DeclsInGroup;
John McCalldadc5752010-08-24 06:29:42 +00002185 ExprResult BitfieldSize;
Richard Smithc8a79032012-01-09 22:31:44 +00002186 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002187
2188 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002189 // member-declarator:
2190 // declarator pure-specifier[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002191 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002192 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002193 if (Tok.is(tok::colon)) {
2194 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002195 BitfieldSize = ParseConstantExpression();
2196 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002197 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002198 }
Mike Stump11289f42009-09-09 15:08:12 +00002199
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002200 // If a simple-asm-expr is present, parse it.
2201 if (Tok.is(tok::kw_asm)) {
2202 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00002203 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002204 if (AsmLabel.isInvalid())
2205 SkipUntil(tok::comma, true, true);
2206
2207 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2208 DeclaratorInfo.SetRangeEnd(Loc);
2209 }
2210
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002211 // If attributes exist after the declarator, parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002212 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002213
Richard Smith938f40b2011-06-11 17:19:42 +00002214 // FIXME: When g++ adds support for this, we'll need to check whether it
2215 // goes before or after the GNU attributes and __asm__.
Richard Smith89645bc2013-01-02 12:01:23 +00002216 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith938f40b2011-06-11 17:19:42 +00002217
Richard Smith2b013182012-06-10 03:12:00 +00002218 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002219 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002220 if (BitfieldSize.get()) {
2221 Diag(Tok, diag::err_bitfield_member_init);
2222 SkipUntil(tok::comma, true, true);
2223 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002224 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002225 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2226 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002227 != DeclSpec::SCS_typedef)
2228 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002229 }
2230 }
2231
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002232 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002233 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002234 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002235
Rafael Espindola0a67e2f2013-01-08 20:44:06 +00002236 NamedDecl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00002237 if (DS.isFriendSpecified()) {
Michael Handdc016d2012-11-28 23:17:40 +00002238 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2239 // to a friend declaration, that declaration shall be a definition.
2240 //
2241 // Diagnose attributes appear after friend member function declarator:
2242 // foo [[]] ();
2243 SmallVector<SourceRange, 4> Ranges;
2244 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2245 if (!Ranges.empty()) {
2246 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2247 E = Ranges.end(); I != E; ++I) {
2248 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2249 << *I;
2250 }
2251 }
2252
John McCall2f212b32009-09-11 21:02:39 +00002253 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00002254 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002255 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002256 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002257 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002258 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002259 TemplateParams,
John McCall07e91c02009-08-06 02:15:43 +00002260 BitfieldSize.release(),
Richard Smith2b013182012-06-10 03:12:00 +00002261 VS, HasInClassInit);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002262 if (AccessAttrs)
2263 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2264 false, true);
Douglas Gregor3447e762009-08-20 22:52:58 +00002265 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002266
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002267 // Set the Decl for any late parsed attributes
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002268 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2269 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2270 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002271 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002272 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002273 }
2274 LateParsedAttrs.clear();
2275
Douglas Gregor728d00b2011-10-10 14:49:18 +00002276 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002277 if (HasInClassInit != ICIS_NoInit &&
2278 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2279 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002280 // The initializer was deferred; parse it and cache the tokens.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002281 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00002282 diag::warn_cxx98_compat_nonstatic_member_init :
2283 diag::ext_nonstatic_member_init);
2284
Richard Smith938f40b2011-06-11 17:19:42 +00002285 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002286 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2287 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002288 //
2289 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002290 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002291 Diag(Tok, diag::err_incomplete_array_member_init);
2292 SkipUntil(tok::comma, true, true);
David Blaikiecdd91db2012-02-14 09:00:46 +00002293 if (ThisDecl)
2294 // Avoid later warnings about a class member of incomplete type.
2295 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002296 } else
2297 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002298 } else if (HasInitializer) {
2299 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002300 if (!Init.isUsable())
Douglas Gregor926410d2012-02-21 02:22:07 +00002301 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002302 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2303
Douglas Gregor728d00b2011-10-10 14:49:18 +00002304 if (Init.isInvalid())
2305 SkipUntil(tok::comma, true, true);
2306 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002307 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002308 DS.containsPlaceholderType());
Douglas Gregor728d00b2011-10-10 14:49:18 +00002309 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2310 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002311 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
Richard Smith938f40b2011-06-11 17:19:42 +00002312 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002313
2314 if (ThisDecl) {
2315 Actions.FinalizeDeclaration(ThisDecl);
2316 DeclsInGroup.push_back(ThisDecl);
2317 }
2318
Richard Smith4f402bd2012-04-29 07:31:09 +00002319 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor728d00b2011-10-10 14:49:18 +00002320 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2321 != DeclSpec::SCS_typedef) {
Douglas Gregor433e0532012-04-16 18:27:27 +00002322 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002323 }
2324
2325 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002326
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002327 // If we don't have a comma, it is either the end of the list (a ';')
2328 // or an error, bail out.
2329 if (Tok.isNot(tok::comma))
2330 break;
Mike Stump11289f42009-09-09 15:08:12 +00002331
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002332 // Consume the comma.
Richard Smithc8a79032012-01-09 22:31:44 +00002333 SourceLocation CommaLoc = ConsumeToken();
2334
2335 if (Tok.isAtStartOfLine() &&
2336 !MightBeDeclarator(Declarator::MemberContext)) {
2337 // This comma was followed by a line-break and something which can't be
2338 // the start of a declarator. The comma was probably a typo for a
2339 // semicolon.
2340 Diag(CommaLoc, diag::err_expected_semi_declaration)
2341 << FixItHint::CreateReplacement(CommaLoc, ";");
2342 ExpectSemi = false;
2343 break;
2344 }
Mike Stump11289f42009-09-09 15:08:12 +00002345
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002346 // Parse the next declarator.
2347 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002348 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002349 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002350 Init = true;
2351 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002352 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002353
Bill Wendling44426052012-12-20 19:22:21 +00002354 // Attributes are only allowed on the second declarator.
John McCall53fa7142010-12-24 02:08:15 +00002355 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002356
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002357 if (Tok.isNot(tok::colon))
2358 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002359 }
2360
Richard Smithc8a79032012-01-09 22:31:44 +00002361 if (ExpectSemi &&
2362 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002363 // Skip to end of block or statement.
2364 SkipUntil(tok::r_brace, true, true);
2365 // If we stopped at a ';', eat it.
2366 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002367 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002368 }
2369
Douglas Gregor0be31a22010-07-02 17:43:08 +00002370 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattner916dbf12010-02-02 00:43:15 +00002371 DeclsInGroup.size());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002372}
2373
Richard Smith938f40b2011-06-11 17:19:42 +00002374/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2375/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2376/// function definition. The location of the '=', if any, will be placed in
2377/// EqualLoc.
2378///
2379/// pure-specifier:
2380/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002381///
Richard Smith938f40b2011-06-11 17:19:42 +00002382/// brace-or-equal-initializer:
2383/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002384/// braced-init-list
2385///
Richard Smith938f40b2011-06-11 17:19:42 +00002386/// initializer-clause:
2387/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002388/// braced-init-list
2389///
Richard Smith938f40b2011-06-11 17:19:42 +00002390/// defaulted/deleted function-definition:
2391/// '=' 'default'
2392/// '=' 'delete'
2393///
2394/// Prior to C++0x, the assignment-expression in an initializer-clause must
2395/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002396ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002397 SourceLocation &EqualLoc) {
2398 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2399 && "Data member initializer not starting with '=' or '{'");
2400
Douglas Gregor926410d2012-02-21 02:22:07 +00002401 EnterExpressionEvaluationContext Context(Actions,
2402 Sema::PotentiallyEvaluated,
2403 D);
Richard Smith938f40b2011-06-11 17:19:42 +00002404 if (Tok.is(tok::equal)) {
2405 EqualLoc = ConsumeToken();
2406 if (Tok.is(tok::kw_delete)) {
2407 // In principle, an initializer of '= delete p;' is legal, but it will
2408 // never type-check. It's better to diagnose it as an ill-formed expression
2409 // than as an ill-formed deleted non-function member.
2410 // An initializer of '= delete p, foo' will never be parsed, because
2411 // a top-level comma always ends the initializer expression.
2412 const Token &Next = NextToken();
2413 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2414 Next.is(tok::eof)) {
2415 if (IsFunction)
2416 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2417 << 1 /* delete */;
2418 else
2419 Diag(ConsumeToken(), diag::err_deleted_non_function);
2420 return ExprResult();
2421 }
2422 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002423 if (IsFunction)
2424 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2425 << 0 /* default */;
2426 else
2427 Diag(ConsumeToken(), diag::err_default_special_members);
2428 return ExprResult();
2429 }
2430
Sebastian Redleef474c2012-02-22 10:50:08 +00002431 }
2432 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002433}
2434
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002435/// ParseCXXMemberSpecification - Parse the class definition.
2436///
2437/// member-specification:
2438/// member-declaration member-specification[opt]
2439/// access-specifier ':' member-specification[opt]
2440///
Joao Matose9a3ed42012-08-31 22:18:20 +00002441void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002442 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002443 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002444 unsigned TagType, Decl *TagDecl) {
2445 assert((TagType == DeclSpec::TST_struct ||
2446 TagType == DeclSpec::TST_interface ||
2447 TagType == DeclSpec::TST_union ||
2448 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2449
John McCallfaf5fb42010-08-26 23:41:50 +00002450 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2451 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002452
Douglas Gregoredf8f392010-01-16 20:52:59 +00002453 // Determine whether this is a non-nested class. Note that local
2454 // classes are *not* considered to be nested classes.
2455 bool NonNestedClass = true;
2456 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002457 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002458 if (S->isClassScope()) {
2459 // We're inside a class scope, so this is a nested class.
2460 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002461
2462 // The Microsoft extension __interface does not permit nested classes.
2463 if (getCurrentClass().IsInterface) {
2464 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2465 << /*ErrorType=*/6
2466 << (isa<NamedDecl>(TagDecl)
2467 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2468 : "<anonymous>");
2469 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002470 break;
2471 }
2472
2473 if ((S->getFlags() & Scope::FnScope)) {
2474 // If we're in a function or function template declared in the
2475 // body of a class, then this is a local class rather than a
2476 // nested class.
2477 const Scope *Parent = S->getParent();
2478 if (Parent->isTemplateParamScope())
2479 Parent = Parent->getParent();
2480 if (Parent->isClassScope())
2481 break;
2482 }
2483 }
2484 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002485
2486 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002487 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002488
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002489 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002490 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2491 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002492
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002493 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002494 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002495
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002496 SourceLocation FinalLoc;
2497
2498 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002499 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith89645bc2013-01-02 12:01:23 +00002500 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002501 FinalLoc = ConsumeToken();
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002502
John McCalldb632ac2012-09-25 07:32:39 +00002503 if (TagType == DeclSpec::TST_interface) {
2504 Diag(FinalLoc, diag::err_override_control_interface)
2505 << "final";
2506 } else {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002507 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalldb632ac2012-09-25 07:32:39 +00002508 diag::warn_cxx98_compat_override_control_keyword :
2509 diag::ext_override_control_keyword) << "final";
2510 }
Michael Han9407e502012-11-26 22:54:45 +00002511
Michael Han309af292013-01-07 16:57:11 +00002512 // Parse any C++11 attributes after 'final' keyword.
2513 // These attributes are not allowed to appear here,
2514 // and the only possible place for them to appertain
2515 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002516 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002517 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002518
John McCall2d814c32009-12-19 21:48:58 +00002519 if (Tok.is(tok::colon)) {
2520 ParseBaseClause(TagDecl);
2521
2522 if (!Tok.is(tok::l_brace)) {
2523 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00002524
2525 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002526 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002527 return;
2528 }
2529 }
2530
2531 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002532 BalancedDelimiterTracker T(*this, tok::l_brace);
2533 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002534
John McCall08bede42010-05-28 08:11:17 +00002535 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002536 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002537 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002538
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002539 // C++ 11p3: Members of a class defined with the keyword class are private
2540 // by default. Members of a class defined with the keywords struct or union
2541 // are public by default.
2542 AccessSpecifier CurAS;
2543 if (TagType == DeclSpec::TST_class)
2544 CurAS = AS_private;
2545 else
2546 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002547 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002548
Douglas Gregor9377c822010-06-21 22:31:09 +00002549 if (TagDecl) {
2550 // While we still have something to read, read the member-declarations.
2551 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2552 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002553
David Blaikiebbafb8a2012-03-11 07:00:24 +00002554 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002555 Tok.is(tok::kw___if_not_exists))) {
2556 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2557 continue;
2558 }
2559
Douglas Gregor9377c822010-06-21 22:31:09 +00002560 // Check for extraneous top-level semicolon.
2561 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002562 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002563 continue;
2564 }
2565
Eli Friedmanec52f922012-02-23 23:47:16 +00002566 if (Tok.is(tok::annot_pragma_vis)) {
2567 HandlePragmaVisibility();
2568 continue;
2569 }
2570
2571 if (Tok.is(tok::annot_pragma_pack)) {
2572 HandlePragmaPack();
2573 continue;
2574 }
2575
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002576 if (Tok.is(tok::annot_pragma_align)) {
2577 HandlePragmaAlign();
2578 continue;
2579 }
2580
Alexey Bataeva769e072013-03-22 06:34:35 +00002581 if (Tok.is(tok::annot_pragma_openmp)) {
2582 ParseOpenMPDeclarativeDirective();
2583 continue;
2584 }
2585
Douglas Gregor9377c822010-06-21 22:31:09 +00002586 AccessSpecifier AS = getAccessSpecifierIfPresent();
2587 if (AS != AS_none) {
2588 // Current token is a C++ access specifier.
2589 CurAS = AS;
2590 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002591 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002592 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002593 AccessAttrs.clear();
2594 MaybeParseGNUAttributes(AccessAttrs);
2595
David Blaikieeba32c22011-10-13 06:08:43 +00002596 SourceLocation EndLoc;
2597 if (Tok.is(tok::colon)) {
2598 EndLoc = Tok.getLocation();
2599 ConsumeToken();
2600 } else if (Tok.is(tok::semi)) {
2601 EndLoc = Tok.getLocation();
2602 ConsumeToken();
2603 Diag(EndLoc, diag::err_expected_colon)
2604 << FixItHint::CreateReplacement(EndLoc, ":");
2605 } else {
2606 EndLoc = ASLoc.getLocWithOffset(TokLength);
2607 Diag(EndLoc, diag::err_expected_colon)
2608 << FixItHint::CreateInsertion(EndLoc, ":");
2609 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002610
John McCalldb632ac2012-09-25 07:32:39 +00002611 // The Microsoft extension __interface does not permit non-public
2612 // access specifiers.
2613 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2614 Diag(ASLoc, diag::err_access_specifier_interface)
2615 << (CurAS == AS_protected);
2616 }
2617
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002618 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2619 AccessAttrs.getList())) {
2620 // found another attribute than only annotations
2621 AccessAttrs.clear();
2622 }
2623
Douglas Gregor9377c822010-06-21 22:31:09 +00002624 continue;
2625 }
2626
2627 // FIXME: Make sure we don't have a template here.
2628
2629 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002630 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002631 }
2632
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002633 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002634 } else {
2635 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002636 }
Mike Stump11289f42009-09-09 15:08:12 +00002637
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002638 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002639 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002640 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002641
John McCall08bede42010-05-28 08:11:17 +00002642 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002643 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002644 T.getOpenLocation(),
2645 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002646 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002647
Douglas Gregor433e0532012-04-16 18:27:27 +00002648 // C++11 [class.mem]p2:
2649 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002650 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002651 // brace-or-equal-initializers for non-static data members (including such
2652 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002653 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002654 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002655 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002656 // declarations and the lexed inline method definitions, along with any
2657 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002658 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002659 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002660 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002661
2662 // We've finished with all pending member declarations.
2663 Actions.ActOnFinishCXXMemberDecls();
2664
Richard Smith938f40b2011-06-11 17:19:42 +00002665 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002666 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002667 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002668 }
2669
John McCall08bede42010-05-28 08:11:17 +00002670 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002671 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2672 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002673
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002674 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002675 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002676 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002677}
Douglas Gregore8381c02008-11-05 04:29:56 +00002678
2679/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2680/// which explicitly initializes the members or base classes of a
2681/// class (C++ [class.base.init]). For example, the three initializers
2682/// after the ':' in the Derived constructor below:
2683///
2684/// @code
2685/// class Base { };
2686/// class Derived : Base {
2687/// int x;
2688/// float f;
2689/// public:
2690/// Derived(float f) : Base(), x(17), f(f) { }
2691/// };
2692/// @endcode
2693///
Mike Stump11289f42009-09-09 15:08:12 +00002694/// [C++] ctor-initializer:
2695/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002696///
Mike Stump11289f42009-09-09 15:08:12 +00002697/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002698/// mem-initializer ...[opt]
2699/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002700void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002701 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2702
John Wiegley1c0675e2011-04-28 01:08:34 +00002703 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2704 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002705 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002706
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002707 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002708 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002709
Douglas Gregore8381c02008-11-05 04:29:56 +00002710 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002711 if (Tok.is(tok::code_completion)) {
2712 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2713 MemInitializers.data(),
2714 MemInitializers.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002715 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002716 } else {
2717 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2718 if (!MemInit.isInvalid())
2719 MemInitializers.push_back(MemInit.get());
2720 else
2721 AnyErrors = true;
2722 }
2723
Douglas Gregore8381c02008-11-05 04:29:56 +00002724 if (Tok.is(tok::comma))
2725 ConsumeToken();
2726 else if (Tok.is(tok::l_brace))
2727 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002728 // If the next token looks like a base or member initializer, assume that
2729 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002730 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2731 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2732 Diag(Loc, diag::err_ctor_init_missing_comma)
2733 << FixItHint::CreateInsertion(Loc, ", ");
2734 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002735 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00002736 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00002737 SkipUntil(tok::l_brace, true, true);
2738 break;
2739 }
2740 } while (true);
2741
David Blaikie3fc2f912013-01-17 05:26:25 +00002742 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002743 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002744}
2745
2746/// ParseMemInitializer - Parse a C++ member initializer, which is
2747/// part of a constructor initializer that explicitly initializes one
2748/// member or base class (C++ [class.base.init]). See
2749/// ParseConstructorInitializer for an example.
2750///
2751/// [C++] mem-initializer:
2752/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002753/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002754///
Douglas Gregore8381c02008-11-05 04:29:56 +00002755/// [C++] mem-initializer-id:
2756/// '::'[opt] nested-name-specifier[opt] class-name
2757/// identifier
John McCall48871652010-08-21 09:40:31 +00002758Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002759 // parse '::'[opt] nested-name-specifier[opt]
2760 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002761 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002762 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002763 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002764 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002765 if (TemplateId->Kind == TNK_Type_template ||
2766 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002767 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002768 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002769 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002770 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002771 }
David Blaikie186a8892012-01-24 06:03:59 +00002772 // Uses of decltype will already have been converted to annot_decltype by
2773 // ParseOptionalCXXScopeSpecifier at this point.
2774 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2775 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002776 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002777 return true;
2778 }
Mike Stump11289f42009-09-09 15:08:12 +00002779
David Blaikie186a8892012-01-24 06:03:59 +00002780 IdentifierInfo *II = 0;
2781 DeclSpec DS(AttrFactory);
2782 SourceLocation IdLoc = Tok.getLocation();
2783 if (Tok.is(tok::annot_decltype)) {
2784 // Get the decltype expression, if there is one.
2785 ParseDecltypeSpecifier(DS);
2786 } else {
2787 if (Tok.is(tok::identifier))
2788 // Get the identifier. This may be a member name or a class name,
2789 // but we'll let the semantic analysis determine which it is.
2790 II = Tok.getIdentifierInfo();
2791 ConsumeToken();
2792 }
2793
Douglas Gregore8381c02008-11-05 04:29:56 +00002794
2795 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002796 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002797 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2798
Sebastian Redla74948d2011-09-24 17:48:25 +00002799 ExprResult InitList = ParseBraceInitializer();
2800 if (InitList.isInvalid())
2801 return true;
2802
2803 SourceLocation EllipsisLoc;
2804 if (Tok.is(tok::ellipsis))
2805 EllipsisLoc = ConsumeToken();
2806
2807 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002808 TemplateTypeTy, DS, IdLoc,
2809 InitList.take(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002810 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002811 BalancedDelimiterTracker T(*this, tok::l_paren);
2812 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00002813
Sebastian Redl3da34892011-06-05 12:23:16 +00002814 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00002815 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00002816 CommaLocsTy CommaLocs;
2817 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2818 SkipUntil(tok::r_paren);
2819 return true;
2820 }
2821
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002822 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00002823
2824 SourceLocation EllipsisLoc;
2825 if (Tok.is(tok::ellipsis))
2826 EllipsisLoc = ConsumeToken();
2827
2828 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002829 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00002830 T.getOpenLocation(), ArgExprs,
2831 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002832 }
2833
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002834 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redl3da34892011-06-05 12:23:16 +00002835 : diag::err_expected_lparen);
2836 return true;
Douglas Gregore8381c02008-11-05 04:29:56 +00002837}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002838
Sebastian Redl965b0e32011-03-05 14:45:16 +00002839/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002840///
Douglas Gregor356513d2008-12-01 18:00:20 +00002841/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002842/// dynamic-exception-specification
2843/// noexcept-specification
2844///
2845/// noexcept-specification:
2846/// 'noexcept'
2847/// 'noexcept' '(' constant-expression ')'
2848ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00002849Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00002850 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002851 SmallVectorImpl<ParsedType> &DynamicExceptions,
2852 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00002853 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002854 ExceptionSpecificationType Result = EST_None;
2855
2856 // See if there's a dynamic specification.
2857 if (Tok.is(tok::kw_throw)) {
2858 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2859 DynamicExceptions,
2860 DynamicExceptionRanges);
2861 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2862 "Produced different number of exception types and ranges.");
2863 }
2864
2865 // If there's no noexcept specification, we're done.
2866 if (Tok.isNot(tok::kw_noexcept))
2867 return Result;
2868
Richard Smithb15c11c2011-10-17 23:06:20 +00002869 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2870
Sebastian Redl965b0e32011-03-05 14:45:16 +00002871 // If we already had a dynamic specification, parse the noexcept for,
2872 // recovery, but emit a diagnostic and don't store the results.
2873 SourceRange NoexceptRange;
2874 ExceptionSpecificationType NoexceptType = EST_None;
2875
2876 SourceLocation KeywordLoc = ConsumeToken();
2877 if (Tok.is(tok::l_paren)) {
2878 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002879 BalancedDelimiterTracker T(*this, tok::l_paren);
2880 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002881 NoexceptType = EST_ComputedNoexcept;
2882 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002883 // The argument must be contextually convertible to bool. We use
2884 // ActOnBooleanCondition for this purpose.
2885 if (!NoexceptExpr.isInvalid())
2886 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2887 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002888 T.consumeClose();
2889 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002890 } else {
2891 // There is no argument.
2892 NoexceptType = EST_BasicNoexcept;
2893 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2894 }
2895
2896 if (Result == EST_None) {
2897 SpecificationRange = NoexceptRange;
2898 Result = NoexceptType;
2899
2900 // If there's a dynamic specification after a noexcept specification,
2901 // parse that and ignore the results.
2902 if (Tok.is(tok::kw_throw)) {
2903 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2904 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2905 DynamicExceptionRanges);
2906 }
2907 } else {
2908 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2909 }
2910
2911 return Result;
2912}
2913
Richard Smith8ca78a12013-06-13 02:02:51 +00002914static void diagnoseDynamicExceptionSpecification(
2915 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2916 if (P.getLangOpts().CPlusPlus11) {
2917 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2918 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2919 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2920 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2921 }
2922}
2923
Sebastian Redl965b0e32011-03-05 14:45:16 +00002924/// ParseDynamicExceptionSpecification - Parse a C++
2925/// dynamic-exception-specification (C++ [except.spec]).
2926///
2927/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00002928/// 'throw' '(' type-id-list [opt] ')'
2929/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00002930///
Douglas Gregor356513d2008-12-01 18:00:20 +00002931/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00002932/// type-id ... [opt]
2933/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002934///
Sebastian Redl965b0e32011-03-05 14:45:16 +00002935ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2936 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002937 SmallVectorImpl<ParsedType> &Exceptions,
2938 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002939 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00002940
Sebastian Redl965b0e32011-03-05 14:45:16 +00002941 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002942 BalancedDelimiterTracker T(*this, tok::l_paren);
2943 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002944 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2945 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002946 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002947 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002948
Douglas Gregor356513d2008-12-01 18:00:20 +00002949 // Parse throw(...), a Microsoft extension that means "this function
2950 // can throw anything".
2951 if (Tok.is(tok::ellipsis)) {
2952 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00002953 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00002954 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002955 T.consumeClose();
2956 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00002957 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002958 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00002959 }
2960
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002961 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00002962 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002963 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00002964 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00002965
Douglas Gregor830837d2010-12-20 23:57:46 +00002966 if (Tok.is(tok::ellipsis)) {
2967 // C++0x [temp.variadic]p5:
2968 // - In a dynamic-exception-specification (15.4); the pattern is a
2969 // type-id.
2970 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002971 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00002972 if (!Res.isInvalid())
2973 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2974 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00002975
Sebastian Redld6434562009-05-29 18:02:33 +00002976 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002977 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00002978 Ranges.push_back(Range);
2979 }
Douglas Gregor830837d2010-12-20 23:57:46 +00002980
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002981 if (Tok.is(tok::comma))
2982 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002983 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002984 break;
2985 }
2986
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002987 T.consumeClose();
2988 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00002989 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
2990 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002991 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002992}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002993
Douglas Gregor7fb25412010-10-01 18:44:50 +00002994/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2995/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00002996TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00002997 assert(Tok.is(tok::arrow) && "expected arrow");
2998
2999 ConsumeToken();
3000
Richard Smithbfdb1082012-03-12 08:56:40 +00003001 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003002}
3003
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003004/// \brief We have just started parsing the definition of a new class,
3005/// so push that class onto our stack of classes that is currently
3006/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003007Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003008Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3009 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003010 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003011 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003012 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003013 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003014}
3015
3016/// \brief Deallocate the given parsed class and all of its nested
3017/// classes.
3018void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003019 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3020 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003021 delete Class;
3022}
3023
3024/// \brief Pop the top class of the stack of classes that are
3025/// currently being parsed.
3026///
3027/// This routine should be called when we have finished parsing the
3028/// definition of a class, but have not yet popped the Scope
3029/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003030void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003031 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003032
John McCallc1465822011-02-14 07:13:47 +00003033 Actions.PopParsingClass(state);
3034
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003035 ParsingClass *Victim = ClassStack.top();
3036 ClassStack.pop();
3037 if (Victim->TopLevelClass) {
3038 // Deallocate all of the nested classes of this class,
3039 // recursively: we don't need to keep any of this information.
3040 DeallocateParsedClasses(Victim);
3041 return;
Mike Stump11289f42009-09-09 15:08:12 +00003042 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003043 assert(!ClassStack.empty() && "Missing top-level class?");
3044
Douglas Gregorefc46952010-10-12 16:25:54 +00003045 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003046 // The victim is a nested class, but we will not need to perform
3047 // any processing after the definition of this class since it has
3048 // no members whose handling was delayed. Therefore, we can just
3049 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003050 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003051 return;
3052 }
3053
3054 // This nested class has some members that will need to be processed
3055 // after the top-level class is completely defined. Therefore, add
3056 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003057 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003058 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003059 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003060}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003061
Richard Smith3dff2512012-04-10 03:25:07 +00003062/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3063///
3064/// \return the parsed identifier on success, and 0 if the next token is not an
3065/// attribute-token.
3066///
3067/// C++11 [dcl.attr.grammar]p3:
3068/// If a keyword or an alternative token that satisfies the syntactic
3069/// requirements of an identifier is contained in an attribute-token,
3070/// it is considered an identifier.
3071IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3072 switch (Tok.getKind()) {
3073 default:
3074 // Identifiers and keywords have identifier info attached.
3075 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3076 Loc = ConsumeToken();
3077 return II;
3078 }
3079 return 0;
3080
3081 case tok::ampamp: // 'and'
3082 case tok::pipe: // 'bitor'
3083 case tok::pipepipe: // 'or'
3084 case tok::caret: // 'xor'
3085 case tok::tilde: // 'compl'
3086 case tok::amp: // 'bitand'
3087 case tok::ampequal: // 'and_eq'
3088 case tok::pipeequal: // 'or_eq'
3089 case tok::caretequal: // 'xor_eq'
3090 case tok::exclaim: // 'not'
3091 case tok::exclaimequal: // 'not_eq'
3092 // Alternative tokens do not have identifier info, but their spelling
3093 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003094 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003095 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003096 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003097 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003098 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003099 }
3100 return 0;
3101 }
3102}
3103
Michael Han23214e52012-10-03 01:56:22 +00003104static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3105 IdentifierInfo *ScopeName) {
3106 switch (AttributeList::getKind(AttrName, ScopeName,
3107 AttributeList::AS_CXX11)) {
3108 case AttributeList::AT_CarriesDependency:
3109 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003110 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003111 return true;
3112 }
3113
3114 default:
3115 return false;
3116 }
3117}
3118
Richard Smith3dff2512012-04-10 03:25:07 +00003119/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003120/// only parses standard attributes.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003121///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003122/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003123/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003124/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003125///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003126/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003127/// attribute[opt]
3128/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003129/// attribute '...'
3130/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003131///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003132/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003133/// attribute-token attribute-argument-clause[opt]
3134///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003135/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003136/// identifier
3137/// attribute-scoped-token
3138///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003139/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003140/// attribute-namespace '::' identifier
3141///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003142/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003143/// identifier
3144///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003145/// [C++11] attribute-argument-clause:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003146/// '(' balanced-token-seq ')'
3147///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003148/// [C++11] balanced-token-seq:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003149/// balanced-token
3150/// balanced-token-seq balanced-token
3151///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003152/// [C++11] balanced-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003153/// '(' balanced-token-seq ')'
3154/// '[' balanced-token-seq ']'
3155/// '{' balanced-token-seq '}'
3156/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smith3dff2512012-04-10 03:25:07 +00003157void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003158 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003159 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003160 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003161 ParseAlignmentSpecifier(attrs, endLoc);
3162 return;
3163 }
3164
Alexis Hunt96d5c762009-11-21 08:43:09 +00003165 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003166 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003167
Richard Smithf679b5b2011-10-14 20:48:27 +00003168 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3169
Alexis Hunt96d5c762009-11-21 08:43:09 +00003170 ConsumeBracket();
3171 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003172
Richard Smith10876ef2013-01-17 01:30:42 +00003173 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3174
Richard Smith3dff2512012-04-10 03:25:07 +00003175 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003176 // attribute not present
3177 if (Tok.is(tok::comma)) {
3178 ConsumeToken();
3179 continue;
3180 }
3181
Richard Smith3dff2512012-04-10 03:25:07 +00003182 SourceLocation ScopeLoc, AttrLoc;
3183 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3184
3185 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3186 if (!AttrName)
3187 // Break out to the "expected ']'" diagnostic.
3188 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003189
Alexis Hunt96d5c762009-11-21 08:43:09 +00003190 // scoped attribute
3191 if (Tok.is(tok::coloncolon)) {
3192 ConsumeToken();
3193
Richard Smith3dff2512012-04-10 03:25:07 +00003194 ScopeName = AttrName;
3195 ScopeLoc = AttrLoc;
3196
3197 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3198 if (!AttrName) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003199 Diag(Tok.getLocation(), diag::err_expected_ident);
3200 SkipUntil(tok::r_square, tok::comma, true, true);
3201 continue;
3202 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003203 }
3204
Michael Han23214e52012-10-03 01:56:22 +00003205 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003206 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003207
Richard Smith10876ef2013-01-17 01:30:42 +00003208 if (StandardAttr &&
3209 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3210 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3211 << AttrName << SourceRange(SeenAttrs[AttrName]);
3212
Michael Han23214e52012-10-03 01:56:22 +00003213 // Parse attribute arguments
3214 if (Tok.is(tok::l_paren)) {
3215 if (ScopeName && ScopeName->getName() == "gnu") {
3216 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3217 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3218 AttrParsed = true;
3219 } else {
3220 if (StandardAttr)
3221 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3222 << AttrName->getName();
3223
3224 // FIXME: handle other formats of c++11 attribute arguments
3225 ConsumeParen();
3226 SkipUntil(tok::r_paren, false);
3227 }
3228 }
3229
3230 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003231 attrs.addNew(AttrName,
3232 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3233 AttrLoc),
3234 ScopeName, ScopeLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00003235 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003236
Richard Smith3dff2512012-04-10 03:25:07 +00003237 if (Tok.is(tok::ellipsis)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003238 ConsumeToken();
Michael Han23214e52012-10-03 01:56:22 +00003239
3240 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3241 << AttrName->getName();
Richard Smith3dff2512012-04-10 03:25:07 +00003242 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003243 }
3244
3245 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3246 SkipUntil(tok::r_square, false);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003247 if (endLoc)
3248 *endLoc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003249 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3250 SkipUntil(tok::r_square, false);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003251}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003252
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003253/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003254///
3255/// attribute-specifier-seq:
3256/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003257void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003258 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003259 assert(getLangOpts().CPlusPlus11);
3260
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003261 SourceLocation StartLoc = Tok.getLocation(), Loc;
3262 if (!endLoc)
3263 endLoc = &Loc;
3264
Douglas Gregor6f981002011-10-07 20:35:25 +00003265 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003266 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003267 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003268
3269 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003270}
3271
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003272/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3273///
3274/// [MS] ms-attribute:
3275/// '[' token-seq ']'
3276///
3277/// [MS] ms-attribute-seq:
3278/// ms-attribute[opt]
3279/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003280void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3281 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003282 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3283
3284 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003285 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003286 ConsumeBracket();
3287 SkipUntil(tok::r_square, true, true);
John McCall53fa7142010-12-24 02:08:15 +00003288 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003289 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3290 }
3291}
Francois Pichet8f981d52011-05-25 10:19:49 +00003292
3293void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3294 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003295 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003296 if (ParseMicrosoftIfExistsCondition(Result))
3297 return;
3298
Douglas Gregor43edb322011-10-24 22:31:10 +00003299 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3300 if (Braces.consumeOpen()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003301 Diag(Tok, diag::err_expected_lbrace);
3302 return;
3303 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003304
Douglas Gregor43edb322011-10-24 22:31:10 +00003305 switch (Result.Behavior) {
3306 case IEB_Parse:
3307 // Parse the declarations below.
3308 break;
3309
3310 case IEB_Dependent:
3311 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3312 << Result.IsIfExists;
3313 // Fall through to skip.
3314
3315 case IEB_Skip:
3316 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003317 return;
3318 }
3319
Douglas Gregor43edb322011-10-24 22:31:10 +00003320 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003321 // __if_exists, __if_not_exists can nest.
3322 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3323 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3324 continue;
3325 }
3326
3327 // Check for extraneous top-level semicolon.
3328 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003329 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003330 continue;
3331 }
3332
3333 AccessSpecifier AS = getAccessSpecifierIfPresent();
3334 if (AS != AS_none) {
3335 // Current token is a C++ access specifier.
3336 CurAS = AS;
3337 SourceLocation ASLoc = Tok.getLocation();
3338 ConsumeToken();
3339 if (Tok.is(tok::colon))
3340 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3341 else
3342 Diag(Tok, diag::err_expected_colon);
3343 ConsumeToken();
3344 continue;
3345 }
3346
3347 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003348 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet8f981d52011-05-25 10:19:49 +00003349 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003350
3351 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003352}