blob: 91b55d5b58729a8310251c5b9146ce6c061d2360 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000022using namespace clang;
23
24/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000025/// may either be a top level namespace or a block-level namespace alias. If
26/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000027///
28/// namespace-definition: [C++ 7.3: basic.namespace]
29/// named-namespace-definition
30/// unnamed-namespace-definition
31///
32/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000033/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000034///
35/// named-namespace-definition:
36/// original-namespace-definition
37/// extension-namespace-definition
38///
39/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' identifier attributes[opt]
41/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000042///
43/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' original-namespace-name
45/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000046///
Chris Lattner8f08cb72007-08-25 06:57:03 +000047/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
48/// 'namespace' identifier '=' qualified-namespace-specifier ';'
49///
John McCalld226f652010-08-21 09:40:31 +000050Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000051 SourceLocation &DeclEnd,
52 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000053 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000054 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000055
Douglas Gregor49f40bd2009-09-18 19:03:04 +000056 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000057 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +000058 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +000059 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000060
Chris Lattner8f08cb72007-08-25 06:57:03 +000061 SourceLocation IdentLoc;
62 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000063
64 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner04d66662007-10-09 17:33:22 +000066 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 Ident = Tok.getIdentifierInfo();
68 IdentLoc = ConsumeToken(); // eat the identifier.
69 }
Mike Stump1eb44332009-09-09 15:08:12 +000070
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000072 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000075 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000076 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Douglas Gregor6a588dd2009-06-17 19:49:00 +000078 if (Tok.is(tok::equal)) {
John McCall7f040a92010-12-24 02:08:15 +000079 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000081 if (InlineLoc.isValid())
82 Diag(InlineLoc, diag::err_inline_namespace_alias)
83 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000084
Chris Lattner97144fc2009-04-02 04:16:50 +000085 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner51448322009-03-29 14:02:43 +000088 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000089 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000090 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +000091 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner51448322009-03-29 14:02:43 +000094 SourceLocation LBrace = ConsumeBrace();
95
Douglas Gregor23c94db2010-07-02 17:43:08 +000096 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
97 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
98 getCurScope()->getFnParent()) {
Douglas Gregor95f1b152010-05-14 05:08:22 +000099 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
100 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000101 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000102 }
103
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000104 // If we're still good, complain about inline namespaces in non-C++0x now.
105 if (!getLang().CPlusPlus0x && InlineLoc.isValid())
106 Diag(InlineLoc, diag::ext_inline_namespace);
107
Chris Lattner51448322009-03-29 14:02:43 +0000108 // Enter a scope for the namespace.
109 ParseScope NamespaceScope(this, Scope::DeclScope);
110
John McCalld226f652010-08-21 09:40:31 +0000111 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000112 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
113 IdentLoc, Ident, LBrace, attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000114
John McCallf312b1e2010-08-26 23:41:50 +0000115 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
116 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Sean Huntbbd37c62009-11-21 08:43:09 +0000118 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000119 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000120 MaybeParseCXX0XAttributes(attrs);
121 MaybeParseMicrosoftAttributes(attrs);
122 ParseExternalDeclaration(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner51448322009-03-29 14:02:43 +0000125 // Leave the namespace scope.
126 NamespaceScope.Exit();
127
Chris Lattner97144fc2009-04-02 04:16:50 +0000128 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
129 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000130
Chris Lattner97144fc2009-04-02 04:16:50 +0000131 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000132 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000133}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000134
Anders Carlssonf67606a2009-03-28 04:07:16 +0000135/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
136/// alias definition.
137///
John McCalld226f652010-08-21 09:40:31 +0000138Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000139 SourceLocation AliasLoc,
140 IdentifierInfo *Alias,
141 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000142 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Anders Carlssonf67606a2009-03-28 04:07:16 +0000144 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000146 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000147 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000148 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000149 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000150
Anders Carlssonf67606a2009-03-28 04:07:16 +0000151 CXXScopeSpec SS;
152 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000153 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000154
155 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
156 Diag(Tok, diag::err_expected_namespace_name);
157 // Skip to end of the definition and eat the ';'.
158 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000159 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000160 }
161
162 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000163 IdentifierInfo *Ident = Tok.getIdentifierInfo();
164 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Anders Carlssonf67606a2009-03-28 04:07:16 +0000166 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000167 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000168 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
169 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Douglas Gregor23c94db2010-07-02 17:43:08 +0000171 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000172 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000173}
174
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000175/// ParseLinkage - We know that the current token is a string_literal
176/// and just before that, that extern was seen.
177///
178/// linkage-specification: [C++ 7.5p2: dcl.link]
179/// 'extern' string-literal '{' declaration-seq[opt] '}'
180/// 'extern' string-literal declaration
181///
Chris Lattner7d642712010-11-09 20:15:55 +0000182Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000183 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000184 llvm::SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000185 bool Invalid = false;
186 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
187 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000188 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000189
190 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000191
Douglas Gregor074149e2009-01-05 19:45:36 +0000192 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000193 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000194 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000195 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000196 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000197 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000198 : SourceLocation());
199
John McCall0b7e6782011-03-24 11:26:52 +0000200 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000201 MaybeParseCXX0XAttributes(attrs);
202 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000203
Douglas Gregor074149e2009-01-05 19:45:36 +0000204 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000205 // Reset the source range in DS, as the leading "extern"
206 // does not really belong to the inner declaration ...
207 DS.SetRangeStart(SourceLocation());
208 DS.SetRangeEnd(SourceLocation());
209 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000210 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000211 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000212 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000213 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000214 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000215
Douglas Gregor63a01132010-02-07 08:38:28 +0000216 DS.abort();
217
John McCall7f040a92010-12-24 02:08:15 +0000218 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000219
Douglas Gregorf44515a2008-12-16 22:23:02 +0000220 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000221 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000222 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000223 MaybeParseCXX0XAttributes(attrs);
224 MaybeParseMicrosoftAttributes(attrs);
225 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000226 }
227
Douglas Gregorf44515a2008-12-16 22:23:02 +0000228 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Chris Lattner7d642712010-11-09 20:15:55 +0000229 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
230 RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000231}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000232
Douglas Gregorf780abc2008-12-30 03:27:21 +0000233/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
234/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000235Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000236 const ParsedTemplateInfo &TemplateInfo,
237 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000238 ParsedAttributesWithRange &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000239 assert(Tok.is(tok::kw_using) && "Not using token");
240
241 // Eat 'using'.
242 SourceLocation UsingLoc = ConsumeToken();
243
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000244 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000245 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000246 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000247 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000248
John McCall78b81052010-11-10 02:40:36 +0000249 // 'using namespace' means this is a using-directive.
250 if (Tok.is(tok::kw_namespace)) {
251 // Template parameters are always an error here.
252 if (TemplateInfo.Kind) {
253 SourceRange R = TemplateInfo.getSourceRange();
254 Diag(UsingLoc, diag::err_templated_using_directive)
255 << R << FixItHint::CreateRemoval(R);
256 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000257
John McCall7f040a92010-12-24 02:08:15 +0000258 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000259 }
260
Richard Smith162e1c12011-04-15 14:24:37 +0000261 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000262
263 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000264 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000265
John McCall78b81052010-11-10 02:40:36 +0000266 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000267}
268
269/// ParseUsingDirective - Parse C++ using-directive, assumes
270/// that current token is 'namespace' and 'using' was already parsed.
271///
272/// using-directive: [C++ 7.3.p4: namespace.udir]
273/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
274/// namespace-name ;
275/// [GNU] using-directive:
276/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
277/// namespace-name attributes[opt] ;
278///
John McCalld226f652010-08-21 09:40:31 +0000279Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000280 SourceLocation UsingLoc,
281 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000282 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000283 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
284
285 // Eat 'namespace'.
286 SourceLocation NamespcLoc = ConsumeToken();
287
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000288 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000289 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000290 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000291 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000292
Douglas Gregorf780abc2008-12-30 03:27:21 +0000293 CXXScopeSpec SS;
294 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000295 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000296
Douglas Gregorf780abc2008-12-30 03:27:21 +0000297 IdentifierInfo *NamespcName = 0;
298 SourceLocation IdentLoc = SourceLocation();
299
300 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000301 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000302 Diag(Tok, diag::err_expected_namespace_name);
303 // If there was invalid namespace name, skip to end of decl, and eat ';'.
304 SkipUntil(tok::semi);
305 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000306 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattner823c44e2009-01-06 07:27:21 +0000309 // Parse identifier.
310 NamespcName = Tok.getIdentifierInfo();
311 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Chris Lattner823c44e2009-01-06 07:27:21 +0000313 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000314 bool GNUAttr = false;
315 if (Tok.is(tok::kw___attribute)) {
316 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000317 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000318 }
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Chris Lattner823c44e2009-01-06 07:27:21 +0000320 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000321 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000322 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000323 GNUAttr ? diag::err_expected_semi_after_attribute_list
324 : diag::err_expected_semi_after_namespace_name,
325 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000326
Douglas Gregor23c94db2010-07-02 17:43:08 +0000327 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000328 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000329}
330
Richard Smith162e1c12011-04-15 14:24:37 +0000331/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
332/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000333///
334/// using-declaration: [C++ 7.3.p3: namespace.udecl]
335/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000336/// unqualified-id
337/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000338///
Richard Smith162e1c12011-04-15 14:24:37 +0000339/// alias-declaration: C++0x [decl.typedef]p2
340/// 'using' identifier = type-id ;
341///
John McCalld226f652010-08-21 09:40:31 +0000342Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000343 const ParsedTemplateInfo &TemplateInfo,
344 SourceLocation UsingLoc,
345 SourceLocation &DeclEnd,
346 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000347 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000348 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000349 bool IsTypeName;
350
351 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000352 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000353 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000354 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000355 ConsumeToken();
356 IsTypeName = true;
357 }
358 else
359 IsTypeName = false;
360
361 // Parse nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000362 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000363
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000364 // Check nested-name specifier.
365 if (SS.isInvalid()) {
366 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000367 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000368 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000369
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000370 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000371 // destructor names and allow the action module to diagnose any semantic
372 // errors.
373 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000374 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000375 /*EnteringContext=*/false,
376 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000377 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000378 ParsedType(),
Douglas Gregor12c118a2009-11-04 16:30:06 +0000379 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000380 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000381 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000382 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000383
John McCall0b7e6782011-03-24 11:26:52 +0000384 ParsedAttributes attrs(AttrFactory);
Richard Smith162e1c12011-04-15 14:24:37 +0000385
386 // Maybe this is an alias-declaration.
387 bool IsAliasDecl = Tok.is(tok::equal);
388 TypeResult TypeAlias;
389 if (IsAliasDecl) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000390 // TODO: Attribute support. C++0x attributes may appear before the equals.
391 // Where can GNU attributes appear?
Richard Smith162e1c12011-04-15 14:24:37 +0000392 ConsumeToken();
393
394 if (!getLang().CPlusPlus0x)
395 Diag(Tok.getLocation(), diag::ext_alias_declaration);
396
Richard Smith3e4c6c42011-05-05 21:57:07 +0000397 // Type alias templates cannot be specialized.
398 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000399 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
400 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000401 SpecKind = 0;
402 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
403 SpecKind = 1;
404 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
405 SpecKind = 2;
406 if (SpecKind != -1) {
407 SourceRange Range;
408 if (SpecKind == 0)
409 Range = SourceRange(Name.TemplateId->LAngleLoc,
410 Name.TemplateId->RAngleLoc);
411 else
412 Range = TemplateInfo.getSourceRange();
413 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
414 << SpecKind << Range;
415 SkipUntil(tok::semi);
416 return 0;
417 }
418
Richard Smith162e1c12011-04-15 14:24:37 +0000419 // Name must be an identifier.
420 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
421 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
422 // No removal fixit: can't recover from this.
423 SkipUntil(tok::semi);
424 return 0;
425 } else if (IsTypeName)
426 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
427 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
428 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
429 else if (SS.isNotEmpty())
430 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
431 << FixItHint::CreateRemoval(SS.getRange());
432
Richard Smith3e4c6c42011-05-05 21:57:07 +0000433 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
434 Declarator::AliasTemplateContext :
435 Declarator::AliasDeclContext);
Richard Smith162e1c12011-04-15 14:24:37 +0000436 } else
437 // Parse (optional) attributes (most likely GNU strong-using extension).
438 MaybeParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000440 // Eat ';'.
441 DeclEnd = Tok.getLocation();
442 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000443 !attrs.empty() ? "attributes list" :
444 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000445 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000446
John McCall78b81052010-11-10 02:40:36 +0000447 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000448 // In C++0x, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000449 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000450 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000451 SourceRange R = TemplateInfo.getSourceRange();
452 Diag(UsingLoc, diag::err_templated_using_declaration)
453 << R << FixItHint::CreateRemoval(R);
454
455 // Unfortunately, we have to bail out instead of recovering by
456 // ignoring the parameters, just in case the nested name specifier
457 // depends on the parameters.
458 return 0;
459 }
460
Richard Smith3e4c6c42011-05-05 21:57:07 +0000461 if (IsAliasDecl) {
462 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
463 MultiTemplateParamsArg TemplateParamsArg(Actions,
464 TemplateParams ? TemplateParams->data() : 0,
465 TemplateParams ? TemplateParams->size() : 0);
466 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
467 UsingLoc, Name, TypeAlias);
468 }
Richard Smith162e1c12011-04-15 14:24:37 +0000469
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000470 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000471 Name, attrs.getList(),
472 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000473}
474
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000475/// ParseStaticAssertDeclaration - Parse C++0x or C1X static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000476///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000477/// [C++0x] static_assert-declaration:
478/// static_assert ( constant-expression , string-literal ) ;
479///
480/// [C1X] static_assert-declaration:
481/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000482///
John McCalld226f652010-08-21 09:40:31 +0000483Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000484 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
485 "Not a static_assert declaration");
486
487 if (Tok.is(tok::kw__Static_assert) && !getLang().C1X)
488 Diag(Tok, diag::ext_c1x_static_assert);
489
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000490 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000492 if (Tok.isNot(tok::l_paren)) {
493 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000494 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000497 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000498
John McCall60d7b3a2010-08-24 06:29:42 +0000499 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000500 if (AssertExpr.isInvalid()) {
501 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000502 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Anders Carlssonad5f9602009-03-13 23:29:20 +0000505 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000506 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000507
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000508 if (Tok.isNot(tok::string_literal)) {
509 Diag(Tok, diag::err_expected_string_literal);
510 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000511 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
John McCall60d7b3a2010-08-24 06:29:42 +0000514 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000515 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000516 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000517
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000518 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Chris Lattner97144fc2009-04-02 04:16:50 +0000520 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000521 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000522
John McCall9ae2f072010-08-23 23:25:46 +0000523 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
524 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000525 AssertMessage.take(),
526 RParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000527}
528
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000529/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
530///
531/// 'decltype' ( expression )
532///
533void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
534 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
535
536 SourceLocation StartLoc = ConsumeToken();
537 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000538
539 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000540 "decltype")) {
541 SkipUntil(tok::r_paren);
542 return;
543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000545 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000547 // C++0x [dcl.type.simple]p4:
548 // The operand of the decltype specifier is an unevaluated operand.
549 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000550 Sema::Unevaluated);
John McCall60d7b3a2010-08-24 06:29:42 +0000551 ExprResult Result = ParseExpression();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000552 if (Result.isInvalid()) {
553 SkipUntil(tok::r_paren);
554 return;
555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000557 // Match the ')'
558 SourceLocation RParenLoc;
559 if (Tok.is(tok::r_paren))
560 RParenLoc = ConsumeParen();
561 else
562 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000564 if (RParenLoc.isInvalid())
565 return;
566
567 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000568 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000569 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000570 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000571 DiagID, Result.release()))
572 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000573}
574
Douglas Gregor42a552f2008-11-05 20:51:48 +0000575/// ParseClassName - Parse a C++ class-name, which names a class. Note
576/// that we only check that the result names a type; semantic analysis
577/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000578/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000579/// found.
580///
581/// class-name: [C++ 9.1]
582/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000583/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000584///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000585Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +0000586 CXXScopeSpec &SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000587 // Check whether we have a template-id that names a type.
588 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000589 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000590 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000591 if (TemplateId->Kind == TNK_Type_template ||
592 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000593 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000594
595 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000596 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000597 EndLocation = Tok.getAnnotationEndLoc();
598 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000599
600 if (Type)
601 return Type;
602 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000603 }
604
605 // Fall through to produce an error below.
606 }
607
Douglas Gregor42a552f2008-11-05 20:51:48 +0000608 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000609 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000610 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000611 }
612
Douglas Gregor84d0a192010-01-12 21:28:44 +0000613 IdentifierInfo *Id = Tok.getIdentifierInfo();
614 SourceLocation IdLoc = ConsumeToken();
615
616 if (Tok.is(tok::less)) {
617 // It looks the user intended to write a template-id here, but the
618 // template-name was wrong. Try to fix that.
619 TemplateNameKind TNK = TNK_Type_template;
620 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000621 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000622 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000623 Diag(IdLoc, diag::err_unknown_template_name)
624 << Id;
625 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000626
Douglas Gregor84d0a192010-01-12 21:28:44 +0000627 if (!Template)
628 return true;
629
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000630 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000631 UnqualifiedId TemplateName;
632 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000633
Douglas Gregor84d0a192010-01-12 21:28:44 +0000634 // Parse the full template-id, then turn it into a type.
635 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
636 SourceLocation(), true))
637 return true;
638 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000639 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000640
Douglas Gregor84d0a192010-01-12 21:28:44 +0000641 // If we didn't end up with a typename token, there's nothing more we
642 // can do.
643 if (Tok.isNot(tok::annot_typename))
644 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000645
Douglas Gregor84d0a192010-01-12 21:28:44 +0000646 // Retrieve the type from the annotation token, consume that token, and
647 // return.
648 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000649 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000650 ConsumeToken();
651 return Type;
652 }
653
Douglas Gregor42a552f2008-11-05 20:51:48 +0000654 // We have an identifier; check whether it is actually a type.
Douglas Gregor059101f2011-03-02 00:47:37 +0000655 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000656 false, ParsedType(),
657 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000658 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000659 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000660 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000661 }
662
663 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000664 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000665
666 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000667 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000668 DS.SetRangeStart(IdLoc);
669 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000670 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000671
672 const char *PrevSpec = 0;
673 unsigned DiagID;
674 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
675
676 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
677 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000678}
679
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000680/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
681/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
682/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000683/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000684///
685/// class-specifier: [C++ class]
686/// class-head '{' member-specification[opt] '}'
687/// class-head '{' member-specification[opt] '}' attributes[opt]
688/// class-head:
689/// class-key identifier[opt] base-clause[opt]
690/// class-key nested-name-specifier identifier base-clause[opt]
691/// class-key nested-name-specifier[opt] simple-template-id
692/// base-clause[opt]
693/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000694/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000695/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000696/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000697/// simple-template-id base-clause[opt]
698/// class-key:
699/// 'class'
700/// 'struct'
701/// 'union'
702///
703/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000704/// class-key ::[opt] nested-name-specifier[opt] identifier
705/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
706/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000707///
708/// Note that the C++ class-specifier and elaborated-type-specifier,
709/// together, subsume the C99 struct-or-union-specifier:
710///
711/// struct-or-union-specifier: [C99 6.7.2.1]
712/// struct-or-union identifier[opt] '{' struct-contents '}'
713/// struct-or-union identifier
714/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
715/// '}' attributes[opt]
716/// [GNU] struct-or-union attributes[opt] identifier
717/// struct-or-union:
718/// 'struct'
719/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000720void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
721 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000722 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000723 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000724 DeclSpec::TST TagType;
725 if (TagTokKind == tok::kw_struct)
726 TagType = DeclSpec::TST_struct;
727 else if (TagTokKind == tok::kw_class)
728 TagType = DeclSpec::TST_class;
729 else {
730 assert(TagTokKind == tok::kw_union && "Not a class specifier");
731 TagType = DeclSpec::TST_union;
732 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000733
Douglas Gregor374929f2009-09-18 15:37:17 +0000734 if (Tok.is(tok::code_completion)) {
735 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000736 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000737 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000738 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000739
Chandler Carruth926c4b42010-06-28 08:39:25 +0000740 // C++03 [temp.explicit] 14.7.2/8:
741 // The usual access checking rules do not apply to names used to specify
742 // explicit instantiations.
743 //
744 // As an extension we do not perform access checking on the names used to
745 // specify explicit specializations either. This is important to allow
746 // specializing traits classes for private types.
747 bool SuppressingAccessChecks = false;
748 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
749 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
750 Actions.ActOnStartSuppressingAccessChecks();
751 SuppressingAccessChecks = true;
752 }
753
John McCall0b7e6782011-03-24 11:26:52 +0000754 ParsedAttributes attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000755 // If attributes exist after tag, parse them.
756 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +0000757 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000758
Steve Narofff59e17e2008-12-24 20:59:21 +0000759 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000760 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +0000761 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000762
Sean Huntbbd37c62009-11-21 08:43:09 +0000763 // If C++0x attributes exist here, parse them.
764 // FIXME: Are we consistent with the ordering of parsing of different
765 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +0000766 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
John Wiegley20c0da72011-04-27 23:09:49 +0000768 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +0000769 !Tok.is(tok::identifier) &&
770 Tok.getIdentifierInfo() &&
771 (Tok.is(tok::kw___is_arithmetic) ||
772 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +0000773 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000774 Tok.is(tok::kw___is_floating_point) ||
775 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +0000776 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000777 Tok.is(tok::kw___is_integral) ||
778 Tok.is(tok::kw___is_member_function_pointer) ||
779 Tok.is(tok::kw___is_member_pointer) ||
780 Tok.is(tok::kw___is_pod) ||
781 Tok.is(tok::kw___is_pointer) ||
782 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +0000783 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000784 Tok.is(tok::kw___is_signed) ||
785 Tok.is(tok::kw___is_unsigned) ||
786 Tok.is(tok::kw___is_void))) {
787 // GNU libstdc++ 4.2 and libc++ uaw certain intrinsic names as the
788 // name of struct templates, but some are keywords in GCC >= 4.3
789 // and Clang. Therefore, when we see the token sequence "struct
790 // X", make X into a normal identifier rather than a keyword, to
791 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000792 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000793 Tok.setKind(tok::identifier);
794 }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000796 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000797 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000798 if (getLang().CPlusPlus) {
799 // "FOO : BAR" is not a potential typo for "FOO::BAR".
800 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000801
John McCallb3d87482010-08-24 05:47:05 +0000802 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall207014e2010-07-30 06:26:29 +0000803 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000804 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000805 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
806 Diag(Tok, diag::err_expected_ident);
807 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000808
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000809 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
810
Douglas Gregorcc636682009-02-17 23:15:12 +0000811 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000812 IdentifierInfo *Name = 0;
813 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000814 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000815 if (Tok.is(tok::identifier)) {
816 Name = Tok.getIdentifierInfo();
817 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000818
Douglas Gregor5ee37342010-05-30 22:30:21 +0000819 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000820 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000821 // Eat the template argument list and try to continue parsing this as
822 // a class (or template thereof).
823 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000824 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +0000825 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000826 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000827 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000828 // We couldn't parse the template argument list at all, so don't
829 // try to give any location information for the list.
830 LAngleLoc = RAngleLoc = SourceLocation();
831 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000832
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000833 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000834 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000835 << (TagType == DeclSpec::TST_class? 0
836 : TagType == DeclSpec::TST_struct? 1
837 : 2)
838 << Name
839 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000840
841 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000842 // we've removed its template argument list.
843 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
844 if (TemplateParams && TemplateParams->size() > 1) {
845 TemplateParams->pop_back();
846 } else {
847 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000848 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000849 = ParsedTemplateInfo::NonTemplate;
850 }
851 } else if (TemplateInfo.Kind
852 == ParsedTemplateInfo::ExplicitInstantiation) {
853 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000854 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000855 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000856 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000857 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000858 = SourceLocation();
859 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
860 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000861 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000862 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000863 } else if (Tok.is(tok::annot_template_id)) {
864 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
865 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000866
Douglas Gregor059101f2011-03-02 00:47:37 +0000867 if (TemplateId->Kind != TNK_Type_template &&
868 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000869 // The template-name in the simple-template-id refers to
870 // something other than a class template. Give an appropriate
871 // error message and skip to the ';'.
872 SourceRange Range(NameLoc);
873 if (SS.isNotEmpty())
874 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000875
Douglas Gregor39a8de12009-02-25 19:37:18 +0000876 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
877 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Douglas Gregor39a8de12009-02-25 19:37:18 +0000879 DS.SetTypeSpecError();
880 SkipUntil(tok::semi, false, true);
881 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000882 if (SuppressingAccessChecks)
883 Actions.ActOnStopSuppressingAccessChecks();
884
Douglas Gregor39a8de12009-02-25 19:37:18 +0000885 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000886 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000887 }
888
Chandler Carruth926c4b42010-06-28 08:39:25 +0000889 // As soon as we're finished parsing the class's template-id, turn access
890 // checking back on.
891 if (SuppressingAccessChecks)
892 Actions.ActOnStopSuppressingAccessChecks();
893
John McCall67d1a672009-08-06 02:15:43 +0000894 // There are four options here. If we have 'struct foo;', then this
895 // is either a forward declaration or a friend declaration, which
Anders Carlssoncc54d592011-01-22 16:56:46 +0000896 // have to be treated differently. If we have 'struct foo {...',
Anders Carlsson1d209272011-03-25 14:55:14 +0000897 // 'struct foo :...' or 'struct foo final[opt]' then this is a
Anders Carlssoncc54d592011-01-22 16:56:46 +0000898 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000899 // However, in some contexts, things look like declarations but are just
900 // references, e.g.
901 // new struct s;
902 // or
903 // &T::operator struct s;
904 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +0000905 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000906 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +0000907 TUK = Sema::TUK_Reference;
Anders Carlssoncc54d592011-01-22 16:56:46 +0000908 else if (Tok.is(tok::l_brace) ||
909 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +0000910 isCXX0XFinalKeyword()) {
Douglas Gregord85bea22009-09-26 06:47:28 +0000911 if (DS.isFriendSpecified()) {
912 // C++ [class.friend]p2:
913 // A class shall not be defined in a friend declaration.
914 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
915 << SourceRange(DS.getFriendSpecLoc());
916
917 // Skip everything up to the semicolon, so that this looks like a proper
918 // friend class (or template thereof) declaration.
919 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +0000920 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +0000921 } else {
922 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +0000923 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +0000924 }
925 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +0000926 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000927 else
John McCallf312b1e2010-08-26 23:41:50 +0000928 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000929
John McCall207014e2010-07-30 06:26:29 +0000930 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +0000931 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +0000932 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
933 // We have a declaration or reference to an anonymous class.
934 Diag(StartLoc, diag::err_anon_type_definition)
935 << DeclSpec::getSpecifierName(TagType);
936 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000937
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000938 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000939
940 if (TemplateId)
941 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000942 return;
943 }
944
Douglas Gregorddc29e12009-02-06 22:42:48 +0000945 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +0000946 DeclResult TagOrTempResult = true; // invalid
947 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000948
Douglas Gregor402abb52009-05-28 23:31:59 +0000949 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000950 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000951 // Explicit specialization, class template partial specialization,
952 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000953 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000954 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000955 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000956 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000957 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000958 // This is an explicit instantiation of a class template.
959 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000960 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000961 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000962 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000963 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000964 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000965 SS,
John McCall2b5289b2010-08-23 07:28:44 +0000966 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000967 TemplateId->TemplateNameLoc,
968 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000969 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000970 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +0000971 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +0000972
973 // Friend template-ids are treated as references unless
974 // they have template headers, in which case they're ill-formed
975 // (FIXME: "template <class T> friend class A<T>::B<int>;").
976 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +0000977 } else if (TUK == Sema::TUK_Reference ||
978 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +0000979 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000980 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType,
981 StartLoc,
982 TemplateId->SS,
983 TemplateId->Template,
984 TemplateId->TemplateNameLoc,
985 TemplateId->LAngleLoc,
986 TemplateArgsPtr,
987 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000988 } else {
989 // This is an explicit specialization or a class template
990 // partial specialization.
991 TemplateParameterLists FakedParamLists;
992
993 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
994 // This looks like an explicit instantiation, because we have
995 // something like
996 //
997 // template class Foo<X>
998 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000999 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001000 // meant to be an explicit specialization, but the user forgot
1001 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001002 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001003
Mike Stump1eb44332009-09-09 15:08:12 +00001004 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001005 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001006 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001007 diag::err_explicit_instantiation_with_definition)
1008 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001009 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001010
1011 // Create a fake template parameter list that contains only
1012 // "template<>", so that we treat this construct as a class
1013 // template specialization.
1014 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001015 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001016 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001017 LAngleLoc,
1018 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001019 LAngleLoc));
1020 TemplateParams = &FakedParamLists;
1021 }
1022
1023 // Build the class template specialization.
1024 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001025 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001026 StartLoc, SS,
John McCall2b5289b2010-08-23 07:28:44 +00001027 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001028 TemplateId->TemplateNameLoc,
1029 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001030 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001031 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001032 attrs.getList(),
John McCallf312b1e2010-08-26 23:41:50 +00001033 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +00001034 TemplateParams? &(*TemplateParams)[0] : 0,
1035 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001036 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001037 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001038 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001039 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001040 // Explicit instantiation of a member of a class template
1041 // specialization, e.g.,
1042 //
1043 // template struct Outer<int>::Inner;
1044 //
1045 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001046 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001047 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001048 TemplateInfo.TemplateLoc,
1049 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001050 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001051 } else if (TUK == Sema::TUK_Friend &&
1052 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1053 TagOrTempResult =
1054 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1055 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001056 Name, NameLoc, attrs.getList(),
John McCall9a34edb2010-10-19 01:40:49 +00001057 MultiTemplateParamsArg(Actions,
1058 TemplateParams? &(*TemplateParams)[0] : 0,
1059 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001060 } else {
1061 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001062 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001063 // FIXME: Diagnose this particular error.
1064 }
1065
John McCallc4e70192009-09-11 04:59:25 +00001066 bool IsDependent = false;
1067
John McCalla25c4082010-10-19 18:40:57 +00001068 // Don't pass down template parameter lists if this is just a tag
1069 // reference. For example, we don't need the template parameters here:
1070 // template <class T> class A *makeA(T t);
1071 MultiTemplateParamsArg TParams;
1072 if (TUK != Sema::TUK_Reference && TemplateParams)
1073 TParams =
1074 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1075
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001076 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001077 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001078 SS, Name, NameLoc, attrs.getList(), AS,
John McCalla25c4082010-10-19 18:40:57 +00001079 TParams, Owned, IsDependent, false,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001080 false, clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001081
1082 // If ActOnTag said the type was dependent, try again with the
1083 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001084 if (IsDependent) {
1085 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001086 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001087 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001088 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001089 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001090
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001091 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001092 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001093 assert(Tok.is(tok::l_brace) ||
Anders Carlssoncc54d592011-01-22 16:56:46 +00001094 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001095 isCXX0XFinalKeyword());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001096 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001097 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001098 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001099 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001100 }
1101
John McCallb3d87482010-08-24 05:47:05 +00001102 const char *PrevSpec = 0;
1103 unsigned DiagID;
1104 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001105 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001106 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1107 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001108 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001109 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001110 Result = DS.SetTypeSpecType(TagType, StartLoc,
1111 NameLoc.isValid() ? NameLoc : StartLoc,
1112 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001113 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001114 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001115 return;
1116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
John McCallb3d87482010-08-24 05:47:05 +00001118 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001119 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001120
Chris Lattner4ed5d912010-02-02 01:23:29 +00001121 // At this point, we've successfully parsed a class-specifier in 'definition'
1122 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1123 // going to look at what comes after it to improve error recovery. If an
1124 // impossible token occurs next, we assume that the programmer forgot a ; at
1125 // the end of the declaration and recover that way.
1126 //
1127 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001128 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001129 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001130 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001131 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001132 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001133 case tok::star: // struct foo {...} * P;
1134 case tok::amp: // struct foo {...} & R = ...
1135 case tok::identifier: // struct foo {...} V ;
1136 case tok::r_paren: //(struct foo {...} ) {4}
1137 case tok::annot_cxxscope: // struct foo {...} a:: b;
1138 case tok::annot_typename: // struct foo {...} a ::b;
1139 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001140 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001141 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001142 ExpectedSemi = false;
1143 break;
1144 // Type qualifiers
1145 case tok::kw_const: // struct foo {...} const x;
1146 case tok::kw_volatile: // struct foo {...} volatile x;
1147 case tok::kw_restrict: // struct foo {...} restrict x;
1148 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001149 // Storage-class specifiers
1150 case tok::kw_static: // struct foo {...} static x;
1151 case tok::kw_extern: // struct foo {...} extern x;
1152 case tok::kw_typedef: // struct foo {...} typedef x;
1153 case tok::kw_register: // struct foo {...} register x;
1154 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001155 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001156 // As shown above, type qualifiers and storage class specifiers absolutely
1157 // can occur after class specifiers according to the grammar. However,
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001158 // almost no one actually writes code like this. If we see one of these,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001159 // it is much more likely that someone missed a semi colon and the
1160 // type/storage class specifier we're seeing is part of the *next*
1161 // intended declaration, as in:
1162 //
1163 // struct foo { ... }
1164 // typedef int X;
1165 //
1166 // We'd really like to emit a missing semicolon error instead of emitting
1167 // an error on the 'int' saying that you can't have two type specifiers in
1168 // the same declaration of X. Because of this, we look ahead past this
1169 // token to see if it's a type specifier. If so, we know the code is
1170 // otherwise invalid, so we can produce the expected semi error.
1171 if (!isKnownToBeTypeSpecifier(NextToken()))
1172 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001173 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001174
1175 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001176 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001177 if (!getLang().CPlusPlus)
1178 ExpectedSemi = false;
1179 break;
1180 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001181
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001182 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001183 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1184 TagType == DeclSpec::TST_class ? "class"
1185 : TagType == DeclSpec::TST_struct? "struct" : "union");
1186 // Push this token back into the preprocessor and change our current token
1187 // to ';' so that the rest of the code recovers as though there were an
1188 // ';' after the definition.
1189 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001190 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001191 }
1192 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001193}
1194
Mike Stump1eb44332009-09-09 15:08:12 +00001195/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001196///
1197/// base-clause : [C++ class.derived]
1198/// ':' base-specifier-list
1199/// base-specifier-list:
1200/// base-specifier '...'[opt]
1201/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001202void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001203 assert(Tok.is(tok::colon) && "Not a base clause");
1204 ConsumeToken();
1205
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001206 // Build up an array of parsed base specifiers.
John McCallca0408f2010-08-23 06:44:23 +00001207 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001208
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001209 while (true) {
1210 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001211 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001212 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001213 // Skip the rest of this base specifier, up until the comma or
1214 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001215 SkipUntil(tok::comma, tok::l_brace, true, true);
1216 } else {
1217 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001218 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001219 }
1220
1221 // If the next token is a comma, consume it and keep reading
1222 // base-specifiers.
1223 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001225 // Consume the comma.
1226 ConsumeToken();
1227 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001228
1229 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001230 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001231}
1232
1233/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1234/// one entry in the base class list of a class specifier, for example:
1235/// class foo : public bar, virtual private baz {
1236/// 'public bar' and 'virtual private baz' are each base-specifiers.
1237///
1238/// base-specifier: [C++ class.derived]
1239/// ::[opt] nested-name-specifier[opt] class-name
1240/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1241/// class-name
1242/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1243/// class-name
John McCalld226f652010-08-21 09:40:31 +00001244Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001245 bool IsVirtual = false;
1246 SourceLocation StartLoc = Tok.getLocation();
1247
1248 // Parse the 'virtual' keyword.
1249 if (Tok.is(tok::kw_virtual)) {
1250 ConsumeToken();
1251 IsVirtual = true;
1252 }
1253
1254 // Parse an (optional) access specifier.
1255 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001256 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001257 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001259 // Parse the 'virtual' keyword (again!), in case it came after the
1260 // access specifier.
1261 if (Tok.is(tok::kw_virtual)) {
1262 SourceLocation VirtualLoc = ConsumeToken();
1263 if (IsVirtual) {
1264 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001265 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001266 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001267 }
1268
1269 IsVirtual = true;
1270 }
1271
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001272 // Parse optional '::' and optional nested-name-specifier.
1273 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001274 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001275
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001276 // The location of the base class itself.
1277 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001278
1279 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001280 SourceLocation EndLocation;
Douglas Gregor059101f2011-03-02 00:47:37 +00001281 TypeResult BaseType = ParseClassName(EndLocation, SS);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001282 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001283 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001285 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1286 // actually part of the base-specifier-list grammar productions, but we
1287 // parse it here for convenience.
1288 SourceLocation EllipsisLoc;
1289 if (Tok.is(tok::ellipsis))
1290 EllipsisLoc = ConsumeToken();
1291
Mike Stump1eb44332009-09-09 15:08:12 +00001292 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001293 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001295 // Notify semantic analysis that we have parsed a complete
1296 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001297 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001298 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001299}
1300
1301/// getAccessSpecifierIfPresent - Determine whether the next token is
1302/// a C++ access-specifier.
1303///
1304/// access-specifier: [C++ class.derived]
1305/// 'private'
1306/// 'protected'
1307/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001308AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001309 switch (Tok.getKind()) {
1310 default: return AS_none;
1311 case tok::kw_private: return AS_private;
1312 case tok::kw_protected: return AS_protected;
1313 case tok::kw_public: return AS_public;
1314 }
1315}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001316
Eli Friedmand33133c2009-07-22 21:45:50 +00001317void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001318 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001319 // We just declared a member function. If this member function
1320 // has any default arguments, we'll need to parse them later.
1321 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001322 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001323 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedmand33133c2009-07-22 21:45:50 +00001324 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1325 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1326 if (!LateMethod) {
1327 // Push this method onto the stack of late-parsed method
1328 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001329 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1330 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001331 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001332
1333 // Add all of the parameters prior to this one (they don't
1334 // have default arguments).
1335 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1336 for (unsigned I = 0; I < ParamIdx; ++I)
1337 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001338 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001339 }
1340
1341 // Add this parameter to the list of parameters (it or may
1342 // not have a default argument).
1343 LateMethod->DefaultArgs.push_back(
1344 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1345 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1346 }
1347 }
1348}
1349
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001350/// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x
1351/// virt-specifier.
1352///
1353/// virt-specifier:
1354/// override
1355/// final
Anders Carlssoncc54d592011-01-22 16:56:46 +00001356VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001357 if (!getLang().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001358 return VirtSpecifiers::VS_None;
1359
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001360 if (Tok.is(tok::identifier)) {
1361 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001362
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001363 // Initialize the contextual keywords.
1364 if (!Ident_final) {
1365 Ident_final = &PP.getIdentifierTable().get("final");
1366 Ident_override = &PP.getIdentifierTable().get("override");
1367 }
1368
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001369 if (II == Ident_override)
1370 return VirtSpecifiers::VS_Override;
1371
1372 if (II == Ident_final)
1373 return VirtSpecifiers::VS_Final;
1374 }
1375
1376 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001377}
1378
1379/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1380///
1381/// virt-specifier-seq:
1382/// virt-specifier
1383/// virt-specifier-seq virt-specifier
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001384void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001385 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001386 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001387 if (Specifier == VirtSpecifiers::VS_None)
1388 return;
1389
1390 // C++ [class.mem]p8:
1391 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001392 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001393 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001394 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1395 << PrevSpec
1396 << FixItHint::CreateRemoval(Tok.getLocation());
1397
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001398 if (!getLang().CPlusPlus0x)
1399 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1400 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001401 ConsumeToken();
1402 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001403}
1404
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001405/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1406/// contextual 'final' keyword.
1407bool Parser::isCXX0XFinalKeyword() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001408 if (!getLang().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001409 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001410
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001411 if (!Tok.is(tok::identifier))
1412 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001413
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001414 // Initialize the contextual keywords.
1415 if (!Ident_final) {
1416 Ident_final = &PP.getIdentifierTable().get("final");
1417 Ident_override = &PP.getIdentifierTable().get("override");
1418 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001419
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001420 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001421}
1422
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001423/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1424///
1425/// member-declaration:
1426/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1427/// function-definition ';'[opt]
1428/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1429/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001430/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001431/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001432/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001433///
1434/// member-declarator-list:
1435/// member-declarator
1436/// member-declarator-list ',' member-declarator
1437///
1438/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001439/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001440/// declarator constant-initializer[opt]
1441/// identifier[opt] ':' constant-expression
1442///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001443/// virt-specifier-seq:
1444/// virt-specifier
1445/// virt-specifier-seq virt-specifier
1446///
1447/// virt-specifier:
1448/// override
1449/// final
1450/// new
1451///
Sebastian Redle2b68332009-04-12 17:16:29 +00001452/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001453/// '= 0'
1454///
1455/// constant-initializer:
1456/// '=' constant-expression
1457///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001458void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001459 const ParsedTemplateInfo &TemplateInfo,
1460 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001461 if (Tok.is(tok::at)) {
1462 if (getLang().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
1463 Diag(Tok, diag::err_at_defs_cxx);
1464 else
1465 Diag(Tok, diag::err_at_in_class);
1466
1467 ConsumeToken();
1468 SkipUntil(tok::r_brace);
1469 return;
1470 }
1471
John McCall60fa3cf2009-12-11 02:10:03 +00001472 // Access declarations.
1473 if (!TemplateInfo.Kind &&
1474 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001475 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001476 Tok.is(tok::annot_cxxscope)) {
1477 bool isAccessDecl = false;
1478 if (NextToken().is(tok::identifier))
1479 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1480 else
1481 isAccessDecl = NextToken().is(tok::kw_operator);
1482
1483 if (isAccessDecl) {
1484 // Collect the scope specifier token we annotated earlier.
1485 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001486 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCall60fa3cf2009-12-11 02:10:03 +00001487
1488 // Try to parse an unqualified-id.
1489 UnqualifiedId Name;
John McCallb3d87482010-08-24 05:47:05 +00001490 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001491 SkipUntil(tok::semi);
1492 return;
1493 }
1494
1495 // TODO: recover from mistakenly-qualified operator declarations.
1496 if (ExpectAndConsume(tok::semi,
1497 diag::err_expected_semi_after,
1498 "access declaration",
1499 tok::semi))
1500 return;
1501
Douglas Gregor23c94db2010-07-02 17:43:08 +00001502 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001503 false, SourceLocation(),
1504 SS, Name,
1505 /* AttrList */ 0,
1506 /* IsTypeName */ false,
1507 SourceLocation());
1508 return;
1509 }
1510 }
1511
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001512 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001513 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001514 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001515 SourceLocation DeclEnd;
1516 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001517 return;
1518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Chris Lattner682bf922009-03-29 16:50:03 +00001520 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001521 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001522 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001523 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001524 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001525 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001526 return;
1527 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001528
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001529 // Handle: member-declaration ::= '__extension__' member-declaration
1530 if (Tok.is(tok::kw___extension__)) {
1531 // __extension__ silences extension warnings in the subexpression.
1532 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1533 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001534 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001535 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001536
Chris Lattner4ed5d912010-02-02 01:23:29 +00001537 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1538 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001539 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001540
John McCall0b7e6782011-03-24 11:26:52 +00001541 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001542 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001543 MaybeParseCXX0XAttributes(attrs);
1544 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001545
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001546 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001547 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001549 // Eat 'using'.
1550 SourceLocation UsingLoc = ConsumeToken();
1551
1552 if (Tok.is(tok::kw_namespace)) {
1553 Diag(UsingLoc, diag::err_using_namespace_in_class);
1554 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001555 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001556 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001557 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001558 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1559 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001560 }
1561 return;
1562 }
1563
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001564 // decl-specifier-seq:
1565 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001566 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001567 DS.takeAttributesFrom(attrs);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001568 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001569
John McCallf312b1e2010-08-26 23:41:50 +00001570 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001571 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1572 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1573
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001574 if (Tok.is(tok::semi)) {
1575 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001576 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00001577 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00001578 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001579 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001580 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001581
John McCall54abf7d2009-11-04 02:18:39 +00001582 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001583 VirtSpecifiers VS;
Francois Pichet6a247472011-05-11 02:14:46 +00001584 ExprResult Init;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001585
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001586 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001587 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1588 ColonProtectionRAIIObject X(*this);
1589
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001590 // Parse the first declarator.
1591 ParseDeclarator(DeclaratorInfo);
1592 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001593 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001594 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001595 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001596 if (Tok.is(tok::semi))
1597 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001598 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001599 }
1600
Nico Weber48673472011-01-28 06:07:34 +00001601 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1602
John Thompson1b2fc0f2009-11-25 22:58:06 +00001603 // If attributes exist after the declarator, but before an '{', parse them.
John McCall7f040a92010-12-24 02:08:15 +00001604 MaybeParseGNUAttributes(DeclaratorInfo);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001605
Francois Pichet6a247472011-05-11 02:14:46 +00001606 // MSVC permits pure specifier on inline functions declared at class scope.
1607 // Hence check for =0 before checking for function definition.
1608 if (getLang().Microsoft && Tok.is(tok::equal) &&
1609 DeclaratorInfo.isFunctionDeclarator() &&
1610 NextToken().is(tok::numeric_constant)) {
1611 ConsumeToken();
1612 Init = ParseInitializer();
1613 if (Init.isInvalid())
1614 SkipUntil(tok::comma, true, true);
1615 }
1616
Sean Hunte4246a62011-05-12 06:15:49 +00001617 bool IsDefinition = false;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001618 // function-definition:
Sean Hunte4246a62011-05-12 06:15:49 +00001619 if (Tok.is(tok::l_brace)) {
1620 IsDefinition = true;
1621 } else if (DeclaratorInfo.isFunctionDeclarator()) {
1622 if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
1623 IsDefinition = true;
1624 } else if (Tok.is(tok::equal)) {
1625 const Token &KW = NextToken();
1626 if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
1627 IsDefinition = true;
1628 }
1629 }
1630
1631 if (IsDefinition) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001632 if (!DeclaratorInfo.isFunctionDeclarator()) {
1633 Diag(Tok, diag::err_func_def_no_params);
1634 ConsumeBrace();
1635 SkipUntil(tok::r_brace, true);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001636
1637 // Consume the optional ';'
1638 if (Tok.is(tok::semi))
1639 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001640 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001641 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001642
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001643 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1644 Diag(Tok, diag::err_function_declared_typedef);
1645 // This recovery skips the entire function body. It would be nice
1646 // to simply call ParseCXXInlineMethodDef() below, however Sema
1647 // assumes the declarator represents a function, not a typedef.
1648 ConsumeBrace();
1649 SkipUntil(tok::r_brace, true);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001650
1651 // Consume the optional ';'
1652 if (Tok.is(tok::semi))
1653 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001654 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001655 }
1656
Francois Pichet6a247472011-05-11 02:14:46 +00001657 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS, Init);
Sean Hunte4246a62011-05-12 06:15:49 +00001658
1659 // Consume the ';' - it's optional unless we have a delete or default
1660 if (Tok.is(tok::semi)) {
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001661 ConsumeToken();
Sean Hunte4246a62011-05-12 06:15:49 +00001662 }
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001663
Chris Lattner682bf922009-03-29 16:50:03 +00001664 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001665 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001666 }
1667
1668 // member-declarator-list:
1669 // member-declarator
1670 // member-declarator-list ',' member-declarator
1671
John McCalld226f652010-08-21 09:40:31 +00001672 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001673 ExprResult BitfieldSize;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001674
1675 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001676 // member-declarator:
1677 // declarator pure-specifier[opt]
1678 // declarator constant-initializer[opt]
1679 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001680 if (Tok.is(tok::colon)) {
1681 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001682 BitfieldSize = ParseConstantExpression();
1683 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001684 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001685 }
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001687 ParseOptionalCXX0XVirtSpecifierSeq(VS);
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001688
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001689 // pure-specifier:
1690 // '= 0'
1691 //
1692 // constant-initializer:
1693 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001694 //
1695 // defaulted/deleted function-definition:
1696 // '=' 'default' [TODO]
1697 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001698 if (Tok.is(tok::equal)) {
1699 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001700 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001701 if (DeclaratorInfo.isFunctionDeclarator())
1702 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1703 << 1 /* delete */;
1704 else
1705 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001706 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001707 if (DeclaratorInfo.isFunctionDeclarator())
1708 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1709 << 1 /* delete */;
1710 else
1711 Diag(ConsumeToken(), diag::err_default_special_members);
Sebastian Redle2b68332009-04-12 17:16:29 +00001712 } else {
1713 Init = ParseInitializer();
1714 if (Init.isInvalid())
1715 SkipUntil(tok::comma, true, true);
1716 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001717 }
1718
Chris Lattnere6563252010-06-13 05:34:18 +00001719 // If a simple-asm-expr is present, parse it.
1720 if (Tok.is(tok::kw_asm)) {
1721 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001722 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001723 if (AsmLabel.isInvalid())
1724 SkipUntil(tok::comma, true, true);
1725
1726 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1727 DeclaratorInfo.SetRangeEnd(Loc);
1728 }
1729
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001730 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001731 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001732
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001733 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001734 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001735 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001736
John McCalld226f652010-08-21 09:40:31 +00001737 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001738 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001739 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001740 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001741 /*IsDefinition*/ false,
1742 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001743 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001744 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001745 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001746 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001747 BitfieldSize.release(),
Sean Hunte4246a62011-05-12 06:15:49 +00001748 VS, Init.release(), false);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001749 }
Chris Lattner682bf922009-03-29 16:50:03 +00001750 if (ThisDecl)
1751 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001752
Douglas Gregor72b505b2008-12-16 21:30:33 +00001753 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001754 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001755 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001756 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001757 }
1758
John McCall54abf7d2009-11-04 02:18:39 +00001759 DeclaratorInfo.complete(ThisDecl);
1760
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001761 // If we don't have a comma, it is either the end of the list (a ';')
1762 // or an error, bail out.
1763 if (Tok.isNot(tok::comma))
1764 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001766 // Consume the comma.
1767 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001769 // Parse the next declarator.
1770 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00001771 VS.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001772 BitfieldSize = 0;
1773 Init = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001775 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00001776 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001777
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001778 if (Tok.isNot(tok::colon))
1779 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001780 }
1781
Chris Lattnerae50d502010-02-02 00:43:15 +00001782 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1783 // Skip to end of block or statement.
1784 SkipUntil(tok::r_brace, true, true);
1785 // If we stopped at a ';', eat it.
1786 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001787 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001788 }
1789
Douglas Gregor23c94db2010-07-02 17:43:08 +00001790 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001791 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001792}
1793
1794/// ParseCXXMemberSpecification - Parse the class definition.
1795///
1796/// member-specification:
1797/// member-declaration member-specification[opt]
1798/// access-specifier ':' member-specification[opt]
1799///
1800void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001801 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001802 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001803 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001804 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001805
John McCallf312b1e2010-08-26 23:41:50 +00001806 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1807 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Douglas Gregor26997fd2010-01-16 20:52:59 +00001809 // Determine whether this is a non-nested class. Note that local
1810 // classes are *not* considered to be nested classes.
1811 bool NonNestedClass = true;
1812 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001813 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001814 if (S->isClassScope()) {
1815 // We're inside a class scope, so this is a nested class.
1816 NonNestedClass = false;
1817 break;
1818 }
1819
1820 if ((S->getFlags() & Scope::FnScope)) {
1821 // If we're in a function or function template declared in the
1822 // body of a class, then this is a local class rather than a
1823 // nested class.
1824 const Scope *Parent = S->getParent();
1825 if (Parent->isTemplateParamScope())
1826 Parent = Parent->getParent();
1827 if (Parent->isClassScope())
1828 break;
1829 }
1830 }
1831 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001832
1833 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001834 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001835
Douglas Gregor6569d682009-05-27 23:11:45 +00001836 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001837 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001838
Douglas Gregorddc29e12009-02-06 22:42:48 +00001839 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001840 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001841
Anders Carlssonb184a182011-03-25 14:46:08 +00001842 SourceLocation FinalLoc;
1843
1844 // Parse the optional 'final' keyword.
1845 if (getLang().CPlusPlus && Tok.is(tok::identifier)) {
1846 IdentifierInfo *II = Tok.getIdentifierInfo();
1847
1848 // Initialize the contextual keywords.
1849 if (!Ident_final) {
1850 Ident_final = &PP.getIdentifierTable().get("final");
1851 Ident_override = &PP.getIdentifierTable().get("override");
1852 }
1853
1854 if (II == Ident_final)
1855 FinalLoc = ConsumeToken();
1856
1857 if (!getLang().CPlusPlus0x)
1858 Diag(FinalLoc, diag::ext_override_control_keyword) << "final";
1859 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001860
John McCallbd0dfa52009-12-19 21:48:58 +00001861 if (Tok.is(tok::colon)) {
1862 ParseBaseClause(TagDecl);
1863
1864 if (!Tok.is(tok::l_brace)) {
1865 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001866
1867 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001868 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001869 return;
1870 }
1871 }
1872
1873 assert(Tok.is(tok::l_brace));
1874
1875 SourceLocation LBraceLoc = ConsumeBrace();
1876
John McCall42a4f662010-05-28 08:11:17 +00001877 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00001878 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Anders Carlssondfc2f102011-01-22 17:51:53 +00001879 LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001880
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001881 // C++ 11p3: Members of a class defined with the keyword class are private
1882 // by default. Members of a class defined with the keywords struct or union
1883 // are public by default.
1884 AccessSpecifier CurAS;
1885 if (TagType == DeclSpec::TST_class)
1886 CurAS = AS_private;
1887 else
1888 CurAS = AS_public;
1889
Douglas Gregor07976d22010-06-21 22:31:09 +00001890 SourceLocation RBraceLoc;
1891 if (TagDecl) {
1892 // While we still have something to read, read the member-declarations.
1893 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1894 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Douglas Gregor07976d22010-06-21 22:31:09 +00001896 // Check for extraneous top-level semicolon.
1897 if (Tok.is(tok::semi)) {
1898 Diag(Tok, diag::ext_extra_struct_semi)
1899 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1900 << FixItHint::CreateRemoval(Tok.getLocation());
1901 ConsumeToken();
1902 continue;
1903 }
1904
1905 AccessSpecifier AS = getAccessSpecifierIfPresent();
1906 if (AS != AS_none) {
1907 // Current token is a C++ access specifier.
1908 CurAS = AS;
1909 SourceLocation ASLoc = Tok.getLocation();
1910 ConsumeToken();
1911 if (Tok.is(tok::colon))
1912 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1913 else
1914 Diag(Tok, diag::err_expected_colon);
1915 ConsumeToken();
1916 continue;
1917 }
1918
1919 // FIXME: Make sure we don't have a template here.
1920
1921 // Parse all the comma separated declarators.
1922 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001923 }
1924
Douglas Gregor07976d22010-06-21 22:31:09 +00001925 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1926 } else {
1927 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001928 }
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001930 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00001931 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001932 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001933
John McCall42a4f662010-05-28 08:11:17 +00001934 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001935 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001936 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00001937 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001938
1939 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1940 // complete within function bodies, default arguments,
1941 // exception-specifications, and constructor ctor-initializers (including
1942 // such things in nested classes).
1943 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001944 // FIXME: Only function bodies and constructor ctor-initializers are
1945 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001946 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001947 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001948 // are complete and we can parse the delayed portions of method
1949 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001950 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001951 ParseLexedMethodDeclarations(getCurrentClass());
1952 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001953 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001954 }
1955
John McCall42a4f662010-05-28 08:11:17 +00001956 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001957 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001958
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001959 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001960 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001961 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001962}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001963
1964/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1965/// which explicitly initializes the members or base classes of a
1966/// class (C++ [class.base.init]). For example, the three initializers
1967/// after the ':' in the Derived constructor below:
1968///
1969/// @code
1970/// class Base { };
1971/// class Derived : Base {
1972/// int x;
1973/// float f;
1974/// public:
1975/// Derived(float f) : Base(), x(17), f(f) { }
1976/// };
1977/// @endcode
1978///
Mike Stump1eb44332009-09-09 15:08:12 +00001979/// [C++] ctor-initializer:
1980/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001981///
Mike Stump1eb44332009-09-09 15:08:12 +00001982/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001983/// mem-initializer ...[opt]
1984/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00001985void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001986 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1987
John Wiegley28bbe4b2011-04-28 01:08:34 +00001988 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
1989 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001990 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Sean Huntcbb67482011-01-08 20:30:50 +00001992 llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001993 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001994
Douglas Gregor7ad83902008-11-05 04:29:56 +00001995 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00001996 if (Tok.is(tok::code_completion)) {
1997 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1998 MemInitializers.data(),
1999 MemInitializers.size());
2000 ConsumeCodeCompletionToken();
2001 } else {
2002 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2003 if (!MemInit.isInvalid())
2004 MemInitializers.push_back(MemInit.get());
2005 else
2006 AnyErrors = true;
2007 }
2008
Douglas Gregor7ad83902008-11-05 04:29:56 +00002009 if (Tok.is(tok::comma))
2010 ConsumeToken();
2011 else if (Tok.is(tok::l_brace))
2012 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002013 // If the next token looks like a base or member initializer, assume that
2014 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002015 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2016 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2017 Diag(Loc, diag::err_ctor_init_missing_comma)
2018 << FixItHint::CreateInsertion(Loc, ", ");
2019 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002020 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002021 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002022 SkipUntil(tok::l_brace, true, true);
2023 break;
2024 }
2025 } while (true);
2026
Mike Stump1eb44332009-09-09 15:08:12 +00002027 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002028 MemInitializers.data(), MemInitializers.size(),
2029 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002030}
2031
2032/// ParseMemInitializer - Parse a C++ member initializer, which is
2033/// part of a constructor initializer that explicitly initializes one
2034/// member or base class (C++ [class.base.init]). See
2035/// ParseConstructorInitializer for an example.
2036///
2037/// [C++] mem-initializer:
2038/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002039///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002040/// [C++] mem-initializer-id:
2041/// '::'[opt] nested-name-specifier[opt] class-name
2042/// identifier
John McCalld226f652010-08-21 09:40:31 +00002043Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002044 // parse '::'[opt] nested-name-specifier[opt]
2045 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002046 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
2047 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002048 if (Tok.is(tok::annot_template_id)) {
2049 TemplateIdAnnotation *TemplateId
2050 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00002051 if (TemplateId->Kind == TNK_Type_template ||
2052 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002053 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002054 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002055 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002056 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002057 }
2058 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002059 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002060 return true;
2061 }
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Douglas Gregor7ad83902008-11-05 04:29:56 +00002063 // Get the identifier. This may be a member name or a class name,
2064 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00002065 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002066 SourceLocation IdLoc = ConsumeToken();
2067
2068 // Parse the '('.
2069 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002070 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002071 return true;
2072 }
2073 SourceLocation LParenLoc = ConsumeParen();
2074
2075 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00002076 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002077 CommaLocsTy CommaLocs;
2078 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2079 SkipUntil(tok::r_paren);
2080 return true;
2081 }
2082
2083 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2084
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002085 SourceLocation EllipsisLoc;
2086 if (Tok.is(tok::ellipsis))
2087 EllipsisLoc = ConsumeToken();
2088
Douglas Gregor23c94db2010-07-02 17:43:08 +00002089 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00002090 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00002091 LParenLoc, ArgExprs.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002092 ArgExprs.size(), RParenLoc,
2093 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002094}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002095
Sebastian Redl7acafd02011-03-05 14:45:16 +00002096/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002097///
Douglas Gregora4745612008-12-01 18:00:20 +00002098/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002099/// dynamic-exception-specification
2100/// noexcept-specification
2101///
2102/// noexcept-specification:
2103/// 'noexcept'
2104/// 'noexcept' '(' constant-expression ')'
2105ExceptionSpecificationType
2106Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange,
2107 llvm::SmallVectorImpl<ParsedType> &DynamicExceptions,
2108 llvm::SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
2109 ExprResult &NoexceptExpr) {
2110 ExceptionSpecificationType Result = EST_None;
2111
2112 // See if there's a dynamic specification.
2113 if (Tok.is(tok::kw_throw)) {
2114 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2115 DynamicExceptions,
2116 DynamicExceptionRanges);
2117 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2118 "Produced different number of exception types and ranges.");
2119 }
2120
2121 // If there's no noexcept specification, we're done.
2122 if (Tok.isNot(tok::kw_noexcept))
2123 return Result;
2124
2125 // If we already had a dynamic specification, parse the noexcept for,
2126 // recovery, but emit a diagnostic and don't store the results.
2127 SourceRange NoexceptRange;
2128 ExceptionSpecificationType NoexceptType = EST_None;
2129
2130 SourceLocation KeywordLoc = ConsumeToken();
2131 if (Tok.is(tok::l_paren)) {
2132 // There is an argument.
2133 SourceLocation LParenLoc = ConsumeParen();
2134 NoexceptType = EST_ComputedNoexcept;
2135 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002136 // The argument must be contextually convertible to bool. We use
2137 // ActOnBooleanCondition for this purpose.
2138 if (!NoexceptExpr.isInvalid())
2139 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2140 NoexceptExpr.get());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002141 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2142 NoexceptRange = SourceRange(KeywordLoc, RParenLoc);
2143 } else {
2144 // There is no argument.
2145 NoexceptType = EST_BasicNoexcept;
2146 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2147 }
2148
2149 if (Result == EST_None) {
2150 SpecificationRange = NoexceptRange;
2151 Result = NoexceptType;
2152
2153 // If there's a dynamic specification after a noexcept specification,
2154 // parse that and ignore the results.
2155 if (Tok.is(tok::kw_throw)) {
2156 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2157 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2158 DynamicExceptionRanges);
2159 }
2160 } else {
2161 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2162 }
2163
2164 return Result;
2165}
2166
2167/// ParseDynamicExceptionSpecification - Parse a C++
2168/// dynamic-exception-specification (C++ [except.spec]).
2169///
2170/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002171/// 'throw' '(' type-id-list [opt] ')'
2172/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002173///
Douglas Gregora4745612008-12-01 18:00:20 +00002174/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002175/// type-id ... [opt]
2176/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002177///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002178ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2179 SourceRange &SpecificationRange,
2180 llvm::SmallVectorImpl<ParsedType> &Exceptions,
2181 llvm::SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002182 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002183
Sebastian Redl7acafd02011-03-05 14:45:16 +00002184 SpecificationRange.setBegin(ConsumeToken());
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002186 if (!Tok.is(tok::l_paren)) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002187 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2188 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002189 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002190 }
2191 SourceLocation LParenLoc = ConsumeParen();
2192
Douglas Gregora4745612008-12-01 18:00:20 +00002193 // Parse throw(...), a Microsoft extension that means "this function
2194 // can throw anything".
2195 if (Tok.is(tok::ellipsis)) {
2196 SourceLocation EllipsisLoc = ConsumeToken();
2197 if (!getLang().Microsoft)
2198 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl7acafd02011-03-05 14:45:16 +00002199 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2200 SpecificationRange.setEnd(RParenLoc);
Sebastian Redl60618fa2011-03-12 11:50:43 +00002201 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002202 }
2203
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002204 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002205 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002206 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002207 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002208
Douglas Gregora04426c2010-12-20 23:57:46 +00002209 if (Tok.is(tok::ellipsis)) {
2210 // C++0x [temp.variadic]p5:
2211 // - In a dynamic-exception-specification (15.4); the pattern is a
2212 // type-id.
2213 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002214 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002215 if (!Res.isInvalid())
2216 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2217 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002218
Sebastian Redlef65f062009-05-29 18:02:33 +00002219 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002220 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002221 Ranges.push_back(Range);
2222 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002223
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002224 if (Tok.is(tok::comma))
2225 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002226 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002227 break;
2228 }
2229
Sebastian Redl7acafd02011-03-05 14:45:16 +00002230 SpecificationRange.setEnd(MatchRHSPunctuation(tok::r_paren, LParenLoc));
Sebastian Redl60618fa2011-03-12 11:50:43 +00002231 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002232}
Douglas Gregor6569d682009-05-27 23:11:45 +00002233
Douglas Gregordab60ad2010-10-01 18:44:50 +00002234/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2235/// function declaration.
2236TypeResult Parser::ParseTrailingReturnType() {
2237 assert(Tok.is(tok::arrow) && "expected arrow");
2238
2239 ConsumeToken();
2240
2241 // FIXME: Need to suppress declarations when parsing this typename.
2242 // Otherwise in this function definition:
2243 //
2244 // auto f() -> struct X {}
2245 //
2246 // struct X is parsed as class definition because of the trailing
2247 // brace.
2248
2249 SourceRange Range;
2250 return ParseTypeName(&Range);
2251}
2252
Douglas Gregor6569d682009-05-27 23:11:45 +00002253/// \brief We have just started parsing the definition of a new class,
2254/// so push that class onto our stack of classes that is currently
2255/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002256Sema::ParsingClassState
2257Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002258 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002259 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00002260 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCalleee1d542011-02-14 07:13:47 +00002261 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002262}
2263
2264/// \brief Deallocate the given parsed class and all of its nested
2265/// classes.
2266void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002267 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2268 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002269 delete Class;
2270}
2271
2272/// \brief Pop the top class of the stack of classes that are
2273/// currently being parsed.
2274///
2275/// This routine should be called when we have finished parsing the
2276/// definition of a class, but have not yet popped the Scope
2277/// associated with the class's definition.
2278///
2279/// \returns true if the class we've popped is a top-level class,
2280/// false otherwise.
John McCalleee1d542011-02-14 07:13:47 +00002281void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002282 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002283
John McCalleee1d542011-02-14 07:13:47 +00002284 Actions.PopParsingClass(state);
2285
Douglas Gregor6569d682009-05-27 23:11:45 +00002286 ParsingClass *Victim = ClassStack.top();
2287 ClassStack.pop();
2288 if (Victim->TopLevelClass) {
2289 // Deallocate all of the nested classes of this class,
2290 // recursively: we don't need to keep any of this information.
2291 DeallocateParsedClasses(Victim);
2292 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002293 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002294 assert(!ClassStack.empty() && "Missing top-level class?");
2295
Douglas Gregord54eb442010-10-12 16:25:54 +00002296 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002297 // The victim is a nested class, but we will not need to perform
2298 // any processing after the definition of this class since it has
2299 // no members whose handling was delayed. Therefore, we can just
2300 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002301 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002302 return;
2303 }
2304
2305 // This nested class has some members that will need to be processed
2306 // after the top-level class is completely defined. Therefore, add
2307 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002308 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002309 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002310 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002311}
Sean Huntbbd37c62009-11-21 08:43:09 +00002312
2313/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
2314/// parses standard attributes.
2315///
2316/// [C++0x] attribute-specifier:
2317/// '[' '[' attribute-list ']' ']'
2318///
2319/// [C++0x] attribute-list:
2320/// attribute[opt]
2321/// attribute-list ',' attribute[opt]
2322///
2323/// [C++0x] attribute:
2324/// attribute-token attribute-argument-clause[opt]
2325///
2326/// [C++0x] attribute-token:
2327/// identifier
2328/// attribute-scoped-token
2329///
2330/// [C++0x] attribute-scoped-token:
2331/// attribute-namespace '::' identifier
2332///
2333/// [C++0x] attribute-namespace:
2334/// identifier
2335///
2336/// [C++0x] attribute-argument-clause:
2337/// '(' balanced-token-seq ')'
2338///
2339/// [C++0x] balanced-token-seq:
2340/// balanced-token
2341/// balanced-token-seq balanced-token
2342///
2343/// [C++0x] balanced-token:
2344/// '(' balanced-token-seq ')'
2345/// '[' balanced-token-seq ']'
2346/// '{' balanced-token-seq '}'
2347/// any token but '(', ')', '[', ']', '{', or '}'
John McCall7f040a92010-12-24 02:08:15 +00002348void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2349 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002350 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2351 && "Not a C++0x attribute list");
2352
2353 SourceLocation StartLoc = Tok.getLocation(), Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002354
2355 ConsumeBracket();
2356 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002357
Sean Huntbbd37c62009-11-21 08:43:09 +00002358 if (Tok.is(tok::comma)) {
2359 Diag(Tok.getLocation(), diag::err_expected_ident);
2360 ConsumeToken();
2361 }
2362
2363 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2364 // attribute not present
2365 if (Tok.is(tok::comma)) {
2366 ConsumeToken();
2367 continue;
2368 }
2369
2370 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2371 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002372
Sean Huntbbd37c62009-11-21 08:43:09 +00002373 // scoped attribute
2374 if (Tok.is(tok::coloncolon)) {
2375 ConsumeToken();
2376
2377 if (!Tok.is(tok::identifier)) {
2378 Diag(Tok.getLocation(), diag::err_expected_ident);
2379 SkipUntil(tok::r_square, tok::comma, true, true);
2380 continue;
2381 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002382
Sean Huntbbd37c62009-11-21 08:43:09 +00002383 ScopeName = AttrName;
2384 ScopeLoc = AttrLoc;
2385
2386 AttrName = Tok.getIdentifierInfo();
2387 AttrLoc = ConsumeToken();
2388 }
2389
2390 bool AttrParsed = false;
2391 // No scoped names are supported; ideally we could put all non-standard
2392 // attributes into namespaces.
2393 if (!ScopeName) {
2394 switch(AttributeList::getKind(AttrName))
2395 {
2396 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002397 case AttributeList::AT_carries_dependency:
Anders Carlsson15e14a22011-01-23 21:33:18 +00002398 case AttributeList::AT_noreturn: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002399 if (Tok.is(tok::l_paren)) {
2400 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2401 << AttrName->getName();
2402 break;
2403 }
2404
John McCall0b7e6782011-03-24 11:26:52 +00002405 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0,
2406 SourceLocation(), 0, 0, false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002407 AttrParsed = true;
2408 break;
2409 }
2410
2411 // One argument; must be a type-id or assignment-expression
2412 case AttributeList::AT_aligned: {
2413 if (Tok.isNot(tok::l_paren)) {
2414 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2415 << AttrName->getName();
2416 break;
2417 }
2418 SourceLocation ParamLoc = ConsumeParen();
2419
John McCall60d7b3a2010-08-24 06:29:42 +00002420 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002421
2422 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2423
2424 ExprVector ArgExprs(Actions);
2425 ArgExprs.push_back(ArgExpr.release());
John McCall0b7e6782011-03-24 11:26:52 +00002426 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc,
2427 0, ParamLoc, ArgExprs.take(), 1,
2428 false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002429
2430 AttrParsed = true;
2431 break;
2432 }
2433
2434 // Silence warnings
2435 default: break;
2436 }
2437 }
2438
2439 // Skip the entire parameter clause, if any
2440 if (!AttrParsed && Tok.is(tok::l_paren)) {
2441 ConsumeParen();
2442 // SkipUntil maintains the balancedness of tokens.
2443 SkipUntil(tok::r_paren, false);
2444 }
2445 }
2446
2447 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2448 SkipUntil(tok::r_square, false);
2449 Loc = Tok.getLocation();
2450 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2451 SkipUntil(tok::r_square, false);
2452
John McCall7f040a92010-12-24 02:08:15 +00002453 attrs.Range = SourceRange(StartLoc, Loc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002454}
2455
2456/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2457/// attribute.
2458///
2459/// FIXME: Simply returns an alignof() expression if the argument is a
2460/// type. Ideally, the type should be propagated directly into Sema.
2461///
2462/// [C++0x] 'align' '(' type-id ')'
2463/// [C++0x] 'align' '(' assignment-expression ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002464ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002465 if (isTypeIdInParens()) {
John McCallf312b1e2010-08-26 23:41:50 +00002466 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sean Huntbbd37c62009-11-21 08:43:09 +00002467 SourceLocation TypeLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002468 ParsedType Ty = ParseTypeName().get();
Sean Huntbbd37c62009-11-21 08:43:09 +00002469 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002470 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2471 Ty.getAsOpaquePtr(), TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002472 } else
2473 return ParseConstantExpression();
2474}
Francois Pichet334d47e2010-10-11 12:59:39 +00002475
2476/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2477///
2478/// [MS] ms-attribute:
2479/// '[' token-seq ']'
2480///
2481/// [MS] ms-attribute-seq:
2482/// ms-attribute[opt]
2483/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00002484void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2485 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00002486 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2487
2488 while (Tok.is(tok::l_square)) {
2489 ConsumeBracket();
2490 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00002491 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00002492 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2493 }
2494}