blob: 32e151cf2c29eb240a33040cad920b3407c9d8d4 [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"
Larisse Voufo39a1e502013-08-06 01:03:05 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner60f36222009-01-29 05:15:15 +000019#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
John McCalldb632ac2012-09-25 07:32:39 +000024#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Chris Lattnera5235172007-08-25 06:57:03 +000026using namespace clang;
27
28/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-08-27 23:12:46 +000029/// may either be a top level namespace or a block-level namespace alias. If
30/// there was an inline keyword, it has already been parsed.
Chris Lattnera5235172007-08-25 06:57:03 +000031///
32/// namespace-definition: [C++ 7.3: basic.namespace]
33/// named-namespace-definition
34/// unnamed-namespace-definition
35///
36/// unnamed-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000037/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000038///
39/// named-namespace-definition:
40/// original-namespace-definition
41/// extension-namespace-definition
42///
43/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' identifier attributes[opt]
45/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000046///
47/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000048/// 'inline'[opt] 'namespace' original-namespace-name
49/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000050///
Chris Lattnera5235172007-08-25 06:57:03 +000051/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
52/// 'namespace' identifier '=' qualified-namespace-specifier ';'
53///
John McCall48871652010-08-21 09:40:31 +000054Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000055 SourceLocation &DeclEnd,
56 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000057 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000058 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000059 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000060
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000061 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000062 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000063 cutOffParsing();
64 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000065 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000066
Chris Lattnera5235172007-08-25 06:57:03 +000067 SourceLocation IdentLoc;
68 IdentifierInfo *Ident = 0;
Richard Trieu61384cb2011-05-26 20:11:09 +000069 std::vector<SourceLocation> ExtraIdentLoc;
70 std::vector<IdentifierInfo*> ExtraIdent;
71 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000072
73 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000074
Chris Lattner76c72282007-10-09 17:33:22 +000075 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000076 Ident = Tok.getIdentifierInfo();
77 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-05-26 20:11:09 +000078 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
79 ExtraNamespaceLoc.push_back(ConsumeToken());
80 ExtraIdent.push_back(Tok.getIdentifierInfo());
81 ExtraIdentLoc.push_back(ConsumeToken());
82 }
Chris Lattnera5235172007-08-25 06:57:03 +000083 }
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattnera5235172007-08-25 06:57:03 +000085 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000086 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000087 if (Tok.is(tok::kw___attribute)) {
88 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000089 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000090 }
Mike Stump11289f42009-09-09 15:08:12 +000091
Douglas Gregor6b6bba42009-06-17 19:49:00 +000092 if (Tok.is(tok::equal)) {
Nico Weber729f1e22012-10-27 23:44:27 +000093 if (Ident == 0) {
94 Diag(Tok, diag::err_expected_ident);
95 // Skip to end of the definition and eat the ';'.
96 SkipUntil(tok::semi);
97 return 0;
98 }
John McCall53fa7142010-12-24 02:08:15 +000099 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000100 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000101 if (InlineLoc.isValid())
102 Diag(InlineLoc, diag::err_inline_namespace_alias)
103 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000104 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000105 }
Mike Stump11289f42009-09-09 15:08:12 +0000106
Richard Trieu61384cb2011-05-26 20:11:09 +0000107
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000108 BalancedDelimiterTracker T(*this, tok::l_brace);
109 if (T.consumeOpen()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000110 if (!ExtraIdent.empty()) {
111 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
112 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
113 }
Mike Stump11289f42009-09-09 15:08:12 +0000114 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner4de55aa2009-03-29 14:02:43 +0000115 diag::err_expected_ident_lbrace);
John McCall48871652010-08-21 09:40:31 +0000116 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000117 }
Mike Stump11289f42009-09-09 15:08:12 +0000118
Douglas Gregor0be31a22010-07-02 17:43:08 +0000119 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
120 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
121 getCurScope()->getFnParent()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000122 if (!ExtraIdent.empty()) {
123 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
124 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
125 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000126 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000127 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000128 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000129 }
130
Richard Trieu61384cb2011-05-26 20:11:09 +0000131 if (!ExtraIdent.empty()) {
132 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000133 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000134 Token rBraceToken = Tok;
135 TPA.Revert();
136
137 if (!rBraceToken.is(tok::r_brace)) {
138 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
139 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
140 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000141 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000142 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
143 E = ExtraIdent.end(); I != E; ++I) {
144 NamespaceFix += " { namespace ";
145 NamespaceFix += (*I)->getName();
146 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000147
Richard Trieu61384cb2011-05-26 20:11:09 +0000148 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000149 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000150 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000151
Richard Trieu61384cb2011-05-26 20:11:09 +0000152 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
153 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
154 ExtraIdentLoc.back()),
155 NamespaceFix)
156 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
157 }
158 }
159
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000160 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000161 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000162 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000163 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000164
Chris Lattner4de55aa2009-03-29 14:02:43 +0000165 // Enter a scope for the namespace.
166 ParseScope NamespaceScope(this, Scope::DeclScope);
167
John McCall48871652010-08-21 09:40:31 +0000168 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000169 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000170 IdentLoc, Ident, T.getOpenLocation(),
171 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000172
John McCallfaf5fb42010-08-26 23:41:50 +0000173 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
174 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000175
Richard Trieu61384cb2011-05-26 20:11:09 +0000176 // Parse the contents of the namespace. This includes parsing recovery on
177 // any improperly nested namespaces.
178 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000179 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000180
Chris Lattner4de55aa2009-03-29 14:02:43 +0000181 // Leave the namespace scope.
182 NamespaceScope.Exit();
183
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000184 DeclEnd = T.getCloseLocation();
185 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000186
187 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000188}
Chris Lattner38376f12008-01-12 07:05:38 +0000189
Richard Trieu61384cb2011-05-26 20:11:09 +0000190/// ParseInnerNamespace - Parse the contents of a namespace.
191void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
192 std::vector<IdentifierInfo*>& Ident,
193 std::vector<SourceLocation>& NamespaceLoc,
194 unsigned int index, SourceLocation& InlineLoc,
Richard Trieu61384cb2011-05-26 20:11:09 +0000195 ParsedAttributes& attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000196 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000197 if (index == Ident.size()) {
Richard Smith34f30512013-11-23 04:06:09 +0000198 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000199 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000200 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000201 MaybeParseMicrosoftAttributes(attrs);
202 ParseExternalDeclaration(attrs);
203 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000204
205 // The caller is what called check -- we are simply calling
206 // the close for it.
207 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000208
209 return;
210 }
211
212 // Parse improperly nested namespaces.
213 ParseScope NamespaceScope(this, Scope::DeclScope);
214 Decl *NamespcDecl =
215 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
216 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000217 Ident[index], Tracker.getOpenLocation(),
218 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000219
220 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000221 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000222
223 NamespaceScope.Exit();
224
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000225 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000226}
227
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000228/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
229/// alias definition.
230///
John McCall48871652010-08-21 09:40:31 +0000231Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000232 SourceLocation AliasLoc,
233 IdentifierInfo *Alias,
234 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000235 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000236
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000237 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000238
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000239 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000240 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000241 cutOffParsing();
242 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000243 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000244
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000245 CXXScopeSpec SS;
246 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000247 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000248
249 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
250 Diag(Tok, diag::err_expected_namespace_name);
251 // Skip to end of the definition and eat the ';'.
252 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000253 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000254 }
255
256 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000257 IdentifierInfo *Ident = Tok.getIdentifierInfo();
258 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000259
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000260 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000261 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000262 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
263 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000264
Douglas Gregor0be31a22010-07-02 17:43:08 +0000265 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000266 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000267}
268
Chris Lattner38376f12008-01-12 07:05:38 +0000269/// ParseLinkage - We know that the current token is a string_literal
270/// and just before that, that extern was seen.
271///
272/// linkage-specification: [C++ 7.5p2: dcl.link]
273/// 'extern' string-literal '{' declaration-seq[opt] '}'
274/// 'extern' string-literal declaration
275///
Chris Lattner8ea64422010-11-09 20:15:55 +0000276Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000277 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000278 SmallString<8> LangBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000279 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000280 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000281 if (Invalid)
John McCall48871652010-08-21 09:40:31 +0000282 return 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000283
Richard Smithd67aea22012-03-06 03:21:47 +0000284 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
285 // phase 7, so string-literal concatenation is supposed to occur.
286 // extern "" "C" "" "+" "+" { } is legal.
287 if (Tok.hasUDSuffix())
288 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattner38376f12008-01-12 07:05:38 +0000289 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000290
Douglas Gregor07665a62009-01-05 19:45:36 +0000291 ParseScope LinkageScope(this, Scope::DeclScope);
John McCall48871652010-08-21 09:40:31 +0000292 Decl *LinkageSpec
Douglas Gregor0be31a22010-07-02 17:43:08 +0000293 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000294 DS.getSourceRange().getBegin(),
Benjamin Kramerbebee842010-05-03 13:08:54 +0000295 Loc, Lang,
Abramo Bagnaraea947882011-03-08 16:41:52 +0000296 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000297 : SourceLocation());
298
John McCall084e83d2011-03-24 11:26:52 +0000299 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000300 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000301 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000302
Douglas Gregor07665a62009-01-05 19:45:36 +0000303 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000304 // Reset the source range in DS, as the leading "extern"
305 // does not really belong to the inner declaration ...
306 DS.SetRangeStart(SourceLocation());
307 DS.SetRangeEnd(SourceLocation());
308 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000309 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000310 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000311 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000312 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000313 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000314
Douglas Gregorb65a9132010-02-07 08:38:28 +0000315 DS.abort();
316
John McCall53fa7142010-12-24 02:08:15 +0000317 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000318
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000319 BalancedDelimiterTracker T(*this, tok::l_brace);
320 T.consumeOpen();
Richard Smith34f30512013-11-23 04:06:09 +0000321 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
John McCall084e83d2011-03-24 11:26:52 +0000322 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000323 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000324 MaybeParseMicrosoftAttributes(attrs);
325 ParseExternalDeclaration(attrs);
Chris Lattner38376f12008-01-12 07:05:38 +0000326 }
327
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000328 T.consumeClose();
Chris Lattner8ea64422010-11-09 20:15:55 +0000329 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000330 T.getCloseLocation());
Chris Lattner38376f12008-01-12 07:05:38 +0000331}
Douglas Gregor556877c2008-04-13 21:30:24 +0000332
Douglas Gregord7c4d982008-12-30 03:27:21 +0000333/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
334/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000335Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000336 const ParsedTemplateInfo &TemplateInfo,
337 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000338 ParsedAttributesWithRange &attrs,
339 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000340 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000341 ObjCDeclContextSwitch ObjCDC(*this);
342
Douglas Gregord7c4d982008-12-30 03:27:21 +0000343 // Eat 'using'.
344 SourceLocation UsingLoc = ConsumeToken();
345
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000346 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000347 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000348 cutOffParsing();
349 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000350 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000351
John McCall9b72f892010-11-10 02:40:36 +0000352 // 'using namespace' means this is a using-directive.
353 if (Tok.is(tok::kw_namespace)) {
354 // Template parameters are always an error here.
355 if (TemplateInfo.Kind) {
356 SourceRange R = TemplateInfo.getSourceRange();
357 Diag(UsingLoc, diag::err_templated_using_directive)
358 << R << FixItHint::CreateRemoval(R);
359 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000360
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000361 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000362 }
363
Richard Smithdda56e42011-04-15 14:24:37 +0000364 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000365
366 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000367 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000368
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000369 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000370 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000371}
372
373/// ParseUsingDirective - Parse C++ using-directive, assumes
374/// that current token is 'namespace' and 'using' was already parsed.
375///
376/// using-directive: [C++ 7.3.p4: namespace.udir]
377/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
378/// namespace-name ;
379/// [GNU] using-directive:
380/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
381/// namespace-name attributes[opt] ;
382///
John McCall48871652010-08-21 09:40:31 +0000383Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000384 SourceLocation UsingLoc,
385 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000386 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000387 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
388
389 // Eat 'namespace'.
390 SourceLocation NamespcLoc = ConsumeToken();
391
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000392 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000393 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000394 cutOffParsing();
395 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000396 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000397
Douglas Gregord7c4d982008-12-30 03:27:21 +0000398 CXXScopeSpec SS;
399 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000400 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000401
Douglas Gregord7c4d982008-12-30 03:27:21 +0000402 IdentifierInfo *NamespcName = 0;
403 SourceLocation IdentLoc = SourceLocation();
404
405 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000406 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000407 Diag(Tok, diag::err_expected_namespace_name);
408 // If there was invalid namespace name, skip to end of decl, and eat ';'.
409 SkipUntil(tok::semi);
410 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCall48871652010-08-21 09:40:31 +0000411 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000414 // Parse identifier.
415 NamespcName = Tok.getIdentifierInfo();
416 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000418 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000419 bool GNUAttr = false;
420 if (Tok.is(tok::kw___attribute)) {
421 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000422 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000425 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000426 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000427 ExpectAndConsume(tok::semi,
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000428 GNUAttr ? diag::err_expected_semi_after_attribute_list
429 : diag::err_expected_semi_after_namespace_name,
430 "", tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000431
Douglas Gregor0be31a22010-07-02 17:43:08 +0000432 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000433 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000434}
435
Richard Smithdda56e42011-04-15 14:24:37 +0000436/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
437/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000438///
439/// using-declaration: [C++ 7.3.p3: namespace.udecl]
440/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000441/// unqualified-id
442/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000443///
Richard Smith810ad3e2013-01-29 10:02:16 +0000444/// alias-declaration: C++11 [dcl.dcl]p1
445/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000446///
John McCall48871652010-08-21 09:40:31 +0000447Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000448 const ParsedTemplateInfo &TemplateInfo,
449 SourceLocation UsingLoc,
450 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000451 AccessSpecifier AS,
452 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000453 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000454 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000455 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000456
Richard Smithc2c8bb82013-10-15 01:34:54 +0000457 // Check for misplaced attributes before the identifier in an
458 // alias-declaration.
459 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
460 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000461
462 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000463 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000464 if (Tok.is(tok::kw_typename)) {
Richard Smith54ecd982013-02-20 19:22:51 +0000465 TypenameLoc = ConsumeToken();
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000466 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000467 }
Douglas Gregorfec52632009-06-20 00:51:54 +0000468
469 // Parse nested-name-specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000470 IdentifierInfo *LastII = 0;
471 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
472 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
473 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000474
Douglas Gregorfec52632009-06-20 00:51:54 +0000475 // Check nested-name specifier.
476 if (SS.isInvalid()) {
477 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000478 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000479 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000480
Richard Smith7447af42013-03-26 01:15:19 +0000481 SourceLocation TemplateKWLoc;
482 UnqualifiedId Name;
483
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000484 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000485 // destructor names and allow the action module to diagnose any semantic
486 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000487 //
488 // C++11 [class.qual]p2:
489 // [...] in a using-declaration that is a member-declaration, if the name
490 // specified after the nested-name-specifier is the same as the identifier
491 // or the simple-template-id's template-name in the last component of the
492 // nested-name-specifier, the name is [...] considered to name the
493 // constructor.
494 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
495 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
496 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
497 !SS.getScopeRep()->getAsNamespace() &&
498 !SS.getScopeRep()->getAsNamespaceAlias()) {
499 SourceLocation IdLoc = ConsumeToken();
500 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
501 Name.setConstructorName(Type, IdLoc, IdLoc);
502 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
503 /*AllowDestructorName=*/ true,
504 /*AllowConstructorName=*/ true, ParsedType(),
505 TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000506 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000507 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000508 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000509
Richard Smithc2c8bb82013-10-15 01:34:54 +0000510 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000511 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000512 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000513
514 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000515 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000516 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smithdda56e42011-04-15 14:24:37 +0000517 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000518 // If we had any misplaced attributes from earlier, this is where they
519 // should have been written.
520 if (MisplacedAttrs.Range.isValid()) {
521 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
522 << FixItHint::CreateInsertionFromRange(
523 Tok.getLocation(),
524 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
525 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
526 Attrs.takeAllFrom(MisplacedAttrs);
527 }
528
Richard Smithdda56e42011-04-15 14:24:37 +0000529 ConsumeToken();
530
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000531 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000532 diag::warn_cxx98_compat_alias_declaration :
533 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000534
Richard Smith3f1b5d02011-05-05 21:57:07 +0000535 // Type alias templates cannot be specialized.
536 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000537 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
538 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000539 SpecKind = 0;
540 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
541 SpecKind = 1;
542 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
543 SpecKind = 2;
544 if (SpecKind != -1) {
545 SourceRange Range;
546 if (SpecKind == 0)
547 Range = SourceRange(Name.TemplateId->LAngleLoc,
548 Name.TemplateId->RAngleLoc);
549 else
550 Range = TemplateInfo.getSourceRange();
551 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
552 << SpecKind << Range;
553 SkipUntil(tok::semi);
554 return 0;
555 }
556
Richard Smithdda56e42011-04-15 14:24:37 +0000557 // Name must be an identifier.
558 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
559 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
560 // No removal fixit: can't recover from this.
561 SkipUntil(tok::semi);
562 return 0;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000563 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000564 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
565 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
566 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
567 else if (SS.isNotEmpty())
568 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
569 << FixItHint::CreateRemoval(SS.getRange());
570
Richard Smith3f1b5d02011-05-05 21:57:07 +0000571 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
572 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000573 Declarator::AliasDeclContext, AS, OwnedType,
574 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000575 } else {
576 // C++11 attributes are not allowed on a using-declaration, but GNU ones
577 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000578 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000579 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000580
Richard Smithdda56e42011-04-15 14:24:37 +0000581 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000582 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000583 }
Mike Stump11289f42009-09-09 15:08:12 +0000584
Douglas Gregorfec52632009-06-20 00:51:54 +0000585 // Eat ';'.
586 DeclEnd = Tok.getLocation();
587 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith54ecd982013-02-20 19:22:51 +0000588 !Attrs.empty() ? "attributes list" :
Richard Smithdda56e42011-04-15 14:24:37 +0000589 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor220f4272009-11-04 16:30:06 +0000590 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000591
John McCall9b72f892010-11-10 02:40:36 +0000592 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000593 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000594 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000595 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000596 SourceRange R = TemplateInfo.getSourceRange();
597 Diag(UsingLoc, diag::err_templated_using_declaration)
598 << R << FixItHint::CreateRemoval(R);
599
600 // Unfortunately, we have to bail out instead of recovering by
601 // ignoring the parameters, just in case the nested name specifier
602 // depends on the parameters.
603 return 0;
604 }
605
Douglas Gregor882a61a2011-09-26 14:30:28 +0000606 // "typename" keyword is allowed for identifiers only,
607 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000608 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000609 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
610 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000611 // Proceed parsing, but reset the HasTypenameKeyword flag.
612 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000613 }
614
Richard Smith3f1b5d02011-05-05 21:57:07 +0000615 if (IsAliasDecl) {
616 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000617 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3f1b5d02011-05-05 21:57:07 +0000618 TemplateParams ? TemplateParams->data() : 0,
619 TemplateParams ? TemplateParams->size() : 0);
620 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000621 UsingLoc, Name, Attrs.getList(),
622 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000623 }
Richard Smithdda56e42011-04-15 14:24:37 +0000624
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000625 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
626 /* HasUsingKeyword */ true, UsingLoc,
627 SS, Name, Attrs.getList(),
628 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000629}
630
Benjamin Kramere56f3932011-12-23 17:00:35 +0000631/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000632///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000633/// [C++0x] static_assert-declaration:
634/// static_assert ( constant-expression , string-literal ) ;
635///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000636/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000637/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000638///
John McCall48871652010-08-21 09:40:31 +0000639Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000640 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
641 "Not a static_assert declaration");
642
David Blaikiebbafb8a2012-03-11 07:00:24 +0000643 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000644 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000645 if (Tok.is(tok::kw_static_assert))
646 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000647
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000648 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000649
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000650 BalancedDelimiterTracker T(*this, tok::l_paren);
651 if (T.consumeOpen()) {
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000652 Diag(Tok, diag::err_expected_lparen);
Richard Smith76965712012-09-13 19:12:50 +0000653 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000654 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000655 }
Mike Stump11289f42009-09-09 15:08:12 +0000656
John McCalldadc5752010-08-24 06:29:42 +0000657 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000658 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000659 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000660 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000661 }
Mike Stump11289f42009-09-09 15:08:12 +0000662
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000663 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCall48871652010-08-21 09:40:31 +0000664 return 0;
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000665
Richard Smithf506eaf2012-03-05 23:20:05 +0000666 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000667 Diag(Tok, diag::err_expected_string_literal)
668 << /*Source='static_assert'*/1;
Richard Smith76965712012-09-13 19:12:50 +0000669 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000670 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000671 }
Mike Stump11289f42009-09-09 15:08:12 +0000672
John McCalldadc5752010-08-24 06:29:42 +0000673 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smithd67aea22012-03-06 03:21:47 +0000674 if (AssertMessage.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000675 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000676 return 0;
Richard Smithd67aea22012-03-06 03:21:47 +0000677 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000678
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000679 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattner49836b42009-04-02 04:16:50 +0000681 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000682 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000683
John McCallb268a282010-08-23 23:25:46 +0000684 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
685 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000686 AssertMessage.take(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000687 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000688}
689
Richard Smith74aeef52013-04-26 16:15:35 +0000690/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000691///
692/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000693/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000694///
David Blaikie15a430a2011-12-04 05:04:18 +0000695SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
696 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
697 && "Not a decltype specifier");
698
David Blaikie15a430a2011-12-04 05:04:18 +0000699 ExprResult Result;
700 SourceLocation StartLoc = Tok.getLocation();
701 SourceLocation EndLoc;
702
703 if (Tok.is(tok::annot_decltype)) {
704 Result = getExprAnnotation(Tok);
705 EndLoc = Tok.getAnnotationEndLoc();
706 ConsumeToken();
707 if (Result.isInvalid()) {
708 DS.SetTypeSpecError();
709 return EndLoc;
710 }
711 } else {
Richard Smith324df552012-02-24 22:30:04 +0000712 if (Tok.getIdentifierInfo()->isStr("decltype"))
713 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000714
David Blaikie15a430a2011-12-04 05:04:18 +0000715 ConsumeToken();
716
717 BalancedDelimiterTracker T(*this, tok::l_paren);
718 if (T.expectAndConsume(diag::err_expected_lparen_after,
719 "decltype", tok::r_paren)) {
720 DS.SetTypeSpecError();
721 return T.getOpenLocation() == Tok.getLocation() ?
722 StartLoc : T.getOpenLocation();
723 }
724
Richard Smith74aeef52013-04-26 16:15:35 +0000725 // Check for C++1y 'decltype(auto)'.
726 if (Tok.is(tok::kw_auto)) {
727 // No need to disambiguate here: an expression can't start with 'auto',
728 // because the typename-specifier in a function-style cast operation can't
729 // be 'auto'.
730 Diag(Tok.getLocation(),
731 getLangOpts().CPlusPlus1y
732 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
733 : diag::ext_decltype_auto_type_specifier);
734 ConsumeToken();
735 } else {
736 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000737
Richard Smith74aeef52013-04-26 16:15:35 +0000738 // C++11 [dcl.type.simple]p4:
739 // The operand of the decltype specifier is an unevaluated operand.
740 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
741 0, /*IsDecltype=*/true);
742 Result = ParseExpression();
743 if (Result.isInvalid()) {
744 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000745 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000746 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000747 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000748 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
749 // Backtrack to get the location of the last token before the semi.
750 PP.RevertCachedTokens(2);
751 ConsumeToken(); // the semi.
752 EndLoc = ConsumeAnyToken();
753 assert(Tok.is(tok::semi));
754 } else {
755 EndLoc = Tok.getLocation();
756 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000757 }
Richard Smith74aeef52013-04-26 16:15:35 +0000758 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000759 }
Richard Smith74aeef52013-04-26 16:15:35 +0000760
761 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie15a430a2011-12-04 05:04:18 +0000762 }
763
764 // Match the ')'
765 T.consumeClose();
766 if (T.getCloseLocation().isInvalid()) {
767 DS.SetTypeSpecError();
768 // FIXME: this should return the location of the last token
769 // that was consumed (by "consumeClose()")
770 return T.getCloseLocation();
771 }
772
Richard Smithfd555f62012-02-22 02:04:18 +0000773 if (Result.isInvalid()) {
774 DS.SetTypeSpecError();
775 return T.getCloseLocation();
776 }
777
David Blaikie15a430a2011-12-04 05:04:18 +0000778 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000779 }
Richard Smith74aeef52013-04-26 16:15:35 +0000780 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000781
Anders Carlsson74948d02009-06-24 17:47:40 +0000782 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000783 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000784 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000785 if (Result.get()
786 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
787 DiagID, Result.release())
788 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
789 DiagID)) {
John McCall49bfce42009-08-03 20:12:06 +0000790 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000791 DS.SetTypeSpecError();
792 }
793 return EndLoc;
794}
795
796void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
797 SourceLocation StartLoc,
798 SourceLocation EndLoc) {
799 // make sure we have a token we can turn into an annotation token
800 if (PP.isBacktrackEnabled())
801 PP.RevertCachedTokens(1);
802 else
803 PP.EnterToken(Tok);
804
805 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000806 setExprAnnotation(Tok,
807 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
808 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
809 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000810 Tok.setAnnotationEndLoc(EndLoc);
811 Tok.setLocation(StartLoc);
812 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000813}
814
Alexis Hunt4a257072011-05-19 05:37:45 +0000815void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
816 assert(Tok.is(tok::kw___underlying_type) &&
817 "Not an underlying type specifier");
818
819 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000820 BalancedDelimiterTracker T(*this, tok::l_paren);
821 if (T.expectAndConsume(diag::err_expected_lparen_after,
822 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000823 return;
824 }
825
826 TypeResult Result = ParseTypeName();
827 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000828 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000829 return;
830 }
831
832 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000833 T.consumeClose();
834 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000835 return;
836
837 const char *PrevSpec = 0;
838 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000839 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Alexis Hunt4a257072011-05-19 05:37:45 +0000840 DiagID, Result.release()))
841 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000842 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000843}
844
David Blaikie00ee7a082011-10-25 15:01:20 +0000845/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
846/// class name or decltype-specifier. Note that we only check that the result
847/// names a type; semantic analysis will need to verify that the type names a
848/// class. The result is either a type or null, depending on whether a type
849/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000850///
Richard Smith4c96e992013-02-19 23:47:15 +0000851/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000852/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000853/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000854/// nested-name-specifier[opt] class-name
855/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000856/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000857/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000858/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000859///
Richard Smith4c96e992013-02-19 23:47:15 +0000860/// In C++98, instead of base-type-specifier, we have:
861///
862/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie1cd50022011-10-25 17:10:12 +0000863Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
864 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000865 // Ignore attempts to use typename
866 if (Tok.is(tok::kw_typename)) {
867 Diag(Tok, diag::err_expected_class_name_not_template)
868 << FixItHint::CreateRemoval(Tok.getLocation());
869 ConsumeToken();
870 }
871
David Blaikieafa155f2011-10-25 18:17:58 +0000872 // Parse optional nested-name-specifier
873 CXXScopeSpec SS;
874 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
875
876 BaseLoc = Tok.getLocation();
877
David Blaikie1cd50022011-10-25 17:10:12 +0000878 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000879 // tok == kw_decltype is just error recovery, it can only happen when SS
880 // isn't empty
881 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000882 if (SS.isNotEmpty())
883 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
884 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000885 // Fake up a Declarator to use with ActOnTypeName.
886 DeclSpec DS(AttrFactory);
887
David Blaikie7491e732011-12-08 04:53:15 +0000888 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000889
890 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
891 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
892 }
893
Douglas Gregord54dfb82009-02-25 23:52:28 +0000894 // Check whether we have a template-id that names a type.
895 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000896 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000897 if (TemplateId->Kind == TNK_Type_template ||
898 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000899 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000900
901 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000902 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000903 EndLocation = Tok.getAnnotationEndLoc();
904 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000905
906 if (Type)
907 return Type;
908 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000909 }
910
911 // Fall through to produce an error below.
912 }
913
Douglas Gregor831c93f2008-11-05 20:51:48 +0000914 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000915 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000916 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000917 }
918
Douglas Gregor18473f32010-01-12 21:28:44 +0000919 IdentifierInfo *Id = Tok.getIdentifierInfo();
920 SourceLocation IdLoc = ConsumeToken();
921
922 if (Tok.is(tok::less)) {
923 // It looks the user intended to write a template-id here, but the
924 // template-name was wrong. Try to fix that.
925 TemplateNameKind TNK = TNK_Type_template;
926 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000927 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000928 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000929 Diag(IdLoc, diag::err_unknown_template_name)
930 << Id;
931 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000932
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000933 if (!Template) {
934 TemplateArgList TemplateArgs;
935 SourceLocation LAngleLoc, RAngleLoc;
936 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
937 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000938 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000939 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000940
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000941 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000942 UnqualifiedId TemplateName;
943 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000944
Douglas Gregor18473f32010-01-12 21:28:44 +0000945 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000946 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
947 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000948 return true;
949 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000950 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000951
Douglas Gregor18473f32010-01-12 21:28:44 +0000952 // If we didn't end up with a typename token, there's nothing more we
953 // can do.
954 if (Tok.isNot(tok::annot_typename))
955 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000956
Douglas Gregor18473f32010-01-12 21:28:44 +0000957 // Retrieve the type from the annotation token, consume that token, and
958 // return.
959 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000960 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000961 ConsumeToken();
962 return Type;
963 }
964
Douglas Gregor831c93f2008-11-05 20:51:48 +0000965 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000966 IdentifierInfo *CorrectedII = 0;
Douglas Gregore7c20652011-03-02 00:47:37 +0000967 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000968 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +0000969 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000970 /*NonTrivialTypeSourceInfo=*/true,
971 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000972 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000973 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000974 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000975 }
976
977 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000978 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000979
980 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000981 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +0000982 DS.SetRangeStart(IdLoc);
983 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000984 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000985
986 const char *PrevSpec = 0;
987 unsigned DiagID;
988 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
989
990 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
991 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000992}
993
John McCall8d32c052012-05-22 21:28:12 +0000994void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
995 while (Tok.is(tok::kw___single_inheritance) ||
996 Tok.is(tok::kw___multiple_inheritance) ||
997 Tok.is(tok::kw___virtual_inheritance)) {
998 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
999 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +00001000 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
1001 AttributeList::AS_GNU);
John McCall8d32c052012-05-22 21:28:12 +00001002 }
1003}
1004
Richard Smith369b9f92012-06-25 21:37:02 +00001005/// Determine whether the following tokens are valid after a type-specifier
1006/// which could be a standalone declaration. This will conservatively return
1007/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001008bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001009 // This switch enumerates the valid "follow" set for type-specifiers.
1010 switch (Tok.getKind()) {
1011 default: break;
1012 case tok::semi: // struct foo {...} ;
1013 case tok::star: // struct foo {...} * P;
1014 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001015 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001016 case tok::identifier: // struct foo {...} V ;
1017 case tok::r_paren: //(struct foo {...} ) {4}
1018 case tok::annot_cxxscope: // struct foo {...} a:: b;
1019 case tok::annot_typename: // struct foo {...} a ::b;
1020 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1021 case tok::l_paren: // struct foo {...} ( x);
1022 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001023 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001024 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith369b9f92012-06-25 21:37:02 +00001025 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001026 case tok::colon:
1027 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001028 // Type qualifiers
1029 case tok::kw_const: // struct foo {...} const x;
1030 case tok::kw_volatile: // struct foo {...} volatile x;
1031 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001032 // Function specifiers
1033 // Note, no 'explicit'. An explicit function must be either a conversion
1034 // operator or a constructor. Either way, it can't have a return type.
1035 case tok::kw_inline: // struct foo inline f();
1036 case tok::kw_virtual: // struct foo virtual f();
1037 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001038 // Storage-class specifiers
1039 case tok::kw_static: // struct foo {...} static x;
1040 case tok::kw_extern: // struct foo {...} extern x;
1041 case tok::kw_typedef: // struct foo {...} typedef x;
1042 case tok::kw_register: // struct foo {...} register x;
1043 case tok::kw_auto: // struct foo {...} auto x;
1044 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001045 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001046 case tok::kw_constexpr: // struct foo {...} constexpr x;
1047 // As shown above, type qualifiers and storage class specifiers absolutely
1048 // can occur after class specifiers according to the grammar. However,
1049 // almost no one actually writes code like this. If we see one of these,
1050 // it is much more likely that someone missed a semi colon and the
1051 // type/storage class specifier we're seeing is part of the *next*
1052 // intended declaration, as in:
1053 //
1054 // struct foo { ... }
1055 // typedef int X;
1056 //
1057 // We'd really like to emit a missing semicolon error instead of emitting
1058 // an error on the 'int' saying that you can't have two type specifiers in
1059 // the same declaration of X. Because of this, we look ahead past this
1060 // token to see if it's a type specifier. If so, we know the code is
1061 // otherwise invalid, so we can produce the expected semi error.
1062 if (!isKnownToBeTypeSpecifier(NextToken()))
1063 return true;
1064 break;
1065 case tok::r_brace: // struct bar { struct foo {...} }
1066 // Missing ';' at end of struct is accepted as an extension in C mode.
1067 if (!getLangOpts().CPlusPlus)
1068 return true;
1069 break;
Richard Smith1ac67d12013-01-19 03:48:05 +00001070 // C++11 attributes
1071 case tok::l_square: // enum E [[]] x
1072 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1073 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith52c5b872013-01-29 04:13:32 +00001074 case tok::greater:
1075 // template<class T = class X>
1076 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001077 }
1078 return false;
1079}
1080
Douglas Gregor556877c2008-04-13 21:30:24 +00001081/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1082/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1083/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001084/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001085///
1086/// class-specifier: [C++ class]
1087/// class-head '{' member-specification[opt] '}'
1088/// class-head '{' member-specification[opt] '}' attributes[opt]
1089/// class-head:
1090/// class-key identifier[opt] base-clause[opt]
1091/// class-key nested-name-specifier identifier base-clause[opt]
1092/// class-key nested-name-specifier[opt] simple-template-id
1093/// base-clause[opt]
1094/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001095/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001096/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001097/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001098/// simple-template-id base-clause[opt]
1099/// class-key:
1100/// 'class'
1101/// 'struct'
1102/// 'union'
1103///
1104/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001105/// class-key ::[opt] nested-name-specifier[opt] identifier
1106/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1107/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001108///
1109/// Note that the C++ class-specifier and elaborated-type-specifier,
1110/// together, subsume the C99 struct-or-union-specifier:
1111///
1112/// struct-or-union-specifier: [C99 6.7.2.1]
1113/// struct-or-union identifier[opt] '{' struct-contents '}'
1114/// struct-or-union identifier
1115/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1116/// '}' attributes[opt]
1117/// [GNU] struct-or-union attributes[opt] identifier
1118/// struct-or-union:
1119/// 'struct'
1120/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001121void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1122 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001123 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001124 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001125 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001126 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001127 DeclSpec::TST TagType;
1128 if (TagTokKind == tok::kw_struct)
1129 TagType = DeclSpec::TST_struct;
1130 else if (TagTokKind == tok::kw___interface)
1131 TagType = DeclSpec::TST_interface;
1132 else if (TagTokKind == tok::kw_class)
1133 TagType = DeclSpec::TST_class;
1134 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001135 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1136 TagType = DeclSpec::TST_union;
1137 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001138
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001139 if (Tok.is(tok::code_completion)) {
1140 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001141 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001142 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001143 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001144
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001145 // C++03 [temp.explicit] 14.7.2/8:
1146 // The usual access checking rules do not apply to names used to specify
1147 // explicit instantiations.
1148 //
1149 // As an extension we do not perform access checking on the names used to
1150 // specify explicit specializations either. This is important to allow
1151 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001152 //
1153 // Note that we don't suppress if this turns out to be an elaborated
1154 // type specifier.
1155 bool shouldDelayDiagsInTag =
1156 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1157 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1158 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001159
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001160 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001161 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001162 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001163
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001164 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001165 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001166 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001167
John McCall8d32c052012-05-22 21:28:12 +00001168 // Parse inheritance specifiers.
1169 if (Tok.is(tok::kw___single_inheritance) ||
1170 Tok.is(tok::kw___multiple_inheritance) ||
1171 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001172 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001173
Alexis Hunt96d5c762009-11-21 08:43:09 +00001174 // If C++0x attributes exist here, parse them.
1175 // FIXME: Are we consistent with the ordering of parsing of different
1176 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001177 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001178
Michael Han309af292013-01-07 16:57:11 +00001179 // Source location used by FIXIT to insert misplaced
1180 // C++11 attributes
1181 SourceLocation AttrFixitLoc = Tok.getLocation();
1182
John Wiegley65497cc2011-04-27 23:09:49 +00001183 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001184 !Tok.is(tok::identifier) &&
1185 Tok.getIdentifierInfo() &&
1186 (Tok.is(tok::kw___is_arithmetic) ||
1187 Tok.is(tok::kw___is_convertible) ||
John Wiegley65497cc2011-04-27 23:09:49 +00001188 Tok.is(tok::kw___is_empty) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001189 Tok.is(tok::kw___is_floating_point) ||
1190 Tok.is(tok::kw___is_function) ||
John Wiegley65497cc2011-04-27 23:09:49 +00001191 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001192 Tok.is(tok::kw___is_integral) ||
1193 Tok.is(tok::kw___is_member_function_pointer) ||
1194 Tok.is(tok::kw___is_member_pointer) ||
1195 Tok.is(tok::kw___is_pod) ||
1196 Tok.is(tok::kw___is_pointer) ||
1197 Tok.is(tok::kw___is_same) ||
Douglas Gregor63180b12011-04-29 01:38:03 +00001198 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001199 Tok.is(tok::kw___is_signed) ||
1200 Tok.is(tok::kw___is_unsigned) ||
1201 Tok.is(tok::kw___is_void))) {
Douglas Gregordf445f02011-07-30 07:01:49 +00001202 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001203 // name of struct templates, but some are keywords in GCC >= 4.3
1204 // and Clang. Therefore, when we see the token sequence "struct
1205 // X", make X into a normal identifier rather than a keyword, to
1206 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001207 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregor119b0c72009-09-04 05:53:02 +00001208 Tok.setKind(tok::identifier);
1209 }
Mike Stump11289f42009-09-09 15:08:12 +00001210
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001211 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001212 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001213 if (getLangOpts().CPlusPlus) {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001214 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1215 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001216
Douglas Gregordf593fb2011-11-07 17:33:42 +00001217 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001218 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001219 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001220 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1221 Diag(Tok, diag::err_expected_ident);
1222 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001223
Douglas Gregor916462b2009-10-30 21:46:58 +00001224 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1225
Douglas Gregor67a65642009-02-17 23:15:12 +00001226 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +00001227 IdentifierInfo *Name = 0;
1228 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +00001229 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +00001230 if (Tok.is(tok::identifier)) {
1231 Name = Tok.getIdentifierInfo();
1232 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001233
David Blaikiebbafb8a2012-03-11 07:00:24 +00001234 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001235 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001236 // Eat the template argument list and try to continue parsing this as
1237 // a class (or template thereof).
1238 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001239 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001240 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001241 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001242 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001243 // We couldn't parse the template argument list at all, so don't
1244 // try to give any location information for the list.
1245 LAngleLoc = RAngleLoc = SourceLocation();
1246 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001247
Douglas Gregor916462b2009-10-30 21:46:58 +00001248 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matose9a3ed42012-08-31 22:18:20 +00001249 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1250 << (TagType == DeclSpec::TST_class? 0
1251 : TagType == DeclSpec::TST_struct? 1
Richard Smith68b14532013-11-08 21:51:24 +00001252 : TagType == DeclSpec::TST_union? 2
Joao Matose9a3ed42012-08-31 22:18:20 +00001253 : 3)
1254 << Name
1255 << SourceRange(LAngleLoc, RAngleLoc);
1256
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001257 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001258 // we've removed its template argument list.
1259 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1260 if (TemplateParams && TemplateParams->size() > 1) {
1261 TemplateParams->pop_back();
1262 } else {
1263 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001264 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001265 = ParsedTemplateInfo::NonTemplate;
1266 }
1267 } else if (TemplateInfo.Kind
1268 == ParsedTemplateInfo::ExplicitInstantiation) {
1269 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +00001270 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001271 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001272 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001273 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001274 = SourceLocation();
1275 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1276 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001277 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001278 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001279 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001280 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001281 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001282
Douglas Gregore7c20652011-03-02 00:47:37 +00001283 if (TemplateId->Kind != TNK_Type_template &&
1284 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001285 // The template-name in the simple-template-id refers to
1286 // something other than a class template. Give an appropriate
1287 // error message and skip to the ';'.
1288 SourceRange Range(NameLoc);
1289 if (SS.isNotEmpty())
1290 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001291
Douglas Gregor7f741122009-02-25 19:37:18 +00001292 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001293 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001294
Douglas Gregor7f741122009-02-25 19:37:18 +00001295 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001296 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001297 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001298 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001299 }
1300
Richard Smithbfdb1082012-03-12 08:56:40 +00001301 // There are four options here.
1302 // - If we are in a trailing return type, this is always just a reference,
1303 // and we must not try to parse a definition. For instance,
1304 // [] () -> struct S { };
1305 // does not define a type.
1306 // - If we have 'struct foo {...', 'struct foo :...',
1307 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1308 // - If we have 'struct foo;', then this is either a forward declaration
1309 // or a friend declaration, which have to be treated differently.
1310 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001311 //
1312 // We also detect these erroneous cases to provide better diagnostic for
1313 // C++11 attributes parsing.
1314 // - attributes follow class name:
1315 // struct foo [[]] {};
1316 // - attributes appear before or after 'final':
1317 // struct foo [[]] final [[]] {};
1318 //
Richard Smithc5b05522012-03-12 07:56:15 +00001319 // However, in type-specifier-seq's, things look like declarations but are
1320 // just references, e.g.
1321 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001322 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001323 // &T::operator struct s;
1324 // For these, DSC is DSC_type_specifier.
Michael Han9407e502012-11-26 22:54:45 +00001325
1326 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001327 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001328
John McCallfaf5fb42010-08-26 23:41:50 +00001329 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001330 if (DSC == DSC_trailing)
1331 TUK = Sema::TUK_Reference;
1332 else if (Tok.is(tok::l_brace) ||
1333 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001334 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001335 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001336 if (DS.isFriendSpecified()) {
1337 // C++ [class.friend]p2:
1338 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001339 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001340 << SourceRange(DS.getFriendSpecLoc());
1341
1342 // Skip everything up to the semicolon, so that this looks like a proper
1343 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001344 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001345 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001346 } else {
1347 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001348 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001349 }
Richard Smith434516c2013-02-22 06:46:23 +00001350 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1351 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001352 // We can't tell if this is a definition or reference
1353 // until we skipped the 'final' and C++11 attribute specifiers.
1354 TentativeParsingAction PA(*this);
1355
1356 // Skip the 'final' keyword.
1357 ConsumeToken();
1358
1359 // Skip C++11 attribute specifiers.
1360 while (true) {
1361 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1362 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001363 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001364 break;
Richard Smith434516c2013-02-22 06:46:23 +00001365 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001366 ConsumeToken();
1367 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001368 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001369 break;
1370 } else {
1371 break;
1372 }
1373 }
1374
1375 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1376 TUK = Sema::TUK_Definition;
1377 else
1378 TUK = Sema::TUK_Reference;
1379
1380 PA.Revert();
Richard Smith369b9f92012-06-25 21:37:02 +00001381 } else if (DSC != DSC_type_specifier &&
1382 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001383 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001384 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001385 if (Tok.isNot(tok::semi)) {
1386 // A semicolon was missing after this declaration. Diagnose and recover.
1387 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1388 DeclSpec::getSpecifierName(TagType));
1389 PP.EnterToken(Tok);
1390 Tok.setKind(tok::semi);
1391 }
Richard Smith369b9f92012-06-25 21:37:02 +00001392 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001393 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001394
Michael Han9407e502012-11-26 22:54:45 +00001395 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1396 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001397 if (TUK != Sema::TUK_Reference) {
1398 // If this is not a reference, then the only possible
1399 // valid place for C++11 attributes to appear here
1400 // is between class-key and class-name. If there are
1401 // any attributes after class-name, we try a fixit to move
1402 // them to the right place.
1403 SourceRange AttrRange = Attributes.Range;
1404 if (AttrRange.isValid()) {
1405 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1406 << AttrRange
1407 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1408 CharSourceRange(AttrRange, true))
1409 << FixItHint::CreateRemoval(AttrRange);
1410
1411 // Recover by adding misplaced attributes to the attribute list
1412 // of the class so they can be applied on the class later.
1413 attrs.takeAllFrom(Attributes);
1414 }
1415 }
Michael Han9407e502012-11-26 22:54:45 +00001416
John McCall6347b682012-05-07 06:16:58 +00001417 // If this is an elaborated type specifier, and we delayed
1418 // diagnostics before, just merge them into the current pool.
1419 if (shouldDelayDiagsInTag) {
1420 diagsFromTag.done();
1421 if (TUK == Sema::TUK_Reference)
1422 diagsFromTag.redelay();
1423 }
1424
John McCall413021a2010-07-30 06:26:29 +00001425 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001426 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001427 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1428 // We have a declaration or reference to an anonymous class.
1429 Diag(StartLoc, diag::err_anon_type_definition)
1430 << DeclSpec::getSpecifierName(TagType);
1431 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001432
Alexey Bataevee6507d2013-11-18 08:17:37 +00001433 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001434 return;
1435 }
1436
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001437 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001438 DeclResult TagOrTempResult = true; // invalid
1439 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001440
Douglas Gregord6ab8742009-05-28 23:31:59 +00001441 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001442 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001443 // Explicit specialization, class template partial specialization,
1444 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001445 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001446 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001447 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001448 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001449 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001450 ProhibitAttributes(attrs);
1451
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001452 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001453 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001454 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001455 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001456 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001457 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001458 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001459 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001460 TemplateId->TemplateNameLoc,
1461 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001462 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001463 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001464 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001465
1466 // Friend template-ids are treated as references unless
1467 // they have template headers, in which case they're ill-formed
1468 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1469 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001470 } else if (TUK == Sema::TUK_Reference ||
1471 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001472 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001473 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001474 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001475 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001476 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001477 TemplateId->Template,
1478 TemplateId->TemplateNameLoc,
1479 TemplateId->LAngleLoc,
1480 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001481 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001482 } else {
1483 // This is an explicit specialization or a class template
1484 // partial specialization.
1485 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001486 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1487 // This looks like an explicit instantiation, because we have
1488 // something like
1489 //
1490 // template class Foo<X>
1491 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001492 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001493 // meant to be an explicit specialization, but the user forgot
1494 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001495 // It this is friend declaration however, since it cannot have a
1496 // template header, it is most likely that the user meant to
1497 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001498 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001499 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001500
Richard Smith003c5e12013-11-08 19:03:29 +00001501 if (TUK == Sema::TUK_Friend) {
1502 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
1503 TemplateParams = 0;
1504 } else {
1505 SourceLocation LAngleLoc =
1506 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1507 Diag(TemplateId->TemplateNameLoc,
1508 diag::err_explicit_instantiation_with_definition)
1509 << SourceRange(TemplateInfo.TemplateLoc)
1510 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1511
1512 // Create a fake template parameter list that contains only
1513 // "template<>", so that we treat this construct as a class
1514 // template specialization.
1515 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1516 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1517 LAngleLoc));
1518 TemplateParams = &FakedParamLists;
1519 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001520 }
1521
1522 // Build the class template specialization.
1523 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001524 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor3c7cd6a2011-09-09 20:53:38 +00001525 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall3e56fd42010-08-23 07:28:44 +00001526 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001527 TemplateId->TemplateNameLoc,
1528 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +00001529 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001530 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001531 attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001532 MultiTemplateParamsArg(
Douglas Gregor67a65642009-02-17 23:15:12 +00001533 TemplateParams? &(*TemplateParams)[0] : 0,
1534 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001535 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001536 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001537 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001538 // Explicit instantiation of a member of a class template
1539 // specialization, e.g.,
1540 //
1541 // template struct Outer<int>::Inner;
1542 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001543 ProhibitAttributes(attrs);
1544
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001545 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001546 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001547 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001548 TemplateInfo.TemplateLoc,
1549 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001550 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001551 } else if (TUK == Sema::TUK_Friend &&
1552 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001553 ProhibitAttributes(attrs);
1554
John McCallace48cd2010-10-19 01:40:49 +00001555 TagOrTempResult =
1556 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1557 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001558 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001559 MultiTemplateParamsArg(
John McCallace48cd2010-10-19 01:40:49 +00001560 TemplateParams? &(*TemplateParams)[0] : 0,
1561 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001562 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001563 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1564 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001565
Larisse Voufo725de3e2013-06-21 00:08:46 +00001566 if (TUK == Sema::TUK_Definition &&
1567 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1568 // If the declarator-id is not a template-id, issue a diagnostic and
1569 // recover by ignoring the 'template' keyword.
1570 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1571 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001572 TemplateParams = 0;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001573 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001574
John McCall7f41d982009-09-11 04:59:25 +00001575 bool IsDependent = false;
1576
John McCall32723e92010-10-19 18:40:57 +00001577 // Don't pass down template parameter lists if this is just a tag
1578 // reference. For example, we don't need the template parameters here:
1579 // template <class T> class A *makeA(T t);
1580 MultiTemplateParamsArg TParams;
1581 if (TUK != Sema::TUK_Reference && TemplateParams)
1582 TParams =
1583 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1584
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001585 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001586 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001587 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001588 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001589 TParams, Owned, IsDependent,
1590 SourceLocation(), false,
1591 clang::TypeResult());
John McCall7f41d982009-09-11 04:59:25 +00001592
1593 // If ActOnTag said the type was dependent, try again with the
1594 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001595 if (IsDependent) {
1596 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001597 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001598 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001599 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001600 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001601
Douglas Gregor556877c2008-04-13 21:30:24 +00001602 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001603 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001604 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001605 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001606 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001607 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001608 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1609 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001610 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001611 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001612 }
1613
John McCallba7bf592010-08-24 05:47:05 +00001614 const char *PrevSpec = 0;
1615 unsigned DiagID;
1616 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001617 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001618 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1619 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallba7bf592010-08-24 05:47:05 +00001620 PrevSpec, DiagID, TypeResult.get());
John McCall7f41d982009-09-11 04:59:25 +00001621 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001622 Result = DS.SetTypeSpecType(TagType, StartLoc,
1623 NameLoc.isValid() ? NameLoc : StartLoc,
1624 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCall7f41d982009-09-11 04:59:25 +00001625 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001626 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001627 return;
1628 }
Mike Stump11289f42009-09-09 15:08:12 +00001629
John McCallba7bf592010-08-24 05:47:05 +00001630 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001631 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001632
Chris Lattnercf251412010-02-02 01:23:29 +00001633 // At this point, we've successfully parsed a class-specifier in 'definition'
1634 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1635 // going to look at what comes after it to improve error recovery. If an
1636 // impossible token occurs next, we assume that the programmer forgot a ; at
1637 // the end of the declaration and recover that way.
1638 //
Richard Smith369b9f92012-06-25 21:37:02 +00001639 // Also enforce C++ [temp]p3:
1640 // In a template-declaration which defines a class, no declarator
1641 // is permitted.
Joao Matose9a3ed42012-08-31 22:18:20 +00001642 if (TUK == Sema::TUK_Definition &&
1643 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001644 if (Tok.isNot(tok::semi)) {
1645 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1646 DeclSpec::getSpecifierName(TagType));
1647 // Push this token back into the preprocessor and change our current token
1648 // to ';' so that the rest of the code recovers as though there were an
1649 // ';' after the definition.
1650 PP.EnterToken(Tok);
1651 Tok.setKind(tok::semi);
1652 }
Chris Lattnercf251412010-02-02 01:23:29 +00001653 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001654}
1655
Mike Stump11289f42009-09-09 15:08:12 +00001656/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001657///
1658/// base-clause : [C++ class.derived]
1659/// ':' base-specifier-list
1660/// base-specifier-list:
1661/// base-specifier '...'[opt]
1662/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001663void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001664 assert(Tok.is(tok::colon) && "Not a base clause");
1665 ConsumeToken();
1666
Douglas Gregor29a92472008-10-22 17:49:05 +00001667 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001668 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001669
Douglas Gregor556877c2008-04-13 21:30:24 +00001670 while (true) {
1671 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001672 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001673 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001674 // Skip the rest of this base specifier, up until the comma or
1675 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001676 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001677 } else {
1678 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001679 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001680 }
1681
1682 // If the next token is a comma, consume it and keep reading
1683 // base-specifiers.
1684 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001685
Douglas Gregor556877c2008-04-13 21:30:24 +00001686 // Consume the comma.
1687 ConsumeToken();
1688 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001689
1690 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001691 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001692}
1693
1694/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1695/// one entry in the base class list of a class specifier, for example:
1696/// class foo : public bar, virtual private baz {
1697/// 'public bar' and 'virtual private baz' are each base-specifiers.
1698///
1699/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001700/// attribute-specifier-seq[opt] base-type-specifier
1701/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1702/// base-type-specifier
1703/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1704/// base-type-specifier
John McCall48871652010-08-21 09:40:31 +00001705Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001706 bool IsVirtual = false;
1707 SourceLocation StartLoc = Tok.getLocation();
1708
Richard Smith4c96e992013-02-19 23:47:15 +00001709 ParsedAttributesWithRange Attributes(AttrFactory);
1710 MaybeParseCXX11Attributes(Attributes);
1711
Douglas Gregor556877c2008-04-13 21:30:24 +00001712 // Parse the 'virtual' keyword.
1713 if (Tok.is(tok::kw_virtual)) {
1714 ConsumeToken();
1715 IsVirtual = true;
1716 }
1717
Richard Smith4c96e992013-02-19 23:47:15 +00001718 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1719
Douglas Gregor556877c2008-04-13 21:30:24 +00001720 // Parse an (optional) access specifier.
1721 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001722 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001723 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001724
Richard Smith4c96e992013-02-19 23:47:15 +00001725 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1726
Douglas Gregor556877c2008-04-13 21:30:24 +00001727 // Parse the 'virtual' keyword (again!), in case it came after the
1728 // access specifier.
1729 if (Tok.is(tok::kw_virtual)) {
1730 SourceLocation VirtualLoc = ConsumeToken();
1731 if (IsVirtual) {
1732 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001733 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001734 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001735 }
1736
1737 IsVirtual = true;
1738 }
1739
Richard Smith4c96e992013-02-19 23:47:15 +00001740 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1741
Douglas Gregor831c93f2008-11-05 20:51:48 +00001742 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001743 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001744 SourceLocation BaseLoc;
1745 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001746 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001747 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001748
Douglas Gregor752a5952011-01-03 22:36:02 +00001749 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1750 // actually part of the base-specifier-list grammar productions, but we
1751 // parse it here for convenience.
1752 SourceLocation EllipsisLoc;
1753 if (Tok.is(tok::ellipsis))
1754 EllipsisLoc = ConsumeToken();
1755
Mike Stump11289f42009-09-09 15:08:12 +00001756 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001757 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001758
Douglas Gregor556877c2008-04-13 21:30:24 +00001759 // Notify semantic analysis that we have parsed a complete
1760 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001761 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1762 Access, BaseType.get(), BaseLoc,
1763 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001764}
1765
1766/// getAccessSpecifierIfPresent - Determine whether the next token is
1767/// a C++ access-specifier.
1768///
1769/// access-specifier: [C++ class.derived]
1770/// 'private'
1771/// 'protected'
1772/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001773AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001774 switch (Tok.getKind()) {
1775 default: return AS_none;
1776 case tok::kw_private: return AS_private;
1777 case tok::kw_protected: return AS_protected;
1778 case tok::kw_public: return AS_public;
1779 }
1780}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001781
Douglas Gregor433e0532012-04-16 18:27:27 +00001782/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001783/// delayed, e.g., default arguments, create a late-parsed method declaration
1784/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001785void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1786 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001787 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001788 // has any default arguments, we'll need to parse them later.
Eli Friedman3af2a772009-07-22 21:45:50 +00001789 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001790 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001791 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001792
Eli Friedman3af2a772009-07-22 21:45:50 +00001793 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1794 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1795 if (!LateMethod) {
1796 // Push this method onto the stack of late-parsed method
1797 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001798 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1799 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001800 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001801
1802 // Add all of the parameters prior to this one (they don't
1803 // have default arguments).
1804 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1805 for (unsigned I = 0; I < ParamIdx; ++I)
1806 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001807 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001808 }
1809
Douglas Gregor433e0532012-04-16 18:27:27 +00001810 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001811 // not have a default argument).
1812 LateMethod->DefaultArgs.push_back(
1813 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1814 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1815 }
1816 }
1817}
1818
Richard Smith89645bc2013-01-02 12:01:23 +00001819/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001820/// virt-specifier.
1821///
1822/// virt-specifier:
1823/// override
1824/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001825VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001826 if (!getLangOpts().CPlusPlus)
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001827 return VirtSpecifiers::VS_None;
1828
Anders Carlsson56104902011-01-17 03:05:47 +00001829 if (Tok.is(tok::identifier)) {
1830 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001831
Anders Carlsson428803b2011-01-20 03:47:08 +00001832 // Initialize the contextual keywords.
1833 if (!Ident_final) {
1834 Ident_final = &PP.getIdentifierTable().get("final");
David Majnemera5433082013-10-18 00:33:31 +00001835 if (getLangOpts().MicrosoftExt)
1836 Ident_sealed = &PP.getIdentifierTable().get("sealed");
Anders Carlsson428803b2011-01-20 03:47:08 +00001837 Ident_override = &PP.getIdentifierTable().get("override");
1838 }
1839
Anders Carlsson56104902011-01-17 03:05:47 +00001840 if (II == Ident_override)
1841 return VirtSpecifiers::VS_Override;
1842
David Majnemera5433082013-10-18 00:33:31 +00001843 if (II == Ident_sealed)
1844 return VirtSpecifiers::VS_Sealed;
1845
Anders Carlsson56104902011-01-17 03:05:47 +00001846 if (II == Ident_final)
1847 return VirtSpecifiers::VS_Final;
1848 }
1849
1850 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001851}
1852
Richard Smith89645bc2013-01-02 12:01:23 +00001853/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001854///
1855/// virt-specifier-seq:
1856/// virt-specifier
1857/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001858void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalldb632ac2012-09-25 07:32:39 +00001859 bool IsInterface) {
Anders Carlsson56104902011-01-17 03:05:47 +00001860 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001861 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001862 if (Specifier == VirtSpecifiers::VS_None)
1863 return;
1864
1865 // C++ [class.mem]p8:
1866 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001867 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001868 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001869 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1870 << PrevSpec
1871 << FixItHint::CreateRemoval(Tok.getLocation());
1872
David Majnemera5433082013-10-18 00:33:31 +00001873 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1874 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001875 Diag(Tok.getLocation(), diag::err_override_control_interface)
1876 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001877 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1878 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001879 } else {
David Majnemera5433082013-10-18 00:33:31 +00001880 Diag(Tok.getLocation(),
1881 getLangOpts().CPlusPlus11
1882 ? diag::warn_cxx98_compat_override_control_keyword
1883 : diag::ext_override_control_keyword)
1884 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00001885 }
Anders Carlsson56104902011-01-17 03:05:47 +00001886 ConsumeToken();
1887 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001888}
1889
Richard Smith89645bc2013-01-02 12:01:23 +00001890/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlssoncafbab72011-03-25 14:53:29 +00001891/// contextual 'final' keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001892bool Parser::isCXX11FinalKeyword() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001893 if (!getLangOpts().CPlusPlus)
Anders Carlssoncafbab72011-03-25 14:53:29 +00001894 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001895
Anders Carlssoncafbab72011-03-25 14:53:29 +00001896 if (!Tok.is(tok::identifier))
1897 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001898
Anders Carlssoncafbab72011-03-25 14:53:29 +00001899 // Initialize the contextual keywords.
1900 if (!Ident_final) {
1901 Ident_final = &PP.getIdentifierTable().get("final");
David Majnemera5433082013-10-18 00:33:31 +00001902 if (getLangOpts().MicrosoftExt)
1903 Ident_sealed = &PP.getIdentifierTable().get("sealed");
Anders Carlssoncafbab72011-03-25 14:53:29 +00001904 Ident_override = &PP.getIdentifierTable().get("override");
1905 }
David Majnemera5433082013-10-18 00:33:31 +00001906
1907 return Tok.getIdentifierInfo() == Ident_final ||
1908 Tok.getIdentifierInfo() == Ident_sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001909}
1910
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001911/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1912///
1913/// member-declaration:
1914/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1915/// function-definition ';'[opt]
1916/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1917/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001918/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001919/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001920/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001921///
1922/// member-declarator-list:
1923/// member-declarator
1924/// member-declarator-list ',' member-declarator
1925///
1926/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001927/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001928/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00001929/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001930/// identifier[opt] ':' constant-expression
1931///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001932/// virt-specifier-seq:
1933/// virt-specifier
1934/// virt-specifier-seq virt-specifier
1935///
1936/// virt-specifier:
1937/// override
1938/// final
David Majnemera5433082013-10-18 00:33:31 +00001939/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001940///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001941/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001942/// '= 0'
1943///
1944/// constant-initializer:
1945/// '=' constant-expression
1946///
Douglas Gregor3447e762009-08-20 22:52:58 +00001947void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001948 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00001949 const ParsedTemplateInfo &TemplateInfo,
1950 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001951 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001952 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00001953 Diag(Tok, diag::err_at_defs_cxx);
1954 else
1955 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00001956
Douglas Gregor23c84762011-04-14 17:21:19 +00001957 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001958 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00001959 return;
1960 }
Richard Smithda35e962013-11-09 04:52:51 +00001961
John McCalla0097262009-12-11 02:10:03 +00001962 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00001963 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00001964 if (!TemplateInfo.Kind &&
Richard Smith45855df2012-05-09 08:23:23 +00001965 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1966 if (TryAnnotateCXXScopeToken())
1967 MalformedTypeSpec = true;
1968
1969 bool isAccessDecl;
1970 if (Tok.isNot(tok::annot_cxxscope))
1971 isAccessDecl = false;
1972 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00001973 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1974 else
1975 isAccessDecl = NextToken().is(tok::kw_operator);
1976
1977 if (isAccessDecl) {
1978 // Collect the scope specifier token we annotated earlier.
1979 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001980 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1981 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00001982
1983 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001984 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00001985 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001986 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1987 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00001988 SkipUntil(tok::semi);
1989 return;
1990 }
1991
1992 // TODO: recover from mistakenly-qualified operator declarations.
1993 if (ExpectAndConsume(tok::semi,
1994 diag::err_expected_semi_after,
1995 "access declaration",
1996 tok::semi))
1997 return;
1998
Douglas Gregor0be31a22010-07-02 17:43:08 +00001999 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002000 /* HasUsingKeyword */ false,
2001 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00002002 SS, Name,
2003 /* AttrList */ 0,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002004 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00002005 SourceLocation());
2006 return;
2007 }
2008 }
2009
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002010 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002011 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00002012 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00002013 SourceLocation DeclEnd;
2014 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002015 return;
2016 }
Mike Stump11289f42009-09-09 15:08:12 +00002017
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002018 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002019 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002020 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002021 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002022 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002023 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002024 return;
2025 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002026
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002027 // Handle: member-declaration ::= '__extension__' member-declaration
2028 if (Tok.is(tok::kw___extension__)) {
2029 // __extension__ silences extension warnings in the subexpression.
2030 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2031 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002032 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2033 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002034 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002035
Chris Lattnercf251412010-02-02 01:23:29 +00002036 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2037 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002038 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002039
John McCall084e83d2011-03-24 11:26:52 +00002040 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002041 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002042 // Optional C++11 attribute-specifier
2043 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002044 // We need to keep these attributes for future diagnostic
2045 // before they are taken over by declaration specifier.
2046 FnAttrs.addAll(attrs.getList());
2047 FnAttrs.Range = attrs.Range;
2048
John McCall53fa7142010-12-24 02:08:15 +00002049 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002050
Douglas Gregorfec52632009-06-20 00:51:54 +00002051 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002052 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002053
Douglas Gregorfec52632009-06-20 00:51:54 +00002054 // Eat 'using'.
2055 SourceLocation UsingLoc = ConsumeToken();
2056
2057 if (Tok.is(tok::kw_namespace)) {
2058 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002059 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002060 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002061 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002062 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002063 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2064 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002065 }
2066 return;
2067 }
2068
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002069 // Hold late-parsed attributes so we can attach a Decl to them later.
2070 LateParsedAttrList CommonLateParsedAttrs;
2071
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002072 // decl-specifier-seq:
2073 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002074 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002075 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002076 if (MalformedTypeSpec)
2077 DS.SetTypeSpecError();
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002078 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2079 &CommonLateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002080
Richard Smith404dfb42013-11-19 22:47:36 +00002081 // If we had a free-standing type definition with a missing semicolon, we
2082 // may get this far before the problem becomes obvious.
2083 if (DS.hasTagDefinition() &&
2084 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2085 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2086 &CommonLateParsedAttrs))
2087 return;
2088
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002089 MultiTemplateParamsArg TemplateParams(
John McCall11083da2009-09-16 22:47:08 +00002090 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2091 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2092
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002093 if (Tok.is(tok::semi)) {
2094 ConsumeToken();
Michael Handdc016d2012-11-28 23:17:40 +00002095
2096 if (DS.isFriendSpecified())
2097 ProhibitAttributes(FnAttrs);
2098
John McCall48871652010-08-21 09:40:31 +00002099 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002100 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002101 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002102 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002103 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002104
John McCall28a6aea2009-11-04 02:18:39 +00002105 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002106 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002107
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002108 // Hold late-parsed attributes so we can attach a Decl to them later.
2109 LateParsedAttrList LateParsedAttrs;
2110
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002111 SourceLocation EqualLoc;
2112 bool HasInitializer = false;
2113 ExprResult Init;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002114 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002115 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2116 ColonProtectionRAIIObject X(*this);
2117
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002118 // Parse the first declarator.
2119 ParseDeclarator(DeclaratorInfo);
Richard Smith2331bbf2012-05-02 22:22:32 +00002120 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00002121 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002122 // If so, skip until the semi-colon or a }.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002123 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002124 if (Tok.is(tok::semi))
2125 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002126 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002127 }
2128
Richard Smith89645bc2013-01-02 12:01:23 +00002129 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber24b2a822011-01-28 06:07:34 +00002130
John Thompson5bc5cbe2009-11-25 22:58:06 +00002131 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002132 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson5bc5cbe2009-11-25 22:58:06 +00002133
Francois Pichet3abc9b82011-05-11 02:14:46 +00002134 // MSVC permits pure specifier on inline functions declared at class scope.
2135 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002136 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002137 DeclaratorInfo.isFunctionDeclarator() &&
2138 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002139 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002140 Init = ParseInitializer();
2141 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002142 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002143 else
2144 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002145 }
2146
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002147 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002148 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002149 //
2150 // In C++11, a non-function declarator followed by an open brace is a
2151 // braced-init-list for an in-class member initialization, not an
2152 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002153 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002154 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002155 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002156 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002157 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002158 } else if (Tok.is(tok::equal)) {
2159 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002160 if (KW.is(tok::kw_default))
2161 DefinitionKind = FDK_Defaulted;
2162 else if (KW.is(tok::kw_delete))
2163 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002164 }
2165 }
2166
Michael Handdc016d2012-11-28 23:17:40 +00002167 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2168 // to a friend declaration, that declaration shall be a definition.
2169 if (DeclaratorInfo.isFunctionDeclarator() &&
2170 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2171 // Diagnose attributes that appear before decl specifier:
2172 // [[]] friend int foo();
2173 ProhibitAttributes(FnAttrs);
2174 }
2175
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002176 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002177 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002178 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002179 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002180 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002181
Douglas Gregor8a4db832011-01-19 16:41:58 +00002182 // Consume the optional ';'
2183 if (Tok.is(tok::semi))
2184 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002185 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002186 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002187
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002188 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002189 Diag(DeclaratorInfo.getIdentifierLoc(),
2190 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002191
Richard Smith2603b092012-11-15 22:54:20 +00002192 // Recover by treating the 'typedef' as spurious.
2193 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002194 }
2195
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002196 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002197 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002198 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002199
David Majnemer23252a32013-08-01 04:22:55 +00002200 if (FunDecl) {
2201 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2202 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2203 }
2204 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2205 LateParsedAttrs[i]->addDecl(FunDecl);
2206 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002207 }
2208 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002209
2210 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002211 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002212 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002213
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002214 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002215 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002216 }
2217
2218 // member-declarator-list:
2219 // member-declarator
2220 // member-declarator-list ',' member-declarator
2221
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002222 SmallVector<Decl *, 8> DeclsInGroup;
John McCalldadc5752010-08-24 06:29:42 +00002223 ExprResult BitfieldSize;
Richard Smithc8a79032012-01-09 22:31:44 +00002224 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002225
2226 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002227 // member-declarator:
2228 // declarator pure-specifier[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002229 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002230 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002231 if (Tok.is(tok::colon)) {
2232 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002233 BitfieldSize = ParseConstantExpression();
2234 if (BitfieldSize.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002235 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002236 }
Mike Stump11289f42009-09-09 15:08:12 +00002237
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002238 // If a simple-asm-expr is present, parse it.
2239 if (Tok.is(tok::kw_asm)) {
2240 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00002241 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002242 if (AsmLabel.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002243 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002244
2245 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2246 DeclaratorInfo.SetRangeEnd(Loc);
2247 }
2248
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002249 // If attributes exist after the declarator, parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002250 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002251
Richard Smith938f40b2011-06-11 17:19:42 +00002252 // FIXME: When g++ adds support for this, we'll need to check whether it
2253 // goes before or after the GNU attributes and __asm__.
Richard Smith89645bc2013-01-02 12:01:23 +00002254 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith938f40b2011-06-11 17:19:42 +00002255
Richard Smith2b013182012-06-10 03:12:00 +00002256 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002257 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002258 if (BitfieldSize.get()) {
2259 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002260 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002261 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002262 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002263 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2264 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002265 != DeclSpec::SCS_typedef)
2266 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002267 }
2268 }
2269
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002270 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002271 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002272 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002273
Rafael Espindola0a67e2f2013-01-08 20:44:06 +00002274 NamedDecl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00002275 if (DS.isFriendSpecified()) {
Michael Handdc016d2012-11-28 23:17:40 +00002276 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2277 // to a friend declaration, that declaration shall be a definition.
2278 //
2279 // Diagnose attributes appear after friend member function declarator:
2280 // foo [[]] ();
2281 SmallVector<SourceRange, 4> Ranges;
2282 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2283 if (!Ranges.empty()) {
Craig Topper2341c0d2013-07-04 03:08:24 +00002284 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
Michael Handdc016d2012-11-28 23:17:40 +00002285 E = Ranges.end(); I != E; ++I) {
2286 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2287 << *I;
2288 }
2289 }
2290
John McCall2f212b32009-09-11 21:02:39 +00002291 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00002292 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002293 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002294 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002295 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002296 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002297 TemplateParams,
John McCall07e91c02009-08-06 02:15:43 +00002298 BitfieldSize.release(),
Richard Smith2b013182012-06-10 03:12:00 +00002299 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002300
2301 if (VarTemplateDecl *VT =
2302 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
2303 // Re-direct this decl to refer to the templated decl so that we can
2304 // initialize it.
2305 ThisDecl = VT->getTemplatedDecl();
2306
David Majnemer23252a32013-08-01 04:22:55 +00002307 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002308 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002309 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002310
Douglas Gregor728d00b2011-10-10 14:49:18 +00002311 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002312 if (HasInClassInit != ICIS_NoInit &&
2313 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2314 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002315 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002316 Diag(Tok, getLangOpts().CPlusPlus11
2317 ? diag::warn_cxx98_compat_nonstatic_member_init
2318 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002319
Richard Smith938f40b2011-06-11 17:19:42 +00002320 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002321 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2322 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002323 //
2324 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002325 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002326 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002327 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002328
2329 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002330 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002331 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002332 } else
2333 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002334 } else if (HasInitializer) {
2335 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002336 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002337 Init = ParseCXXMemberInitializer(
2338 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2339
Douglas Gregor728d00b2011-10-10 14:49:18 +00002340 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002341 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002342 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002343 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002344 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002345 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002346 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002347 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002348
Douglas Gregor728d00b2011-10-10 14:49:18 +00002349 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002350 if (!ThisDecl->isInvalidDecl()) {
2351 // Set the Decl for any late parsed attributes
2352 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2353 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2354
2355 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2356 LateParsedAttrs[i]->addDecl(ThisDecl);
2357 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002358 Actions.FinalizeDeclaration(ThisDecl);
2359 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002360
2361 if (DeclaratorInfo.isFunctionDeclarator() &&
2362 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2363 DeclSpec::SCS_typedef)
2364 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002365 }
David Majnemer23252a32013-08-01 04:22:55 +00002366 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002367
2368 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002369
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002370 // If we don't have a comma, it is either the end of the list (a ';')
2371 // or an error, bail out.
2372 if (Tok.isNot(tok::comma))
2373 break;
Mike Stump11289f42009-09-09 15:08:12 +00002374
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002375 // Consume the comma.
Richard Smithc8a79032012-01-09 22:31:44 +00002376 SourceLocation CommaLoc = ConsumeToken();
2377
2378 if (Tok.isAtStartOfLine() &&
2379 !MightBeDeclarator(Declarator::MemberContext)) {
2380 // This comma was followed by a line-break and something which can't be
2381 // the start of a declarator. The comma was probably a typo for a
2382 // semicolon.
2383 Diag(CommaLoc, diag::err_expected_semi_declaration)
2384 << FixItHint::CreateReplacement(CommaLoc, ";");
2385 ExpectSemi = false;
2386 break;
2387 }
Mike Stump11289f42009-09-09 15:08:12 +00002388
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002389 // Parse the next declarator.
2390 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002391 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002392 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002393 Init = true;
2394 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002395 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002396
Bill Wendling44426052012-12-20 19:22:21 +00002397 // Attributes are only allowed on the second declarator.
John McCall53fa7142010-12-24 02:08:15 +00002398 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002399
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002400 if (Tok.isNot(tok::colon))
2401 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002402 }
2403
Richard Smithc8a79032012-01-09 22:31:44 +00002404 if (ExpectSemi &&
2405 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002406 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002407 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002408 // If we stopped at a ';', eat it.
2409 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002410 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002411 }
2412
Rafael Espindolaab417692013-07-09 12:05:01 +00002413 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002414}
2415
Richard Smith938f40b2011-06-11 17:19:42 +00002416/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2417/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2418/// function definition. The location of the '=', if any, will be placed in
2419/// EqualLoc.
2420///
2421/// pure-specifier:
2422/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002423///
Richard Smith938f40b2011-06-11 17:19:42 +00002424/// brace-or-equal-initializer:
2425/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002426/// braced-init-list
2427///
Richard Smith938f40b2011-06-11 17:19:42 +00002428/// initializer-clause:
2429/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002430/// braced-init-list
2431///
Richard Smithda35e962013-11-09 04:52:51 +00002432/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002433/// '=' 'default'
2434/// '=' 'delete'
2435///
2436/// Prior to C++0x, the assignment-expression in an initializer-clause must
2437/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002438ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002439 SourceLocation &EqualLoc) {
2440 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2441 && "Data member initializer not starting with '=' or '{'");
2442
Douglas Gregor926410d2012-02-21 02:22:07 +00002443 EnterExpressionEvaluationContext Context(Actions,
2444 Sema::PotentiallyEvaluated,
2445 D);
Richard Smith938f40b2011-06-11 17:19:42 +00002446 if (Tok.is(tok::equal)) {
2447 EqualLoc = ConsumeToken();
2448 if (Tok.is(tok::kw_delete)) {
2449 // In principle, an initializer of '= delete p;' is legal, but it will
2450 // never type-check. It's better to diagnose it as an ill-formed expression
2451 // than as an ill-formed deleted non-function member.
2452 // An initializer of '= delete p, foo' will never be parsed, because
2453 // a top-level comma always ends the initializer expression.
2454 const Token &Next = NextToken();
2455 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002456 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002457 if (IsFunction)
2458 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2459 << 1 /* delete */;
2460 else
2461 Diag(ConsumeToken(), diag::err_deleted_non_function);
2462 return ExprResult();
2463 }
2464 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002465 if (IsFunction)
2466 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2467 << 0 /* default */;
2468 else
2469 Diag(ConsumeToken(), diag::err_default_special_members);
2470 return ExprResult();
2471 }
2472
Sebastian Redleef474c2012-02-22 10:50:08 +00002473 }
2474 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002475}
2476
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002477/// ParseCXXMemberSpecification - Parse the class definition.
2478///
2479/// member-specification:
2480/// member-declaration member-specification[opt]
2481/// access-specifier ':' member-specification[opt]
2482///
Joao Matose9a3ed42012-08-31 22:18:20 +00002483void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002484 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002485 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002486 unsigned TagType, Decl *TagDecl) {
2487 assert((TagType == DeclSpec::TST_struct ||
2488 TagType == DeclSpec::TST_interface ||
2489 TagType == DeclSpec::TST_union ||
2490 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2491
John McCallfaf5fb42010-08-26 23:41:50 +00002492 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2493 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002494
Douglas Gregoredf8f392010-01-16 20:52:59 +00002495 // Determine whether this is a non-nested class. Note that local
2496 // classes are *not* considered to be nested classes.
2497 bool NonNestedClass = true;
2498 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002499 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002500 if (S->isClassScope()) {
2501 // We're inside a class scope, so this is a nested class.
2502 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002503
2504 // The Microsoft extension __interface does not permit nested classes.
2505 if (getCurrentClass().IsInterface) {
2506 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2507 << /*ErrorType=*/6
2508 << (isa<NamedDecl>(TagDecl)
2509 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2510 : "<anonymous>");
2511 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002512 break;
2513 }
2514
2515 if ((S->getFlags() & Scope::FnScope)) {
2516 // If we're in a function or function template declared in the
2517 // body of a class, then this is a local class rather than a
2518 // nested class.
2519 const Scope *Parent = S->getParent();
2520 if (Parent->isTemplateParamScope())
2521 Parent = Parent->getParent();
2522 if (Parent->isClassScope())
2523 break;
2524 }
2525 }
2526 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002527
2528 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002529 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002530
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002531 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002532 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2533 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002534
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002535 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002536 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002537
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002538 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002539 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002540
2541 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002542 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002543 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2544 assert((Specifier == VirtSpecifiers::VS_Final ||
2545 Specifier == VirtSpecifiers::VS_Sealed) &&
2546 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002547 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002548 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002549
David Majnemera5433082013-10-18 00:33:31 +00002550 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002551 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002552 << VirtSpecifiers::getSpecifierName(Specifier);
2553 else if (Specifier == VirtSpecifiers::VS_Final)
2554 Diag(FinalLoc, getLangOpts().CPlusPlus11
2555 ? diag::warn_cxx98_compat_override_control_keyword
2556 : diag::ext_override_control_keyword)
2557 << VirtSpecifiers::getSpecifierName(Specifier);
2558 else if (Specifier == VirtSpecifiers::VS_Sealed)
2559 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002560
Michael Han309af292013-01-07 16:57:11 +00002561 // Parse any C++11 attributes after 'final' keyword.
2562 // These attributes are not allowed to appear here,
2563 // and the only possible place for them to appertain
2564 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002565 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002566 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002567
John McCall2d814c32009-12-19 21:48:58 +00002568 if (Tok.is(tok::colon)) {
2569 ParseBaseClause(TagDecl);
2570
2571 if (!Tok.is(tok::l_brace)) {
2572 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00002573
2574 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002575 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002576 return;
2577 }
2578 }
2579
2580 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002581 BalancedDelimiterTracker T(*this, tok::l_brace);
2582 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002583
John McCall08bede42010-05-28 08:11:17 +00002584 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002585 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002586 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002587 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002588
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002589 // C++ 11p3: Members of a class defined with the keyword class are private
2590 // by default. Members of a class defined with the keywords struct or union
2591 // are public by default.
2592 AccessSpecifier CurAS;
2593 if (TagType == DeclSpec::TST_class)
2594 CurAS = AS_private;
2595 else
2596 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002597 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002598
Douglas Gregor9377c822010-06-21 22:31:09 +00002599 if (TagDecl) {
2600 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002601 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002602 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002603
David Blaikiebbafb8a2012-03-11 07:00:24 +00002604 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002605 Tok.is(tok::kw___if_not_exists))) {
2606 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2607 continue;
2608 }
2609
Douglas Gregor9377c822010-06-21 22:31:09 +00002610 // Check for extraneous top-level semicolon.
2611 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002612 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002613 continue;
2614 }
2615
Eli Friedmanec52f922012-02-23 23:47:16 +00002616 if (Tok.is(tok::annot_pragma_vis)) {
2617 HandlePragmaVisibility();
2618 continue;
2619 }
2620
2621 if (Tok.is(tok::annot_pragma_pack)) {
2622 HandlePragmaPack();
2623 continue;
2624 }
2625
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002626 if (Tok.is(tok::annot_pragma_align)) {
2627 HandlePragmaAlign();
2628 continue;
2629 }
2630
Alexey Bataeva769e072013-03-22 06:34:35 +00002631 if (Tok.is(tok::annot_pragma_openmp)) {
2632 ParseOpenMPDeclarativeDirective();
2633 continue;
2634 }
2635
Richard Smithda35e962013-11-09 04:52:51 +00002636 // If we see a namespace here, a close brace was missing somewhere.
2637 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002638 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002639 break;
2640 }
2641
Douglas Gregor9377c822010-06-21 22:31:09 +00002642 AccessSpecifier AS = getAccessSpecifierIfPresent();
2643 if (AS != AS_none) {
2644 // Current token is a C++ access specifier.
2645 CurAS = AS;
2646 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002647 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002648 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002649 AccessAttrs.clear();
2650 MaybeParseGNUAttributes(AccessAttrs);
2651
David Blaikieeba32c22011-10-13 06:08:43 +00002652 SourceLocation EndLoc;
2653 if (Tok.is(tok::colon)) {
2654 EndLoc = Tok.getLocation();
2655 ConsumeToken();
2656 } else if (Tok.is(tok::semi)) {
2657 EndLoc = Tok.getLocation();
2658 ConsumeToken();
2659 Diag(EndLoc, diag::err_expected_colon)
2660 << FixItHint::CreateReplacement(EndLoc, ":");
2661 } else {
2662 EndLoc = ASLoc.getLocWithOffset(TokLength);
2663 Diag(EndLoc, diag::err_expected_colon)
2664 << FixItHint::CreateInsertion(EndLoc, ":");
2665 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002666
John McCalldb632ac2012-09-25 07:32:39 +00002667 // The Microsoft extension __interface does not permit non-public
2668 // access specifiers.
2669 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2670 Diag(ASLoc, diag::err_access_specifier_interface)
2671 << (CurAS == AS_protected);
2672 }
2673
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002674 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2675 AccessAttrs.getList())) {
2676 // found another attribute than only annotations
2677 AccessAttrs.clear();
2678 }
2679
Douglas Gregor9377c822010-06-21 22:31:09 +00002680 continue;
2681 }
2682
Douglas Gregor9377c822010-06-21 22:31:09 +00002683 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002684 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002685 }
2686
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002687 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002688 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002689 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002690 }
Mike Stump11289f42009-09-09 15:08:12 +00002691
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002692 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002693 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002694 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002695
John McCall08bede42010-05-28 08:11:17 +00002696 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002697 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002698 T.getOpenLocation(),
2699 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002700 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002701
Douglas Gregor433e0532012-04-16 18:27:27 +00002702 // C++11 [class.mem]p2:
2703 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002704 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002705 // brace-or-equal-initializers for non-static data members (including such
2706 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002707 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002708 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002709 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002710 // declarations and the lexed inline method definitions, along with any
2711 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002712 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002713 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002714 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002715
2716 // We've finished with all pending member declarations.
2717 Actions.ActOnFinishCXXMemberDecls();
2718
Richard Smith938f40b2011-06-11 17:19:42 +00002719 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002720 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002721 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002722 }
2723
John McCall08bede42010-05-28 08:11:17 +00002724 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002725 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2726 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002727
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002728 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002729 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002730 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002731}
Douglas Gregore8381c02008-11-05 04:29:56 +00002732
Richard Smith2ac43ad2013-11-15 23:00:02 +00002733void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002734 assert(Tok.is(tok::kw_namespace));
2735
2736 // FIXME: Suggest where the close brace should have gone by looking
2737 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002738 Diag(D->getLocation(),
2739 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002740 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002741 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002742
2743 // Push '};' onto the token stream to recover.
2744 PP.EnterToken(Tok);
2745
2746 Tok.startToken();
2747 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2748 Tok.setKind(tok::semi);
2749 PP.EnterToken(Tok);
2750
2751 Tok.setKind(tok::r_brace);
2752}
2753
Douglas Gregore8381c02008-11-05 04:29:56 +00002754/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2755/// which explicitly initializes the members or base classes of a
2756/// class (C++ [class.base.init]). For example, the three initializers
2757/// after the ':' in the Derived constructor below:
2758///
2759/// @code
2760/// class Base { };
2761/// class Derived : Base {
2762/// int x;
2763/// float f;
2764/// public:
2765/// Derived(float f) : Base(), x(17), f(f) { }
2766/// };
2767/// @endcode
2768///
Mike Stump11289f42009-09-09 15:08:12 +00002769/// [C++] ctor-initializer:
2770/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002771///
Mike Stump11289f42009-09-09 15:08:12 +00002772/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002773/// mem-initializer ...[opt]
2774/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002775void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002776 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2777
John Wiegley1c0675e2011-04-28 01:08:34 +00002778 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2779 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002780 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002781
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002782 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002783 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002784
Douglas Gregore8381c02008-11-05 04:29:56 +00002785 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002786 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002787 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2788 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002789 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002790 } else {
2791 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2792 if (!MemInit.isInvalid())
2793 MemInitializers.push_back(MemInit.get());
2794 else
2795 AnyErrors = true;
2796 }
2797
Douglas Gregore8381c02008-11-05 04:29:56 +00002798 if (Tok.is(tok::comma))
2799 ConsumeToken();
2800 else if (Tok.is(tok::l_brace))
2801 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002802 // If the next token looks like a base or member initializer, assume that
2803 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002804 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2805 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2806 Diag(Loc, diag::err_ctor_init_missing_comma)
2807 << FixItHint::CreateInsertion(Loc, ", ");
2808 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002809 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00002810 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002811 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00002812 break;
2813 }
2814 } while (true);
2815
David Blaikie3fc2f912013-01-17 05:26:25 +00002816 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002817 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002818}
2819
2820/// ParseMemInitializer - Parse a C++ member initializer, which is
2821/// part of a constructor initializer that explicitly initializes one
2822/// member or base class (C++ [class.base.init]). See
2823/// ParseConstructorInitializer for an example.
2824///
2825/// [C++] mem-initializer:
2826/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002827/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002828///
Douglas Gregore8381c02008-11-05 04:29:56 +00002829/// [C++] mem-initializer-id:
2830/// '::'[opt] nested-name-specifier[opt] class-name
2831/// identifier
John McCall48871652010-08-21 09:40:31 +00002832Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002833 // parse '::'[opt] nested-name-specifier[opt]
2834 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002835 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002836 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002837 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002838 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002839 if (TemplateId->Kind == TNK_Type_template ||
2840 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002841 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002842 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002843 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002844 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002845 }
David Blaikie186a8892012-01-24 06:03:59 +00002846 // Uses of decltype will already have been converted to annot_decltype by
2847 // ParseOptionalCXXScopeSpecifier at this point.
2848 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2849 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002850 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002851 return true;
2852 }
Mike Stump11289f42009-09-09 15:08:12 +00002853
David Blaikie186a8892012-01-24 06:03:59 +00002854 IdentifierInfo *II = 0;
2855 DeclSpec DS(AttrFactory);
2856 SourceLocation IdLoc = Tok.getLocation();
2857 if (Tok.is(tok::annot_decltype)) {
2858 // Get the decltype expression, if there is one.
2859 ParseDecltypeSpecifier(DS);
2860 } else {
2861 if (Tok.is(tok::identifier))
2862 // Get the identifier. This may be a member name or a class name,
2863 // but we'll let the semantic analysis determine which it is.
2864 II = Tok.getIdentifierInfo();
2865 ConsumeToken();
2866 }
2867
Douglas Gregore8381c02008-11-05 04:29:56 +00002868
2869 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002870 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002871 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2872
Sebastian Redla74948d2011-09-24 17:48:25 +00002873 ExprResult InitList = ParseBraceInitializer();
2874 if (InitList.isInvalid())
2875 return true;
2876
2877 SourceLocation EllipsisLoc;
2878 if (Tok.is(tok::ellipsis))
2879 EllipsisLoc = ConsumeToken();
2880
2881 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002882 TemplateTypeTy, DS, IdLoc,
2883 InitList.take(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002884 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002885 BalancedDelimiterTracker T(*this, tok::l_paren);
2886 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00002887
Sebastian Redl3da34892011-06-05 12:23:16 +00002888 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00002889 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00002890 CommaLocsTy CommaLocs;
2891 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002892 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00002893 return true;
2894 }
2895
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002896 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00002897
2898 SourceLocation EllipsisLoc;
2899 if (Tok.is(tok::ellipsis))
2900 EllipsisLoc = ConsumeToken();
2901
2902 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002903 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00002904 T.getOpenLocation(), ArgExprs,
2905 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002906 }
2907
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002908 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redl3da34892011-06-05 12:23:16 +00002909 : diag::err_expected_lparen);
2910 return true;
Douglas Gregore8381c02008-11-05 04:29:56 +00002911}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002912
Sebastian Redl965b0e32011-03-05 14:45:16 +00002913/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002914///
Douglas Gregor356513d2008-12-01 18:00:20 +00002915/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002916/// dynamic-exception-specification
2917/// noexcept-specification
2918///
2919/// noexcept-specification:
2920/// 'noexcept'
2921/// 'noexcept' '(' constant-expression ')'
2922ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00002923Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00002924 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002925 SmallVectorImpl<ParsedType> &DynamicExceptions,
2926 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00002927 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002928 ExceptionSpecificationType Result = EST_None;
2929
2930 // See if there's a dynamic specification.
2931 if (Tok.is(tok::kw_throw)) {
2932 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2933 DynamicExceptions,
2934 DynamicExceptionRanges);
2935 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2936 "Produced different number of exception types and ranges.");
2937 }
2938
2939 // If there's no noexcept specification, we're done.
2940 if (Tok.isNot(tok::kw_noexcept))
2941 return Result;
2942
Richard Smithb15c11c2011-10-17 23:06:20 +00002943 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2944
Sebastian Redl965b0e32011-03-05 14:45:16 +00002945 // If we already had a dynamic specification, parse the noexcept for,
2946 // recovery, but emit a diagnostic and don't store the results.
2947 SourceRange NoexceptRange;
2948 ExceptionSpecificationType NoexceptType = EST_None;
2949
2950 SourceLocation KeywordLoc = ConsumeToken();
2951 if (Tok.is(tok::l_paren)) {
2952 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002953 BalancedDelimiterTracker T(*this, tok::l_paren);
2954 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002955 NoexceptType = EST_ComputedNoexcept;
2956 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002957 // The argument must be contextually convertible to bool. We use
2958 // ActOnBooleanCondition for this purpose.
2959 if (!NoexceptExpr.isInvalid())
2960 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2961 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002962 T.consumeClose();
2963 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002964 } else {
2965 // There is no argument.
2966 NoexceptType = EST_BasicNoexcept;
2967 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2968 }
2969
2970 if (Result == EST_None) {
2971 SpecificationRange = NoexceptRange;
2972 Result = NoexceptType;
2973
2974 // If there's a dynamic specification after a noexcept specification,
2975 // parse that and ignore the results.
2976 if (Tok.is(tok::kw_throw)) {
2977 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2978 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2979 DynamicExceptionRanges);
2980 }
2981 } else {
2982 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2983 }
2984
2985 return Result;
2986}
2987
Richard Smith8ca78a12013-06-13 02:02:51 +00002988static void diagnoseDynamicExceptionSpecification(
2989 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2990 if (P.getLangOpts().CPlusPlus11) {
2991 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2992 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2993 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2994 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2995 }
2996}
2997
Sebastian Redl965b0e32011-03-05 14:45:16 +00002998/// ParseDynamicExceptionSpecification - Parse a C++
2999/// dynamic-exception-specification (C++ [except.spec]).
3000///
3001/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003002/// 'throw' '(' type-id-list [opt] ')'
3003/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003004///
Douglas Gregor356513d2008-12-01 18:00:20 +00003005/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003006/// type-id ... [opt]
3007/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003008///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003009ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3010 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003011 SmallVectorImpl<ParsedType> &Exceptions,
3012 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003013 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003014
Sebastian Redl965b0e32011-03-05 14:45:16 +00003015 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003016 BalancedDelimiterTracker T(*this, tok::l_paren);
3017 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003018 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3019 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003020 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003021 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003022
Douglas Gregor356513d2008-12-01 18:00:20 +00003023 // Parse throw(...), a Microsoft extension that means "this function
3024 // can throw anything".
3025 if (Tok.is(tok::ellipsis)) {
3026 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003027 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003028 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003029 T.consumeClose();
3030 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003031 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003032 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003033 }
3034
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003035 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003036 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003037 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003038 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003039
Douglas Gregor830837d2010-12-20 23:57:46 +00003040 if (Tok.is(tok::ellipsis)) {
3041 // C++0x [temp.variadic]p5:
3042 // - In a dynamic-exception-specification (15.4); the pattern is a
3043 // type-id.
3044 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003045 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003046 if (!Res.isInvalid())
3047 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3048 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003049
Sebastian Redld6434562009-05-29 18:02:33 +00003050 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003051 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003052 Ranges.push_back(Range);
3053 }
Douglas Gregor830837d2010-12-20 23:57:46 +00003054
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003055 if (Tok.is(tok::comma))
3056 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003057 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003058 break;
3059 }
3060
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003061 T.consumeClose();
3062 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003063 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3064 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003065 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003066}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003067
Douglas Gregor7fb25412010-10-01 18:44:50 +00003068/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3069/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003070TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003071 assert(Tok.is(tok::arrow) && "expected arrow");
3072
3073 ConsumeToken();
3074
Richard Smithbfdb1082012-03-12 08:56:40 +00003075 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003076}
3077
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003078/// \brief We have just started parsing the definition of a new class,
3079/// so push that class onto our stack of classes that is currently
3080/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003081Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003082Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3083 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003084 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003085 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003086 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003087 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003088}
3089
3090/// \brief Deallocate the given parsed class and all of its nested
3091/// classes.
3092void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003093 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3094 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003095 delete Class;
3096}
3097
3098/// \brief Pop the top class of the stack of classes that are
3099/// currently being parsed.
3100///
3101/// This routine should be called when we have finished parsing the
3102/// definition of a class, but have not yet popped the Scope
3103/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003104void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003105 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003106
John McCallc1465822011-02-14 07:13:47 +00003107 Actions.PopParsingClass(state);
3108
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003109 ParsingClass *Victim = ClassStack.top();
3110 ClassStack.pop();
3111 if (Victim->TopLevelClass) {
3112 // Deallocate all of the nested classes of this class,
3113 // recursively: we don't need to keep any of this information.
3114 DeallocateParsedClasses(Victim);
3115 return;
Mike Stump11289f42009-09-09 15:08:12 +00003116 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003117 assert(!ClassStack.empty() && "Missing top-level class?");
3118
Douglas Gregorefc46952010-10-12 16:25:54 +00003119 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003120 // The victim is a nested class, but we will not need to perform
3121 // any processing after the definition of this class since it has
3122 // no members whose handling was delayed. Therefore, we can just
3123 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003124 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003125 return;
3126 }
3127
3128 // This nested class has some members that will need to be processed
3129 // after the top-level class is completely defined. Therefore, add
3130 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003131 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003132 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003133 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003134}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003135
Richard Smith3dff2512012-04-10 03:25:07 +00003136/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3137///
3138/// \return the parsed identifier on success, and 0 if the next token is not an
3139/// attribute-token.
3140///
3141/// C++11 [dcl.attr.grammar]p3:
3142/// If a keyword or an alternative token that satisfies the syntactic
3143/// requirements of an identifier is contained in an attribute-token,
3144/// it is considered an identifier.
3145IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3146 switch (Tok.getKind()) {
3147 default:
3148 // Identifiers and keywords have identifier info attached.
3149 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3150 Loc = ConsumeToken();
3151 return II;
3152 }
3153 return 0;
3154
3155 case tok::ampamp: // 'and'
3156 case tok::pipe: // 'bitor'
3157 case tok::pipepipe: // 'or'
3158 case tok::caret: // 'xor'
3159 case tok::tilde: // 'compl'
3160 case tok::amp: // 'bitand'
3161 case tok::ampequal: // 'and_eq'
3162 case tok::pipeequal: // 'or_eq'
3163 case tok::caretequal: // 'xor_eq'
3164 case tok::exclaim: // 'not'
3165 case tok::exclaimequal: // 'not_eq'
3166 // Alternative tokens do not have identifier info, but their spelling
3167 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003168 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003169 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003170 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003171 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003172 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003173 }
3174 return 0;
3175 }
3176}
3177
Michael Han23214e52012-10-03 01:56:22 +00003178static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3179 IdentifierInfo *ScopeName) {
3180 switch (AttributeList::getKind(AttrName, ScopeName,
3181 AttributeList::AS_CXX11)) {
3182 case AttributeList::AT_CarriesDependency:
3183 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003184 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003185 return true;
3186 }
3187
3188 default:
3189 return false;
3190 }
3191}
3192
Richard Smith3dff2512012-04-10 03:25:07 +00003193/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003194/// only parses standard attributes.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003195///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003196/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003197/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003198/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003199///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003200/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003201/// attribute[opt]
3202/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003203/// attribute '...'
3204/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003205///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003206/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003207/// attribute-token attribute-argument-clause[opt]
3208///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003209/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003210/// identifier
3211/// attribute-scoped-token
3212///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003213/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003214/// attribute-namespace '::' identifier
3215///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003216/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003217/// identifier
3218///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003219/// [C++11] attribute-argument-clause:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003220/// '(' balanced-token-seq ')'
3221///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003222/// [C++11] balanced-token-seq:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003223/// balanced-token
3224/// balanced-token-seq balanced-token
3225///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003226/// [C++11] balanced-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003227/// '(' balanced-token-seq ')'
3228/// '[' balanced-token-seq ']'
3229/// '{' balanced-token-seq '}'
3230/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smith3dff2512012-04-10 03:25:07 +00003231void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003232 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003233 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003234 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003235 ParseAlignmentSpecifier(attrs, endLoc);
3236 return;
3237 }
3238
Alexis Hunt96d5c762009-11-21 08:43:09 +00003239 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003240 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003241
Richard Smithf679b5b2011-10-14 20:48:27 +00003242 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3243
Alexis Hunt96d5c762009-11-21 08:43:09 +00003244 ConsumeBracket();
3245 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003246
Richard Smith10876ef2013-01-17 01:30:42 +00003247 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3248
Richard Smith3dff2512012-04-10 03:25:07 +00003249 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003250 // attribute not present
3251 if (Tok.is(tok::comma)) {
3252 ConsumeToken();
3253 continue;
3254 }
3255
Richard Smith3dff2512012-04-10 03:25:07 +00003256 SourceLocation ScopeLoc, AttrLoc;
3257 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3258
3259 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3260 if (!AttrName)
3261 // Break out to the "expected ']'" diagnostic.
3262 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003263
Alexis Hunt96d5c762009-11-21 08:43:09 +00003264 // scoped attribute
3265 if (Tok.is(tok::coloncolon)) {
3266 ConsumeToken();
3267
Richard Smith3dff2512012-04-10 03:25:07 +00003268 ScopeName = AttrName;
3269 ScopeLoc = AttrLoc;
3270
3271 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3272 if (!AttrName) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003273 Diag(Tok.getLocation(), diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003274 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003275 continue;
3276 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003277 }
3278
Michael Han23214e52012-10-03 01:56:22 +00003279 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003280 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003281
Richard Smith10876ef2013-01-17 01:30:42 +00003282 if (StandardAttr &&
3283 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3284 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3285 << AttrName << SourceRange(SeenAttrs[AttrName]);
3286
Michael Han23214e52012-10-03 01:56:22 +00003287 // Parse attribute arguments
3288 if (Tok.is(tok::l_paren)) {
3289 if (ScopeName && ScopeName->getName() == "gnu") {
3290 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3291 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3292 AttrParsed = true;
3293 } else {
3294 if (StandardAttr)
3295 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3296 << AttrName->getName();
3297
3298 // FIXME: handle other formats of c++11 attribute arguments
3299 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003300 SkipUntil(tok::r_paren);
Michael Han23214e52012-10-03 01:56:22 +00003301 }
3302 }
3303
3304 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003305 attrs.addNew(AttrName,
3306 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3307 AttrLoc),
Aaron Ballman00e99962013-08-31 01:11:41 +00003308 ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003309
Richard Smith3dff2512012-04-10 03:25:07 +00003310 if (Tok.is(tok::ellipsis)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003311 ConsumeToken();
Michael Han23214e52012-10-03 01:56:22 +00003312
3313 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3314 << AttrName->getName();
Richard Smith3dff2512012-04-10 03:25:07 +00003315 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003316 }
3317
3318 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003319 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003320 if (endLoc)
3321 *endLoc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003322 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003323 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003324}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003325
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003326/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003327///
3328/// attribute-specifier-seq:
3329/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003330void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003331 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003332 assert(getLangOpts().CPlusPlus11);
3333
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003334 SourceLocation StartLoc = Tok.getLocation(), Loc;
3335 if (!endLoc)
3336 endLoc = &Loc;
3337
Douglas Gregor6f981002011-10-07 20:35:25 +00003338 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003339 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003340 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003341
3342 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003343}
3344
Richard Smithc2c8bb82013-10-15 01:34:54 +00003345void Parser::DiagnoseAndSkipCXX11Attributes() {
3346 if (!isCXX11AttributeSpecifier())
3347 return;
3348
3349 // Start and end location of an attribute or an attribute list.
3350 SourceLocation StartLoc = Tok.getLocation();
3351 SourceLocation EndLoc;
3352
3353 do {
3354 if (Tok.is(tok::l_square)) {
3355 BalancedDelimiterTracker T(*this, tok::l_square);
3356 T.consumeOpen();
3357 T.skipToEnd();
3358 EndLoc = T.getCloseLocation();
3359 } else {
3360 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3361 ConsumeToken();
3362 BalancedDelimiterTracker T(*this, tok::l_paren);
3363 if (!T.consumeOpen())
3364 T.skipToEnd();
3365 EndLoc = T.getCloseLocation();
3366 }
3367 } while (isCXX11AttributeSpecifier());
3368
3369 if (EndLoc.isValid()) {
3370 SourceRange Range(StartLoc, EndLoc);
3371 Diag(StartLoc, diag::err_attributes_not_allowed)
3372 << Range;
3373 }
3374}
3375
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003376/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3377///
3378/// [MS] ms-attribute:
3379/// '[' token-seq ']'
3380///
3381/// [MS] ms-attribute-seq:
3382/// ms-attribute[opt]
3383/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003384void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3385 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003386 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3387
3388 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003389 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003390 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003391 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003392 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003393 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3394 }
3395}
Francois Pichet8f981d52011-05-25 10:19:49 +00003396
3397void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3398 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003399 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003400 if (ParseMicrosoftIfExistsCondition(Result))
3401 return;
3402
Douglas Gregor43edb322011-10-24 22:31:10 +00003403 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3404 if (Braces.consumeOpen()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003405 Diag(Tok, diag::err_expected_lbrace);
3406 return;
3407 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003408
Douglas Gregor43edb322011-10-24 22:31:10 +00003409 switch (Result.Behavior) {
3410 case IEB_Parse:
3411 // Parse the declarations below.
3412 break;
3413
3414 case IEB_Dependent:
3415 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3416 << Result.IsIfExists;
3417 // Fall through to skip.
3418
3419 case IEB_Skip:
3420 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003421 return;
3422 }
3423
Richard Smith34f30512013-11-23 04:06:09 +00003424 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003425 // __if_exists, __if_not_exists can nest.
3426 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3427 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3428 continue;
3429 }
3430
3431 // Check for extraneous top-level semicolon.
3432 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003433 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003434 continue;
3435 }
3436
3437 AccessSpecifier AS = getAccessSpecifierIfPresent();
3438 if (AS != AS_none) {
3439 // Current token is a C++ access specifier.
3440 CurAS = AS;
3441 SourceLocation ASLoc = Tok.getLocation();
3442 ConsumeToken();
3443 if (Tok.is(tok::colon))
3444 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3445 else
3446 Diag(Tok, diag::err_expected_colon);
3447 ConsumeToken();
3448 continue;
3449 }
3450
3451 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003452 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet8f981d52011-05-25 10:19:49 +00003453 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003454
3455 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003456}