blob: d3b34b922d879ffb63d6e91f47af3fbf10bb99e2 [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson74d7f0d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor423984d2008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattnera5235172007-08-25 06:57:03 +000022using namespace clang;
23
24/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-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 Lattnera5235172007-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 Redl67667942010-08-27 23:12:46 +000033/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000034///
35/// named-namespace-definition:
36/// original-namespace-definition
37/// extension-namespace-definition
38///
39/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' identifier attributes[opt]
41/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000042///
43/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' original-namespace-name
45/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000046///
Chris Lattnera5235172007-08-25 06:57:03 +000047/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
48/// 'namespace' identifier '=' qualified-namespace-specifier ';'
49///
John McCall48871652010-08-21 09:40:31 +000050Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000051 SourceLocation &DeclEnd,
52 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000053 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000054 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump11289f42009-09-09 15:08:12 +000055
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000056 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000057 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +000058 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000059 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000060
Chris Lattnera5235172007-08-25 06:57:03 +000061 SourceLocation IdentLoc;
62 IdentifierInfo *Ident = 0;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000063
64 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000065
Chris Lattner76c72282007-10-09 17:33:22 +000066 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000067 Ident = Tok.getIdentifierInfo();
68 IdentLoc = ConsumeToken(); // eat the identifier.
69 }
Mike Stump11289f42009-09-09 15:08:12 +000070
Chris Lattnera5235172007-08-25 06:57:03 +000071 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000072 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000075 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000076 }
Mike Stump11289f42009-09-09 15:08:12 +000077
Douglas Gregor6b6bba42009-06-17 19:49:00 +000078 if (Tok.is(tok::equal)) {
John McCall53fa7142010-12-24 02:08:15 +000079 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +000080 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +000081 if (InlineLoc.isValid())
82 Diag(InlineLoc, diag::err_inline_namespace_alias)
83 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000084
Chris Lattner49836b42009-04-02 04:16:50 +000085 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000086 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner4de55aa2009-03-29 14:02:43 +000088 if (Tok.isNot(tok::l_brace)) {
Mike Stump11289f42009-09-09 15:08:12 +000089 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner4de55aa2009-03-29 14:02:43 +000090 diag::err_expected_ident_lbrace);
John McCall48871652010-08-21 09:40:31 +000091 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +000092 }
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattner4de55aa2009-03-29 14:02:43 +000094 SourceLocation LBrace = ConsumeBrace();
95
Douglas Gregor0be31a22010-07-02 17:43:08 +000096 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
97 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
98 getCurScope()->getFnParent()) {
Douglas Gregor05cfc292010-05-14 05:08:22 +000099 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
100 SkipUntil(tok::r_brace, false);
John McCall48871652010-08-21 09:40:31 +0000101 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000102 }
103
Sebastian Redl5a5f2c72010-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 Lattner4de55aa2009-03-29 14:02:43 +0000108 // Enter a scope for the namespace.
109 ParseScope NamespaceScope(this, Scope::DeclScope);
110
John McCall48871652010-08-21 09:40:31 +0000111 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000112 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
113 IdentLoc, Ident, LBrace, attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000114
John McCallfaf5fb42010-08-26 23:41:50 +0000115 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
116 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000117
Alexis Hunt96d5c762009-11-21 08:43:09 +0000118 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall084e83d2011-03-24 11:26:52 +0000119 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000120 MaybeParseCXX0XAttributes(attrs);
121 MaybeParseMicrosoftAttributes(attrs);
122 ParseExternalDeclaration(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000123 }
Mike Stump11289f42009-09-09 15:08:12 +0000124
Chris Lattner4de55aa2009-03-29 14:02:43 +0000125 // Leave the namespace scope.
126 NamespaceScope.Exit();
127
Chris Lattner49836b42009-04-02 04:16:50 +0000128 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
129 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000130
Chris Lattner49836b42009-04-02 04:16:50 +0000131 DeclEnd = RBraceLoc;
Chris Lattner4de55aa2009-03-29 14:02:43 +0000132 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000133}
Chris Lattner38376f12008-01-12 07:05:38 +0000134
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000135/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
136/// alias definition.
137///
John McCall48871652010-08-21 09:40:31 +0000138Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000139 SourceLocation AliasLoc,
140 IdentifierInfo *Alias,
141 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000142 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000143
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000144 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000145
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000146 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000147 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000148 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000149 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000150
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000151 CXXScopeSpec SS;
152 // Parse (optional) nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000153 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlsson1894f0d42009-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 McCall48871652010-08-21 09:40:31 +0000159 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000160 }
161
162 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000163 IdentifierInfo *Ident = Tok.getIdentifierInfo();
164 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000165
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000166 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000167 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000168 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
169 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000170
Douglas Gregor0be31a22010-07-02 17:43:08 +0000171 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000172 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000173}
174
Chris Lattner38376f12008-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 Lattner8ea64422010-11-09 20:15:55 +0000182Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000183 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000184 llvm::SmallString<8> LangBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000185 bool Invalid = false;
186 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
187 if (Invalid)
John McCall48871652010-08-21 09:40:31 +0000188 return 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000189
190 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000191
Douglas Gregor07665a62009-01-05 19:45:36 +0000192 ParseScope LinkageScope(this, Scope::DeclScope);
John McCall48871652010-08-21 09:40:31 +0000193 Decl *LinkageSpec
Douglas Gregor0be31a22010-07-02 17:43:08 +0000194 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000195 DS.getSourceRange().getBegin(),
Benjamin Kramerbebee842010-05-03 13:08:54 +0000196 Loc, Lang,
Abramo Bagnaraea947882011-03-08 16:41:52 +0000197 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000198 : SourceLocation());
199
John McCall084e83d2011-03-24 11:26:52 +0000200 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000201 MaybeParseCXX0XAttributes(attrs);
202 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000203
Douglas Gregor07665a62009-01-05 19:45:36 +0000204 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-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 Bagnaraed5b6892010-07-30 16:47:02 +0000210 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000211 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000212 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000213 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000214 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000215
Douglas Gregorb65a9132010-02-07 08:38:28 +0000216 DS.abort();
217
John McCall53fa7142010-12-24 02:08:15 +0000218 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000219
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000220 SourceLocation LBrace = ConsumeBrace();
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000221 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall084e83d2011-03-24 11:26:52 +0000222 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000223 MaybeParseCXX0XAttributes(attrs);
224 MaybeParseMicrosoftAttributes(attrs);
225 ParseExternalDeclaration(attrs);
Chris Lattner38376f12008-01-12 07:05:38 +0000226 }
227
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000228 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Chris Lattner8ea64422010-11-09 20:15:55 +0000229 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
230 RBrace);
Chris Lattner38376f12008-01-12 07:05:38 +0000231}
Douglas Gregor556877c2008-04-13 21:30:24 +0000232
Douglas Gregord7c4d982008-12-30 03:27:21 +0000233/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
234/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000235Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000236 const ParsedTemplateInfo &TemplateInfo,
237 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000238 ParsedAttributesWithRange &attrs) {
Douglas Gregord7c4d982008-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 Gregor7e90c6d2009-09-18 19:03:04 +0000244 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000245 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000246 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000247 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000248
John McCall9b72f892010-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 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000257
John McCall53fa7142010-12-24 02:08:15 +0000258 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000259 }
260
Richard Smithdda56e42011-04-15 14:24:37 +0000261 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000262
263 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000264 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000265
John McCall9b72f892010-11-10 02:40:36 +0000266 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd);
Douglas Gregord7c4d982008-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 McCall48871652010-08-21 09:40:31 +0000279Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000280 SourceLocation UsingLoc,
281 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000282 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-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 Gregor7e90c6d2009-09-18 19:03:04 +0000288 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000289 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000290 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000291 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000292
Douglas Gregord7c4d982008-12-30 03:27:21 +0000293 CXXScopeSpec SS;
294 // Parse (optional) nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000295 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000296
Douglas Gregord7c4d982008-12-30 03:27:21 +0000297 IdentifierInfo *NamespcName = 0;
298 SourceLocation IdentLoc = SourceLocation();
299
300 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000301 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-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 McCall48871652010-08-21 09:40:31 +0000306 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000307 }
Mike Stump11289f42009-09-09 15:08:12 +0000308
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000309 // Parse identifier.
310 NamespcName = Tok.getIdentifierInfo();
311 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000312
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000313 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000314 bool GNUAttr = false;
315 if (Tok.is(tok::kw___attribute)) {
316 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000317 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000318 }
Mike Stump11289f42009-09-09 15:08:12 +0000319
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000320 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000321 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000322 ExpectAndConsume(tok::semi,
Douglas Gregor45d6bdf2010-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 Gregord7c4d982008-12-30 03:27:21 +0000326
Douglas Gregor0be31a22010-07-02 17:43:08 +0000327 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000328 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000329}
330
Richard Smithdda56e42011-04-15 14:24:37 +0000331/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
332/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000333///
334/// using-declaration: [C++ 7.3.p3: namespace.udecl]
335/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000336/// unqualified-id
337/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000338///
Richard Smithdda56e42011-04-15 14:24:37 +0000339/// alias-declaration: C++0x [decl.typedef]p2
340/// 'using' identifier = type-id ;
341///
John McCall48871652010-08-21 09:40:31 +0000342Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000343 const ParsedTemplateInfo &TemplateInfo,
344 SourceLocation UsingLoc,
345 SourceLocation &DeclEnd,
346 AccessSpecifier AS) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000347 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000348 SourceLocation TypenameLoc;
Douglas Gregorfec52632009-06-20 00:51:54 +0000349 bool IsTypeName;
350
351 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000352 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000353 if (Tok.is(tok::kw_typename)) {
John McCalle61f2ba2009-11-18 02:36:19 +0000354 TypenameLoc = Tok.getLocation();
Douglas Gregorfec52632009-06-20 00:51:54 +0000355 ConsumeToken();
356 IsTypeName = true;
357 }
358 else
359 IsTypeName = false;
360
361 // Parse nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000362 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorfec52632009-06-20 00:51:54 +0000363
Douglas Gregorfec52632009-06-20 00:51:54 +0000364 // Check nested-name specifier.
365 if (SS.isInvalid()) {
366 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000367 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000368 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000369
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000370 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000371 // destructor names and allow the action module to diagnose any semantic
372 // errors.
373 UnqualifiedId Name;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000374 if (ParseUnqualifiedId(SS,
Douglas Gregor220f4272009-11-04 16:30:06 +0000375 /*EnteringContext=*/false,
376 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000377 /*AllowConstructorName=*/true,
John McCallba7bf592010-08-24 05:47:05 +0000378 ParsedType(),
Douglas Gregor220f4272009-11-04 16:30:06 +0000379 Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000380 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000381 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000382 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000383
John McCall084e83d2011-03-24 11:26:52 +0000384 ParsedAttributes attrs(AttrFactory);
Richard Smithdda56e42011-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 Smith3f1b5d02011-05-05 21:57:07 +0000390 // TODO: Attribute support. C++0x attributes may appear before the equals.
391 // Where can GNU attributes appear?
Richard Smithdda56e42011-04-15 14:24:37 +0000392 ConsumeToken();
393
394 if (!getLang().CPlusPlus0x)
395 Diag(Tok.getLocation(), diag::ext_alias_declaration);
396
Richard Smith3f1b5d02011-05-05 21:57:07 +0000397 // Type alias templates cannot be specialized.
398 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000399 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
400 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-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 Smithdda56e42011-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 Smith3f1b5d02011-05-05 21:57:07 +0000433 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
434 Declarator::AliasTemplateContext :
435 Declarator::AliasDeclContext);
Richard Smithdda56e42011-04-15 14:24:37 +0000436 } else
437 // Parse (optional) attributes (most likely GNU strong-using extension).
438 MaybeParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000439
Douglas Gregorfec52632009-06-20 00:51:54 +0000440 // Eat ';'.
441 DeclEnd = Tok.getLocation();
442 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smithdda56e42011-04-15 14:24:37 +0000443 !attrs.empty() ? "attributes list" :
444 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor220f4272009-11-04 16:30:06 +0000445 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000446
John McCall9b72f892010-11-10 02:40:36 +0000447 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith3f1b5d02011-05-05 21:57:07 +0000448 // In C++0x, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000449 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000450 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-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 Smith3f1b5d02011-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 Smithdda56e42011-04-15 14:24:37 +0000469
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000470 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000471 Name, attrs.getList(),
472 IsTypeName, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000473}
474
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000475/// ParseStaticAssertDeclaration - Parse C++0x or C1X static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000476///
Peter Collingbourne3d9cbdc2011-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 Carlssonf24fcff62009-03-11 16:27:10 +0000482///
John McCall48871652010-08-21 09:40:31 +0000483Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-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 Carlssonf24fcff62009-03-11 16:27:10 +0000490 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000491
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000492 if (Tok.isNot(tok::l_paren)) {
493 Diag(Tok, diag::err_expected_lparen);
John McCall48871652010-08-21 09:40:31 +0000494 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000495 }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000497 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000498
John McCalldadc5752010-08-24 06:29:42 +0000499 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000500 if (AssertExpr.isInvalid()) {
501 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000502 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000505 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCall48871652010-08-21 09:40:31 +0000506 return 0;
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000507
Anders Carlssonf24fcff62009-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 McCall48871652010-08-21 09:40:31 +0000511 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
John McCalldadc5752010-08-24 06:29:42 +0000514 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000515 if (AssertMessage.isInvalid())
John McCall48871652010-08-21 09:40:31 +0000516 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000517
Abramo Bagnaraea947882011-03-08 16:41:52 +0000518 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000519
Chris Lattner49836b42009-04-02 04:16:50 +0000520 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000521 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000522
John McCallb268a282010-08-23 23:25:46 +0000523 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
524 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000525 AssertMessage.take(),
526 RParenLoc);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000527}
528
Anders Carlsson74948d02009-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 Stump11289f42009-09-09 15:08:12 +0000538
539 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson74948d02009-06-24 17:47:40 +0000540 "decltype")) {
541 SkipUntil(tok::r_paren);
542 return;
543 }
Mike Stump11289f42009-09-09 15:08:12 +0000544
Anders Carlsson74948d02009-06-24 17:47:40 +0000545 // Parse the expression
Mike Stump11289f42009-09-09 15:08:12 +0000546
Anders Carlsson74948d02009-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 McCallfaf5fb42010-08-26 23:41:50 +0000550 Sema::Unevaluated);
John McCalldadc5752010-08-24 06:29:42 +0000551 ExprResult Result = ParseExpression();
Anders Carlsson74948d02009-06-24 17:47:40 +0000552 if (Result.isInvalid()) {
553 SkipUntil(tok::r_paren);
554 return;
555 }
Mike Stump11289f42009-09-09 15:08:12 +0000556
Anders Carlsson74948d02009-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 Stump11289f42009-09-09 15:08:12 +0000563
Anders Carlsson74948d02009-06-24 17:47:40 +0000564 if (RParenLoc.isInvalid())
565 return;
566
567 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000568 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000569 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump11289f42009-09-09 15:08:12 +0000570 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000571 DiagID, Result.release()))
572 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson74948d02009-06-24 17:47:40 +0000573}
574
Alexis Hunt4a257072011-05-19 05:37:45 +0000575void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
576 assert(Tok.is(tok::kw___underlying_type) &&
577 "Not an underlying type specifier");
578
579 SourceLocation StartLoc = ConsumeToken();
580 SourceLocation LParenLoc = Tok.getLocation();
581
582 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
583 "__underlying_type")) {
584 SkipUntil(tok::r_paren);
585 return;
586 }
587
588 TypeResult Result = ParseTypeName();
589 if (Result.isInvalid()) {
590 SkipUntil(tok::r_paren);
591 return;
592 }
593
594 // Match the ')'
595 SourceLocation RParenLoc;
596 if (Tok.is(tok::r_paren))
597 RParenLoc = ConsumeParen();
598 else
599 MatchRHSPunctuation(tok::r_paren, LParenLoc);
600
601 if (RParenLoc.isInvalid())
602 return;
603
604 const char *PrevSpec = 0;
605 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000606 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Alexis Hunt4a257072011-05-19 05:37:45 +0000607 DiagID, Result.release()))
608 Diag(StartLoc, DiagID) << PrevSpec;
609}
610
Douglas Gregor831c93f2008-11-05 20:51:48 +0000611/// ParseClassName - Parse a C++ class-name, which names a class. Note
612/// that we only check that the result names a type; semantic analysis
613/// will need to verify that the type names a class. The result is
Douglas Gregord54dfb82009-02-25 23:52:28 +0000614/// either a type or NULL, depending on whether a type name was
Douglas Gregor831c93f2008-11-05 20:51:48 +0000615/// found.
616///
617/// class-name: [C++ 9.1]
618/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000619/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000620///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000621Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +0000622 CXXScopeSpec &SS) {
Douglas Gregord54dfb82009-02-25 23:52:28 +0000623 // Check whether we have a template-id that names a type.
624 if (Tok.is(tok::annot_template_id)) {
Mike Stump11289f42009-09-09 15:08:12 +0000625 TemplateIdAnnotation *TemplateId
Douglas Gregord54dfb82009-02-25 23:52:28 +0000626 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +0000627 if (TemplateId->Kind == TNK_Type_template ||
628 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000629 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000630
631 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000632 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000633 EndLocation = Tok.getAnnotationEndLoc();
634 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000635
636 if (Type)
637 return Type;
638 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000639 }
640
641 // Fall through to produce an error below.
642 }
643
Douglas Gregor831c93f2008-11-05 20:51:48 +0000644 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000645 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000646 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000647 }
648
Douglas Gregor18473f32010-01-12 21:28:44 +0000649 IdentifierInfo *Id = Tok.getIdentifierInfo();
650 SourceLocation IdLoc = ConsumeToken();
651
652 if (Tok.is(tok::less)) {
653 // It looks the user intended to write a template-id here, but the
654 // template-name was wrong. Try to fix that.
655 TemplateNameKind TNK = TNK_Type_template;
656 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000657 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000658 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000659 Diag(IdLoc, diag::err_unknown_template_name)
660 << Id;
661 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000662
Douglas Gregor18473f32010-01-12 21:28:44 +0000663 if (!Template)
664 return true;
665
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000666 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000667 UnqualifiedId TemplateName;
668 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000669
Douglas Gregor18473f32010-01-12 21:28:44 +0000670 // Parse the full template-id, then turn it into a type.
671 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
672 SourceLocation(), true))
673 return true;
674 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000675 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000676
Douglas Gregor18473f32010-01-12 21:28:44 +0000677 // If we didn't end up with a typename token, there's nothing more we
678 // can do.
679 if (Tok.isNot(tok::annot_typename))
680 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000681
Douglas Gregor18473f32010-01-12 21:28:44 +0000682 // Retrieve the type from the annotation token, consume that token, and
683 // return.
684 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000685 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000686 ConsumeToken();
687 return Type;
688 }
689
Douglas Gregor831c93f2008-11-05 20:51:48 +0000690 // We have an identifier; check whether it is actually a type.
Douglas Gregore7c20652011-03-02 00:47:37 +0000691 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000692 false, ParsedType(),
693 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000694 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000695 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000696 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000697 }
698
699 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000700 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000701
702 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000703 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +0000704 DS.SetRangeStart(IdLoc);
705 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000706 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000707
708 const char *PrevSpec = 0;
709 unsigned DiagID;
710 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
711
712 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
713 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000714}
715
Douglas Gregor556877c2008-04-13 21:30:24 +0000716/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
717/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
718/// until we reach the start of a definition or see a token that
Sebastian Redl2b372722010-02-03 21:21:43 +0000719/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregor556877c2008-04-13 21:30:24 +0000720///
721/// class-specifier: [C++ class]
722/// class-head '{' member-specification[opt] '}'
723/// class-head '{' member-specification[opt] '}' attributes[opt]
724/// class-head:
725/// class-key identifier[opt] base-clause[opt]
726/// class-key nested-name-specifier identifier base-clause[opt]
727/// class-key nested-name-specifier[opt] simple-template-id
728/// base-clause[opt]
729/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000730/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +0000731/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000732/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +0000733/// simple-template-id base-clause[opt]
734/// class-key:
735/// 'class'
736/// 'struct'
737/// 'union'
738///
739/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +0000740/// class-key ::[opt] nested-name-specifier[opt] identifier
741/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
742/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +0000743///
744/// Note that the C++ class-specifier and elaborated-type-specifier,
745/// together, subsume the C99 struct-or-union-specifier:
746///
747/// struct-or-union-specifier: [C99 6.7.2.1]
748/// struct-or-union identifier[opt] '{' struct-contents '}'
749/// struct-or-union identifier
750/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
751/// '}' attributes[opt]
752/// [GNU] struct-or-union attributes[opt] identifier
753/// struct-or-union:
754/// 'struct'
755/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000756void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
757 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000758 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redl2b372722010-02-03 21:21:43 +0000759 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000760 DeclSpec::TST TagType;
761 if (TagTokKind == tok::kw_struct)
762 TagType = DeclSpec::TST_struct;
763 else if (TagTokKind == tok::kw_class)
764 TagType = DeclSpec::TST_class;
765 else {
766 assert(TagTokKind == tok::kw_union && "Not a class specifier");
767 TagType = DeclSpec::TST_union;
768 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000769
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000770 if (Tok.is(tok::code_completion)) {
771 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000772 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000773 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000774 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000775
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000776 // C++03 [temp.explicit] 14.7.2/8:
777 // The usual access checking rules do not apply to names used to specify
778 // explicit instantiations.
779 //
780 // As an extension we do not perform access checking on the names used to
781 // specify explicit specializations either. This is important to allow
782 // specializing traits classes for private types.
783 bool SuppressingAccessChecks = false;
784 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
785 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
786 Actions.ActOnStartSuppressingAccessChecks();
787 SuppressingAccessChecks = true;
788 }
789
John McCall084e83d2011-03-24 11:26:52 +0000790 ParsedAttributes attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +0000791 // If attributes exist after tag, parse them.
792 if (Tok.is(tok::kw___attribute))
John McCall53fa7142010-12-24 02:08:15 +0000793 ParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +0000794
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000795 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +0000796 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +0000797 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000798
Alexis Hunt96d5c762009-11-21 08:43:09 +0000799 // If C++0x attributes exist here, parse them.
800 // FIXME: Are we consistent with the ordering of parsing of different
801 // styles of attributes?
John McCall53fa7142010-12-24 02:08:15 +0000802 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000803
John Wiegley65497cc2011-04-27 23:09:49 +0000804 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorf1fce5d2011-04-29 15:31:39 +0000805 !Tok.is(tok::identifier) &&
806 Tok.getIdentifierInfo() &&
807 (Tok.is(tok::kw___is_arithmetic) ||
808 Tok.is(tok::kw___is_convertible) ||
John Wiegley65497cc2011-04-27 23:09:49 +0000809 Tok.is(tok::kw___is_empty) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +0000810 Tok.is(tok::kw___is_floating_point) ||
811 Tok.is(tok::kw___is_function) ||
John Wiegley65497cc2011-04-27 23:09:49 +0000812 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +0000813 Tok.is(tok::kw___is_integral) ||
814 Tok.is(tok::kw___is_member_function_pointer) ||
815 Tok.is(tok::kw___is_member_pointer) ||
816 Tok.is(tok::kw___is_pod) ||
817 Tok.is(tok::kw___is_pointer) ||
818 Tok.is(tok::kw___is_same) ||
Douglas Gregor63180b12011-04-29 01:38:03 +0000819 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +0000820 Tok.is(tok::kw___is_signed) ||
821 Tok.is(tok::kw___is_unsigned) ||
822 Tok.is(tok::kw___is_void))) {
823 // GNU libstdc++ 4.2 and libc++ uaw certain intrinsic names as the
824 // name of struct templates, but some are keywords in GCC >= 4.3
825 // and Clang. Therefore, when we see the token sequence "struct
826 // X", make X into a normal identifier rather than a keyword, to
827 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000828 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregor119b0c72009-09-04 05:53:02 +0000829 Tok.setKind(tok::identifier);
830 }
Mike Stump11289f42009-09-09 15:08:12 +0000831
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000832 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +0000833 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000834 if (getLang().CPlusPlus) {
835 // "FOO : BAR" is not a potential typo for "FOO::BAR".
836 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000837
John McCallba7bf592010-08-24 05:47:05 +0000838 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall413021a2010-07-30 06:26:29 +0000839 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +0000840 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000841 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
842 Diag(Tok, diag::err_expected_ident);
843 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000844
Douglas Gregor916462b2009-10-30 21:46:58 +0000845 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
846
Douglas Gregor67a65642009-02-17 23:15:12 +0000847 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +0000848 IdentifierInfo *Name = 0;
849 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000850 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000851 if (Tok.is(tok::identifier)) {
852 Name = Tok.getIdentifierInfo();
853 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000854
Douglas Gregord5a479c2010-05-30 22:30:21 +0000855 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000856 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +0000857 // Eat the template argument list and try to continue parsing this as
858 // a class (or template thereof).
859 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +0000860 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +0000861 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +0000862 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000863 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +0000864 // We couldn't parse the template argument list at all, so don't
865 // try to give any location information for the list.
866 LAngleLoc = RAngleLoc = SourceLocation();
867 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000868
Douglas Gregor916462b2009-10-30 21:46:58 +0000869 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000870 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor916462b2009-10-30 21:46:58 +0000871 << (TagType == DeclSpec::TST_class? 0
872 : TagType == DeclSpec::TST_struct? 1
873 : 2)
874 << Name
875 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000876
877 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000878 // we've removed its template argument list.
879 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
880 if (TemplateParams && TemplateParams->size() > 1) {
881 TemplateParams->pop_back();
882 } else {
883 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000884 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000885 = ParsedTemplateInfo::NonTemplate;
886 }
887 } else if (TemplateInfo.Kind
888 == ParsedTemplateInfo::ExplicitInstantiation) {
889 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +0000890 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000891 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +0000892 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000893 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000894 = SourceLocation();
895 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
896 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +0000897 }
Douglas Gregor916462b2009-10-30 21:46:58 +0000898 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000899 } else if (Tok.is(tok::annot_template_id)) {
900 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
901 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +0000902
Douglas Gregore7c20652011-03-02 00:47:37 +0000903 if (TemplateId->Kind != TNK_Type_template &&
904 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000905 // The template-name in the simple-template-id refers to
906 // something other than a class template. Give an appropriate
907 // error message and skip to the ';'.
908 SourceRange Range(NameLoc);
909 if (SS.isNotEmpty())
910 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +0000911
Douglas Gregor7f741122009-02-25 19:37:18 +0000912 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
913 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +0000914
Douglas Gregor7f741122009-02-25 19:37:18 +0000915 DS.SetTypeSpecError();
916 SkipUntil(tok::semi, false, true);
917 TemplateId->Destroy();
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000918 if (SuppressingAccessChecks)
919 Actions.ActOnStopSuppressingAccessChecks();
920
Douglas Gregor7f741122009-02-25 19:37:18 +0000921 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000922 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000923 }
924
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000925 // As soon as we're finished parsing the class's template-id, turn access
926 // checking back on.
927 if (SuppressingAccessChecks)
928 Actions.ActOnStopSuppressingAccessChecks();
929
John McCall07e91c02009-08-06 02:15:43 +0000930 // There are four options here. If we have 'struct foo;', then this
931 // is either a forward declaration or a friend declaration, which
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000932 // have to be treated differently. If we have 'struct foo {...',
Anders Carlsson65c76d32011-03-25 14:55:14 +0000933 // 'struct foo :...' or 'struct foo final[opt]' then this is a
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000934 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redl2b372722010-02-03 21:21:43 +0000935 // However, in some contexts, things look like declarations but are just
936 // references, e.g.
937 // new struct s;
938 // or
939 // &T::operator struct s;
940 // For these, SuppressDeclarations is true.
John McCallfaf5fb42010-08-26 23:41:50 +0000941 Sema::TagUseKind TUK;
Sebastian Redl2b372722010-02-03 21:21:43 +0000942 if (SuppressDeclarations)
John McCallfaf5fb42010-08-26 23:41:50 +0000943 TUK = Sema::TUK_Reference;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000944 else if (Tok.is(tok::l_brace) ||
945 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlssoncafbab72011-03-25 14:53:29 +0000946 isCXX0XFinalKeyword()) {
Douglas Gregor3dad8422009-09-26 06:47:28 +0000947 if (DS.isFriendSpecified()) {
948 // C++ [class.friend]p2:
949 // A class shall not be defined in a friend declaration.
950 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
951 << SourceRange(DS.getFriendSpecLoc());
952
953 // Skip everything up to the semicolon, so that this looks like a proper
954 // friend class (or template thereof) declaration.
955 SkipUntil(tok::semi, true, true);
John McCallfaf5fb42010-08-26 23:41:50 +0000956 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000957 } else {
958 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +0000959 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000960 }
961 } else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +0000962 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregor556877c2008-04-13 21:30:24 +0000963 else
John McCallfaf5fb42010-08-26 23:41:50 +0000964 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +0000965
John McCall413021a2010-07-30 06:26:29 +0000966 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +0000967 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +0000968 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
969 // We have a declaration or reference to an anonymous class.
970 Diag(StartLoc, diag::err_anon_type_definition)
971 << DeclSpec::getSpecifierName(TagType);
972 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000973
Douglas Gregor556877c2008-04-13 21:30:24 +0000974 SkipUntil(tok::comma, true);
Douglas Gregor7f741122009-02-25 19:37:18 +0000975
976 if (TemplateId)
977 TemplateId->Destroy();
Douglas Gregor556877c2008-04-13 21:30:24 +0000978 return;
979 }
980
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000981 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +0000982 DeclResult TagOrTempResult = true; // invalid
983 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000984
Douglas Gregord6ab8742009-05-28 23:31:59 +0000985 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +0000986 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000987 // Explicit specialization, class template partial specialization,
988 // or explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000989 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000990 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000991 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000992 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +0000993 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000994 // This is an explicit instantiation of a class template.
995 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000996 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +0000997 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000998 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000999 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001000 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001001 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001002 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001003 TemplateId->TemplateNameLoc,
1004 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001005 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001006 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001007 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001008
1009 // Friend template-ids are treated as references unless
1010 // they have template headers, in which case they're ill-formed
1011 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1012 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001013 } else if (TUK == Sema::TUK_Reference ||
1014 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001015 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Douglas Gregore7c20652011-03-02 00:47:37 +00001016 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType,
1017 StartLoc,
1018 TemplateId->SS,
1019 TemplateId->Template,
1020 TemplateId->TemplateNameLoc,
1021 TemplateId->LAngleLoc,
1022 TemplateArgsPtr,
1023 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001024 } else {
1025 // This is an explicit specialization or a class template
1026 // partial specialization.
1027 TemplateParameterLists FakedParamLists;
1028
1029 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1030 // This looks like an explicit instantiation, because we have
1031 // something like
1032 //
1033 // template class Foo<X>
1034 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001035 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001036 // meant to be an explicit specialization, but the user forgot
1037 // the '<>' after 'template'.
John McCallfaf5fb42010-08-26 23:41:50 +00001038 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001039
Mike Stump11289f42009-09-09 15:08:12 +00001040 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001041 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001042 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001043 diag::err_explicit_instantiation_with_definition)
1044 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregora771f462010-03-31 17:46:05 +00001045 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001046
1047 // Create a fake template parameter list that contains only
1048 // "template<>", so that we treat this construct as a class
1049 // template specialization.
1050 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +00001051 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001052 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001053 LAngleLoc,
1054 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001055 LAngleLoc));
1056 TemplateParams = &FakedParamLists;
1057 }
1058
1059 // Build the class template specialization.
1060 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001061 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor7f741122009-02-25 19:37:18 +00001062 StartLoc, SS,
John McCall3e56fd42010-08-23 07:28:44 +00001063 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001064 TemplateId->TemplateNameLoc,
1065 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +00001066 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001067 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001068 attrs.getList(),
John McCallfaf5fb42010-08-26 23:41:50 +00001069 MultiTemplateParamsArg(Actions,
Douglas Gregor67a65642009-02-17 23:15:12 +00001070 TemplateParams? &(*TemplateParams)[0] : 0,
1071 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001072 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001073 TemplateId->Destroy();
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001074 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001075 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001076 // Explicit instantiation of a member of a class template
1077 // specialization, e.g.,
1078 //
1079 // template struct Outer<int>::Inner;
1080 //
1081 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001082 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001083 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001084 TemplateInfo.TemplateLoc,
1085 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001086 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001087 } else if (TUK == Sema::TUK_Friend &&
1088 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1089 TagOrTempResult =
1090 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1091 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001092 Name, NameLoc, attrs.getList(),
John McCallace48cd2010-10-19 01:40:49 +00001093 MultiTemplateParamsArg(Actions,
1094 TemplateParams? &(*TemplateParams)[0] : 0,
1095 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001096 } else {
1097 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001098 TUK == Sema::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001099 // FIXME: Diagnose this particular error.
1100 }
1101
John McCall7f41d982009-09-11 04:59:25 +00001102 bool IsDependent = false;
1103
John McCall32723e92010-10-19 18:40:57 +00001104 // Don't pass down template parameter lists if this is just a tag
1105 // reference. For example, we don't need the template parameters here:
1106 // template <class T> class A *makeA(T t);
1107 MultiTemplateParamsArg TParams;
1108 if (TUK != Sema::TUK_Reference && TemplateParams)
1109 TParams =
1110 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1111
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001112 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001113 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001114 SS, Name, NameLoc, attrs.getList(), AS,
John McCall32723e92010-10-19 18:40:57 +00001115 TParams, Owned, IsDependent, false,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001116 false, clang::TypeResult());
John McCall7f41d982009-09-11 04:59:25 +00001117
1118 // If ActOnTag said the type was dependent, try again with the
1119 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001120 if (IsDependent) {
1121 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001122 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001123 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001124 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001125 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001126
Douglas Gregor556877c2008-04-13 21:30:24 +00001127 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001128 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001129 assert(Tok.is(tok::l_brace) ||
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001130 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlssoncafbab72011-03-25 14:53:29 +00001131 isCXX0XFinalKeyword());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001132 if (getLang().CPlusPlus)
Douglas Gregorc08f4892009-03-25 00:13:59 +00001133 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001134 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001135 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001136 }
1137
John McCallba7bf592010-08-24 05:47:05 +00001138 const char *PrevSpec = 0;
1139 unsigned DiagID;
1140 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001141 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001142 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1143 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallba7bf592010-08-24 05:47:05 +00001144 PrevSpec, DiagID, TypeResult.get());
John McCall7f41d982009-09-11 04:59:25 +00001145 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001146 Result = DS.SetTypeSpecType(TagType, StartLoc,
1147 NameLoc.isValid() ? NameLoc : StartLoc,
1148 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCall7f41d982009-09-11 04:59:25 +00001149 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001150 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001151 return;
1152 }
Mike Stump11289f42009-09-09 15:08:12 +00001153
John McCallba7bf592010-08-24 05:47:05 +00001154 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001155 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001156
Chris Lattnercf251412010-02-02 01:23:29 +00001157 // At this point, we've successfully parsed a class-specifier in 'definition'
1158 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1159 // going to look at what comes after it to improve error recovery. If an
1160 // impossible token occurs next, we assume that the programmer forgot a ; at
1161 // the end of the declaration and recover that way.
1162 //
1163 // This switch enumerates the valid "follow" set for definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001164 if (TUK == Sema::TUK_Definition) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001165 bool ExpectedSemi = true;
Chris Lattnercf251412010-02-02 01:23:29 +00001166 switch (Tok.getKind()) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001167 default: break;
Chris Lattnercf251412010-02-02 01:23:29 +00001168 case tok::semi: // struct foo {...} ;
Chris Lattnerafe6a842010-02-02 17:32:27 +00001169 case tok::star: // struct foo {...} * P;
1170 case tok::amp: // struct foo {...} & R = ...
1171 case tok::identifier: // struct foo {...} V ;
1172 case tok::r_paren: //(struct foo {...} ) {4}
1173 case tok::annot_cxxscope: // struct foo {...} a:: b;
1174 case tok::annot_typename: // struct foo {...} a ::b;
1175 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattner5e854b92010-02-03 20:41:24 +00001176 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner35af0ab2010-02-03 01:45:03 +00001177 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001178 ExpectedSemi = false;
1179 break;
1180 // Type qualifiers
1181 case tok::kw_const: // struct foo {...} const x;
1182 case tok::kw_volatile: // struct foo {...} volatile x;
1183 case tok::kw_restrict: // struct foo {...} restrict x;
1184 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattnerafe6a842010-02-02 17:32:27 +00001185 // Storage-class specifiers
1186 case tok::kw_static: // struct foo {...} static x;
1187 case tok::kw_extern: // struct foo {...} extern x;
1188 case tok::kw_typedef: // struct foo {...} typedef x;
1189 case tok::kw_register: // struct foo {...} register x;
1190 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregorc9a99c52010-05-17 18:19:56 +00001191 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001192 // As shown above, type qualifiers and storage class specifiers absolutely
1193 // can occur after class specifiers according to the grammar. However,
Chris Lattner57540c52011-04-15 05:22:18 +00001194 // almost no one actually writes code like this. If we see one of these,
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001195 // it is much more likely that someone missed a semi colon and the
1196 // type/storage class specifier we're seeing is part of the *next*
1197 // intended declaration, as in:
1198 //
1199 // struct foo { ... }
1200 // typedef int X;
1201 //
1202 // We'd really like to emit a missing semicolon error instead of emitting
1203 // an error on the 'int' saying that you can't have two type specifiers in
1204 // the same declaration of X. Because of this, we look ahead past this
1205 // token to see if it's a type specifier. If so, we know the code is
1206 // otherwise invalid, so we can produce the expected semi error.
1207 if (!isKnownToBeTypeSpecifier(NextToken()))
1208 ExpectedSemi = false;
Chris Lattnercf251412010-02-02 01:23:29 +00001209 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001210
1211 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattnercf251412010-02-02 01:23:29 +00001212 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001213 if (!getLang().CPlusPlus)
1214 ExpectedSemi = false;
1215 break;
1216 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001217
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001218 if (ExpectedSemi) {
Chris Lattnercf251412010-02-02 01:23:29 +00001219 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1220 TagType == DeclSpec::TST_class ? "class"
1221 : TagType == DeclSpec::TST_struct? "struct" : "union");
1222 // Push this token back into the preprocessor and change our current token
1223 // to ';' so that the rest of the code recovers as though there were an
1224 // ';' after the definition.
1225 PP.EnterToken(Tok);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001226 Tok.setKind(tok::semi);
Chris Lattnercf251412010-02-02 01:23:29 +00001227 }
1228 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001229}
1230
Mike Stump11289f42009-09-09 15:08:12 +00001231/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001232///
1233/// base-clause : [C++ class.derived]
1234/// ':' base-specifier-list
1235/// base-specifier-list:
1236/// base-specifier '...'[opt]
1237/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001238void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001239 assert(Tok.is(tok::colon) && "Not a base clause");
1240 ConsumeToken();
1241
Douglas Gregor29a92472008-10-22 17:49:05 +00001242 // Build up an array of parsed base specifiers.
John McCall37ad5512010-08-23 06:44:23 +00001243 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001244
Douglas Gregor556877c2008-04-13 21:30:24 +00001245 while (true) {
1246 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001247 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001248 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001249 // Skip the rest of this base specifier, up until the comma or
1250 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +00001251 SkipUntil(tok::comma, tok::l_brace, true, true);
1252 } else {
1253 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001254 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001255 }
1256
1257 // If the next token is a comma, consume it and keep reading
1258 // base-specifiers.
1259 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001260
Douglas Gregor556877c2008-04-13 21:30:24 +00001261 // Consume the comma.
1262 ConsumeToken();
1263 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001264
1265 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001266 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001267}
1268
1269/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1270/// one entry in the base class list of a class specifier, for example:
1271/// class foo : public bar, virtual private baz {
1272/// 'public bar' and 'virtual private baz' are each base-specifiers.
1273///
1274/// base-specifier: [C++ class.derived]
1275/// ::[opt] nested-name-specifier[opt] class-name
1276/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1277/// class-name
1278/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1279/// class-name
John McCall48871652010-08-21 09:40:31 +00001280Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001281 bool IsVirtual = false;
1282 SourceLocation StartLoc = Tok.getLocation();
1283
1284 // Parse the 'virtual' keyword.
1285 if (Tok.is(tok::kw_virtual)) {
1286 ConsumeToken();
1287 IsVirtual = true;
1288 }
1289
1290 // Parse an (optional) access specifier.
1291 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001292 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001293 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001294
Douglas Gregor556877c2008-04-13 21:30:24 +00001295 // Parse the 'virtual' keyword (again!), in case it came after the
1296 // access specifier.
1297 if (Tok.is(tok::kw_virtual)) {
1298 SourceLocation VirtualLoc = ConsumeToken();
1299 if (IsVirtual) {
1300 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001301 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001302 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001303 }
1304
1305 IsVirtual = true;
1306 }
1307
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001308 // Parse optional '::' and optional nested-name-specifier.
1309 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001310 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor556877c2008-04-13 21:30:24 +00001311
Douglas Gregor556877c2008-04-13 21:30:24 +00001312 // The location of the base class itself.
1313 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor831c93f2008-11-05 20:51:48 +00001314
1315 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001316 SourceLocation EndLocation;
Douglas Gregore7c20652011-03-02 00:47:37 +00001317 TypeResult BaseType = ParseClassName(EndLocation, SS);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001318 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001319 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001320
Douglas Gregor752a5952011-01-03 22:36:02 +00001321 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1322 // actually part of the base-specifier-list grammar productions, but we
1323 // parse it here for convenience.
1324 SourceLocation EllipsisLoc;
1325 if (Tok.is(tok::ellipsis))
1326 EllipsisLoc = ConsumeToken();
1327
Mike Stump11289f42009-09-09 15:08:12 +00001328 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001329 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001330
Douglas Gregor556877c2008-04-13 21:30:24 +00001331 // Notify semantic analysis that we have parsed a complete
1332 // base-specifier.
Sebastian Redl511ed552008-11-25 22:21:31 +00001333 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor752a5952011-01-03 22:36:02 +00001334 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001335}
1336
1337/// getAccessSpecifierIfPresent - Determine whether the next token is
1338/// a C++ access-specifier.
1339///
1340/// access-specifier: [C++ class.derived]
1341/// 'private'
1342/// 'protected'
1343/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001344AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001345 switch (Tok.getKind()) {
1346 default: return AS_none;
1347 case tok::kw_private: return AS_private;
1348 case tok::kw_protected: return AS_protected;
1349 case tok::kw_public: return AS_public;
1350 }
1351}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001352
Eli Friedman3af2a772009-07-22 21:45:50 +00001353void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCall48871652010-08-21 09:40:31 +00001354 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001355 // We just declared a member function. If this member function
1356 // has any default arguments, we'll need to parse them later.
1357 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001358 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001359 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedman3af2a772009-07-22 21:45:50 +00001360 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1361 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1362 if (!LateMethod) {
1363 // Push this method onto the stack of late-parsed method
1364 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001365 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1366 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001367 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001368
1369 // Add all of the parameters prior to this one (they don't
1370 // have default arguments).
1371 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1372 for (unsigned I = 0; I < ParamIdx; ++I)
1373 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001374 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001375 }
1376
1377 // Add this parameter to the list of parameters (it or may
1378 // not have a default argument).
1379 LateMethod->DefaultArgs.push_back(
1380 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1381 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1382 }
1383 }
1384}
1385
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001386/// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x
1387/// virt-specifier.
1388///
1389/// virt-specifier:
1390/// override
1391/// final
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001392VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const {
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001393 if (!getLang().CPlusPlus)
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001394 return VirtSpecifiers::VS_None;
1395
Anders Carlsson56104902011-01-17 03:05:47 +00001396 if (Tok.is(tok::identifier)) {
1397 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001398
Anders Carlsson428803b2011-01-20 03:47:08 +00001399 // Initialize the contextual keywords.
1400 if (!Ident_final) {
1401 Ident_final = &PP.getIdentifierTable().get("final");
1402 Ident_override = &PP.getIdentifierTable().get("override");
1403 }
1404
Anders Carlsson56104902011-01-17 03:05:47 +00001405 if (II == Ident_override)
1406 return VirtSpecifiers::VS_Override;
1407
1408 if (II == Ident_final)
1409 return VirtSpecifiers::VS_Final;
1410 }
1411
1412 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001413}
1414
1415/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1416///
1417/// virt-specifier-seq:
1418/// virt-specifier
1419/// virt-specifier-seq virt-specifier
Anders Carlsson56104902011-01-17 03:05:47 +00001420void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlsson56104902011-01-17 03:05:47 +00001421 while (true) {
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001422 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001423 if (Specifier == VirtSpecifiers::VS_None)
1424 return;
1425
1426 // C++ [class.mem]p8:
1427 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001428 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001429 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001430 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1431 << PrevSpec
1432 << FixItHint::CreateRemoval(Tok.getLocation());
1433
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001434 if (!getLang().CPlusPlus0x)
1435 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1436 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlsson56104902011-01-17 03:05:47 +00001437 ConsumeToken();
1438 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001439}
1440
Anders Carlssoncafbab72011-03-25 14:53:29 +00001441/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1442/// contextual 'final' keyword.
1443bool Parser::isCXX0XFinalKeyword() const {
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001444 if (!getLang().CPlusPlus)
Anders Carlssoncafbab72011-03-25 14:53:29 +00001445 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001446
Anders Carlssoncafbab72011-03-25 14:53:29 +00001447 if (!Tok.is(tok::identifier))
1448 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001449
Anders Carlssoncafbab72011-03-25 14:53:29 +00001450 // Initialize the contextual keywords.
1451 if (!Ident_final) {
1452 Ident_final = &PP.getIdentifierTable().get("final");
1453 Ident_override = &PP.getIdentifierTable().get("override");
1454 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001455
Anders Carlssoncafbab72011-03-25 14:53:29 +00001456 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001457}
1458
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001459/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1460///
1461/// member-declaration:
1462/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1463/// function-definition ';'[opt]
1464/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1465/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001466/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001467/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001468/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001469///
1470/// member-declarator-list:
1471/// member-declarator
1472/// member-declarator-list ',' member-declarator
1473///
1474/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001475/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001476/// declarator constant-initializer[opt]
1477/// identifier[opt] ':' constant-expression
1478///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001479/// virt-specifier-seq:
1480/// virt-specifier
1481/// virt-specifier-seq virt-specifier
1482///
1483/// virt-specifier:
1484/// override
1485/// final
1486/// new
1487///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001488/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001489/// '= 0'
1490///
1491/// constant-initializer:
1492/// '=' constant-expression
1493///
Douglas Gregor3447e762009-08-20 22:52:58 +00001494void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCall796c2a52010-07-16 08:13:16 +00001495 const ParsedTemplateInfo &TemplateInfo,
1496 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001497 if (Tok.is(tok::at)) {
1498 if (getLang().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
1499 Diag(Tok, diag::err_at_defs_cxx);
1500 else
1501 Diag(Tok, diag::err_at_in_class);
1502
1503 ConsumeToken();
1504 SkipUntil(tok::r_brace);
1505 return;
1506 }
1507
John McCalla0097262009-12-11 02:10:03 +00001508 // Access declarations.
1509 if (!TemplateInfo.Kind &&
1510 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall1f476a12010-02-26 08:45:28 +00001511 !TryAnnotateCXXScopeToken() &&
John McCalla0097262009-12-11 02:10:03 +00001512 Tok.is(tok::annot_cxxscope)) {
1513 bool isAccessDecl = false;
1514 if (NextToken().is(tok::identifier))
1515 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1516 else
1517 isAccessDecl = NextToken().is(tok::kw_operator);
1518
1519 if (isAccessDecl) {
1520 // Collect the scope specifier token we annotated earlier.
1521 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001522 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCalla0097262009-12-11 02:10:03 +00001523
1524 // Try to parse an unqualified-id.
1525 UnqualifiedId Name;
John McCallba7bf592010-08-24 05:47:05 +00001526 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCalla0097262009-12-11 02:10:03 +00001527 SkipUntil(tok::semi);
1528 return;
1529 }
1530
1531 // TODO: recover from mistakenly-qualified operator declarations.
1532 if (ExpectAndConsume(tok::semi,
1533 diag::err_expected_semi_after,
1534 "access declaration",
1535 tok::semi))
1536 return;
1537
Douglas Gregor0be31a22010-07-02 17:43:08 +00001538 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCalla0097262009-12-11 02:10:03 +00001539 false, SourceLocation(),
1540 SS, Name,
1541 /* AttrList */ 0,
1542 /* IsTypeName */ false,
1543 SourceLocation());
1544 return;
1545 }
1546 }
1547
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001548 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001549 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001550 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001551 SourceLocation DeclEnd;
1552 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001553 return;
1554 }
Mike Stump11289f42009-09-09 15:08:12 +00001555
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001556 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001557 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001558 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001559 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001560 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001561 AS);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001562 return;
1563 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001564
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001565 // Handle: member-declaration ::= '__extension__' member-declaration
1566 if (Tok.is(tok::kw___extension__)) {
1567 // __extension__ silences extension warnings in the subexpression.
1568 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1569 ConsumeToken();
John McCall796c2a52010-07-16 08:13:16 +00001570 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001571 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001572
Chris Lattnercf251412010-02-02 01:23:29 +00001573 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1574 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001575 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001576
John McCall084e83d2011-03-24 11:26:52 +00001577 ParsedAttributesWithRange attrs(AttrFactory);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001578 // Optional C++0x attribute-specifier
John McCall53fa7142010-12-24 02:08:15 +00001579 MaybeParseCXX0XAttributes(attrs);
1580 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001581
Douglas Gregorfec52632009-06-20 00:51:54 +00001582 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00001583 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001584
Douglas Gregorfec52632009-06-20 00:51:54 +00001585 // Eat 'using'.
1586 SourceLocation UsingLoc = ConsumeToken();
1587
1588 if (Tok.is(tok::kw_namespace)) {
1589 Diag(UsingLoc, diag::err_using_namespace_in_class);
1590 SkipUntil(tok::semi, true, true);
Chris Lattner916dbf12010-02-02 00:43:15 +00001591 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00001592 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00001593 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00001594 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1595 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001596 }
1597 return;
1598 }
1599
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001600 // decl-specifier-seq:
1601 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00001602 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00001603 DS.takeAttributesFrom(attrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00001604 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001605
John McCallfaf5fb42010-08-26 23:41:50 +00001606 MultiTemplateParamsArg TemplateParams(Actions,
John McCall11083da2009-09-16 22:47:08 +00001607 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1608 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1609
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001610 if (Tok.is(tok::semi)) {
1611 ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001612 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00001613 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00001614 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00001615 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001616 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001617
John McCall28a6aea2009-11-04 02:18:39 +00001618 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00001619 VirtSpecifiers VS;
Francois Pichet3abc9b82011-05-11 02:14:46 +00001620 ExprResult Init;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001621
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001622 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001623 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1624 ColonProtectionRAIIObject X(*this);
1625
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001626 // Parse the first declarator.
1627 ParseDeclarator(DeclaratorInfo);
1628 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00001629 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001630 // If so, skip until the semi-colon or a }.
Sebastian Redl83f3b852011-04-24 16:27:48 +00001631 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001632 if (Tok.is(tok::semi))
1633 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001634 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001635 }
1636
Nico Weber24b2a822011-01-28 06:07:34 +00001637 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1638
John Thompson5bc5cbe2009-11-25 22:58:06 +00001639 // If attributes exist after the declarator, but before an '{', parse them.
John McCall53fa7142010-12-24 02:08:15 +00001640 MaybeParseGNUAttributes(DeclaratorInfo);
John Thompson5bc5cbe2009-11-25 22:58:06 +00001641
Francois Pichet3abc9b82011-05-11 02:14:46 +00001642 // MSVC permits pure specifier on inline functions declared at class scope.
1643 // Hence check for =0 before checking for function definition.
1644 if (getLang().Microsoft && Tok.is(tok::equal) &&
1645 DeclaratorInfo.isFunctionDeclarator() &&
1646 NextToken().is(tok::numeric_constant)) {
1647 ConsumeToken();
1648 Init = ParseInitializer();
1649 if (Init.isInvalid())
1650 SkipUntil(tok::comma, true, true);
1651 }
1652
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001653 bool IsDefinition = false;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001654 // function-definition:
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001655 if (Tok.is(tok::l_brace)) {
1656 IsDefinition = true;
1657 } else if (DeclaratorInfo.isFunctionDeclarator()) {
1658 if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
1659 IsDefinition = true;
1660 } else if (Tok.is(tok::equal)) {
1661 const Token &KW = NextToken();
1662 if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
1663 IsDefinition = true;
1664 }
1665 }
1666
1667 if (IsDefinition) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001668 if (!DeclaratorInfo.isFunctionDeclarator()) {
1669 Diag(Tok, diag::err_func_def_no_params);
1670 ConsumeBrace();
1671 SkipUntil(tok::r_brace, true);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001672
1673 // Consume the optional ';'
1674 if (Tok.is(tok::semi))
1675 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001676 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001677 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001678
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001679 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1680 Diag(Tok, diag::err_function_declared_typedef);
1681 // This recovery skips the entire function body. It would be nice
1682 // to simply call ParseCXXInlineMethodDef() below, however Sema
1683 // assumes the declarator represents a function, not a typedef.
1684 ConsumeBrace();
1685 SkipUntil(tok::r_brace, true);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001686
1687 // Consume the optional ';'
1688 if (Tok.is(tok::semi))
1689 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001690 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001691 }
1692
Francois Pichet3abc9b82011-05-11 02:14:46 +00001693 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS, Init);
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001694
1695 // Consume the ';' - it's optional unless we have a delete or default
1696 if (Tok.is(tok::semi)) {
Douglas Gregor8a4db832011-01-19 16:41:58 +00001697 ConsumeToken();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001698 }
Douglas Gregor8a4db832011-01-19 16:41:58 +00001699
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001700 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001701 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001702 }
1703
1704 // member-declarator-list:
1705 // member-declarator
1706 // member-declarator-list ',' member-declarator
1707
John McCall48871652010-08-21 09:40:31 +00001708 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCalldadc5752010-08-24 06:29:42 +00001709 ExprResult BitfieldSize;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001710
1711 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001712 // member-declarator:
1713 // declarator pure-specifier[opt]
1714 // declarator constant-initializer[opt]
1715 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001716 if (Tok.is(tok::colon)) {
1717 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001718 BitfieldSize = ParseConstantExpression();
1719 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001720 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001721 }
Mike Stump11289f42009-09-09 15:08:12 +00001722
Anders Carlsson56104902011-01-17 03:05:47 +00001723 ParseOptionalCXX0XVirtSpecifierSeq(VS);
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001724
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001725 // pure-specifier:
1726 // '= 0'
1727 //
1728 // constant-initializer:
1729 // '=' constant-expression
Sebastian Redl42e92c42009-04-12 17:16:29 +00001730 //
1731 // defaulted/deleted function-definition:
1732 // '=' 'default' [TODO]
1733 // '=' 'delete'
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001734 if (Tok.is(tok::equal)) {
1735 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001736 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001737 if (DeclaratorInfo.isFunctionDeclarator())
1738 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1739 << 1 /* delete */;
1740 else
1741 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001742 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001743 if (DeclaratorInfo.isFunctionDeclarator())
1744 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1745 << 1 /* delete */;
1746 else
1747 Diag(ConsumeToken(), diag::err_default_special_members);
Sebastian Redl42e92c42009-04-12 17:16:29 +00001748 } else {
1749 Init = ParseInitializer();
1750 if (Init.isInvalid())
1751 SkipUntil(tok::comma, true, true);
1752 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001753 }
1754
Chris Lattnerf3d3b362010-06-13 05:34:18 +00001755 // If a simple-asm-expr is present, parse it.
1756 if (Tok.is(tok::kw_asm)) {
1757 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001758 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnerf3d3b362010-06-13 05:34:18 +00001759 if (AsmLabel.isInvalid())
1760 SkipUntil(tok::comma, true, true);
1761
1762 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1763 DeclaratorInfo.SetRangeEnd(Loc);
1764 }
1765
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001766 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001767 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001768
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001769 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001770 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001771 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00001772
John McCall48871652010-08-21 09:40:31 +00001773 Decl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00001774 if (DS.isFriendSpecified()) {
John McCall2f212b32009-09-11 21:02:39 +00001775 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00001776 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCall2f212b32009-09-11 21:02:39 +00001777 /*IsDefinition*/ false,
1778 move(TemplateParams));
Douglas Gregor3447e762009-08-20 22:52:58 +00001779 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001780 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00001781 DeclaratorInfo,
Douglas Gregor3447e762009-08-20 22:52:58 +00001782 move(TemplateParams),
John McCall07e91c02009-08-06 02:15:43 +00001783 BitfieldSize.release(),
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001784 VS, Init.release(), false);
Douglas Gregor3447e762009-08-20 22:52:58 +00001785 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001786 if (ThisDecl)
1787 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001788
Douglas Gregor4d87df52008-12-16 21:30:33 +00001789 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump11289f42009-09-09 15:08:12 +00001790 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor4d87df52008-12-16 21:30:33 +00001791 != DeclSpec::SCS_typedef) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001792 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor4d87df52008-12-16 21:30:33 +00001793 }
1794
John McCall28a6aea2009-11-04 02:18:39 +00001795 DeclaratorInfo.complete(ThisDecl);
1796
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001797 // If we don't have a comma, it is either the end of the list (a ';')
1798 // or an error, bail out.
1799 if (Tok.isNot(tok::comma))
1800 break;
Mike Stump11289f42009-09-09 15:08:12 +00001801
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001802 // Consume the comma.
1803 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001804
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001805 // Parse the next declarator.
1806 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00001807 VS.clear();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001808 BitfieldSize = 0;
1809 Init = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001810
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001811 // Attributes are only allowed on the second declarator.
John McCall53fa7142010-12-24 02:08:15 +00001812 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001813
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001814 if (Tok.isNot(tok::colon))
1815 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001816 }
1817
Chris Lattner916dbf12010-02-02 00:43:15 +00001818 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1819 // Skip to end of block or statement.
1820 SkipUntil(tok::r_brace, true, true);
1821 // If we stopped at a ';', eat it.
1822 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001823 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001824 }
1825
Douglas Gregor0be31a22010-07-02 17:43:08 +00001826 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattner916dbf12010-02-02 00:43:15 +00001827 DeclsInGroup.size());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001828}
1829
1830/// ParseCXXMemberSpecification - Parse the class definition.
1831///
1832/// member-specification:
1833/// member-declaration member-specification[opt]
1834/// access-specifier ':' member-specification[opt]
1835///
1836void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001837 unsigned TagType, Decl *TagDecl) {
Sanjiv Guptad7959242008-10-31 09:52:39 +00001838 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001839 TagType == DeclSpec::TST_union ||
Sanjiv Guptad7959242008-10-31 09:52:39 +00001840 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001841
John McCallfaf5fb42010-08-26 23:41:50 +00001842 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1843 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00001844
Douglas Gregoredf8f392010-01-16 20:52:59 +00001845 // Determine whether this is a non-nested class. Note that local
1846 // classes are *not* considered to be nested classes.
1847 bool NonNestedClass = true;
1848 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001849 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00001850 if (S->isClassScope()) {
1851 // We're inside a class scope, so this is a nested class.
1852 NonNestedClass = false;
1853 break;
1854 }
1855
1856 if ((S->getFlags() & Scope::FnScope)) {
1857 // If we're in a function or function template declared in the
1858 // body of a class, then this is a local class rather than a
1859 // nested class.
1860 const Scope *Parent = S->getParent();
1861 if (Parent->isTemplateParamScope())
1862 Parent = Parent->getParent();
1863 if (Parent->isClassScope())
1864 break;
1865 }
1866 }
1867 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001868
1869 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00001870 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001871
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001872 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregoredf8f392010-01-16 20:52:59 +00001873 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001874
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001875 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001876 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001877
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00001878 SourceLocation FinalLoc;
1879
1880 // Parse the optional 'final' keyword.
1881 if (getLang().CPlusPlus && Tok.is(tok::identifier)) {
1882 IdentifierInfo *II = Tok.getIdentifierInfo();
1883
1884 // Initialize the contextual keywords.
1885 if (!Ident_final) {
1886 Ident_final = &PP.getIdentifierTable().get("final");
1887 Ident_override = &PP.getIdentifierTable().get("override");
1888 }
1889
1890 if (II == Ident_final)
1891 FinalLoc = ConsumeToken();
1892
1893 if (!getLang().CPlusPlus0x)
1894 Diag(FinalLoc, diag::ext_override_control_keyword) << "final";
1895 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001896
John McCall2d814c32009-12-19 21:48:58 +00001897 if (Tok.is(tok::colon)) {
1898 ParseBaseClause(TagDecl);
1899
1900 if (!Tok.is(tok::l_brace)) {
1901 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00001902
1903 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001904 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001905 return;
1906 }
1907 }
1908
1909 assert(Tok.is(tok::l_brace));
1910
1911 SourceLocation LBraceLoc = ConsumeBrace();
1912
John McCall08bede42010-05-28 08:11:17 +00001913 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00001914 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Anders Carlssonfc1eef42011-01-22 17:51:53 +00001915 LBraceLoc);
John McCall1c7e6ec2009-12-20 07:58:13 +00001916
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001917 // C++ 11p3: Members of a class defined with the keyword class are private
1918 // by default. Members of a class defined with the keywords struct or union
1919 // are public by default.
1920 AccessSpecifier CurAS;
1921 if (TagType == DeclSpec::TST_class)
1922 CurAS = AS_private;
1923 else
1924 CurAS = AS_public;
1925
Douglas Gregor9377c822010-06-21 22:31:09 +00001926 SourceLocation RBraceLoc;
1927 if (TagDecl) {
1928 // While we still have something to read, read the member-declarations.
1929 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1930 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001931
Douglas Gregor9377c822010-06-21 22:31:09 +00001932 // Check for extraneous top-level semicolon.
1933 if (Tok.is(tok::semi)) {
1934 Diag(Tok, diag::ext_extra_struct_semi)
1935 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1936 << FixItHint::CreateRemoval(Tok.getLocation());
1937 ConsumeToken();
1938 continue;
1939 }
1940
1941 AccessSpecifier AS = getAccessSpecifierIfPresent();
1942 if (AS != AS_none) {
1943 // Current token is a C++ access specifier.
1944 CurAS = AS;
1945 SourceLocation ASLoc = Tok.getLocation();
1946 ConsumeToken();
1947 if (Tok.is(tok::colon))
1948 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1949 else
1950 Diag(Tok, diag::err_expected_colon);
1951 ConsumeToken();
1952 continue;
1953 }
1954
1955 // FIXME: Make sure we don't have a template here.
1956
1957 // Parse all the comma separated declarators.
1958 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001959 }
1960
Douglas Gregor9377c822010-06-21 22:31:09 +00001961 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1962 } else {
1963 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001964 }
Mike Stump11289f42009-09-09 15:08:12 +00001965
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001966 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001967 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001968 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001969
John McCall08bede42010-05-28 08:11:17 +00001970 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001971 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall08bede42010-05-28 08:11:17 +00001972 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00001973 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001974
1975 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1976 // complete within function bodies, default arguments,
1977 // exception-specifications, and constructor ctor-initializers (including
1978 // such things in nested classes).
1979 //
Douglas Gregor4d87df52008-12-16 21:30:33 +00001980 // FIXME: Only function bodies and constructor ctor-initializers are
1981 // parsed correctly, fix the rest.
Douglas Gregor9377c822010-06-21 22:31:09 +00001982 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001983 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00001984 // are complete and we can parse the delayed portions of method
1985 // declarations and the lexed inline method definitions.
Douglas Gregor428119e2010-06-16 23:45:56 +00001986 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001987 ParseLexedMethodDeclarations(getCurrentClass());
1988 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00001989 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001990 }
1991
John McCall08bede42010-05-28 08:11:17 +00001992 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001993 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCall2ff380a2010-03-17 00:38:33 +00001994
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001995 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001996 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001997 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001998}
Douglas Gregore8381c02008-11-05 04:29:56 +00001999
2000/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2001/// which explicitly initializes the members or base classes of a
2002/// class (C++ [class.base.init]). For example, the three initializers
2003/// after the ':' in the Derived constructor below:
2004///
2005/// @code
2006/// class Base { };
2007/// class Derived : Base {
2008/// int x;
2009/// float f;
2010/// public:
2011/// Derived(float f) : Base(), x(17), f(f) { }
2012/// };
2013/// @endcode
2014///
Mike Stump11289f42009-09-09 15:08:12 +00002015/// [C++] ctor-initializer:
2016/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002017///
Mike Stump11289f42009-09-09 15:08:12 +00002018/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002019/// mem-initializer ...[opt]
2020/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002021void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002022 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2023
John Wiegley1c0675e2011-04-28 01:08:34 +00002024 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2025 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002026 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002027
Alexis Hunt1d792652011-01-08 20:30:50 +00002028 llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002029 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002030
Douglas Gregore8381c02008-11-05 04:29:56 +00002031 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002032 if (Tok.is(tok::code_completion)) {
2033 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2034 MemInitializers.data(),
2035 MemInitializers.size());
2036 ConsumeCodeCompletionToken();
2037 } else {
2038 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2039 if (!MemInit.isInvalid())
2040 MemInitializers.push_back(MemInit.get());
2041 else
2042 AnyErrors = true;
2043 }
2044
Douglas Gregore8381c02008-11-05 04:29:56 +00002045 if (Tok.is(tok::comma))
2046 ConsumeToken();
2047 else if (Tok.is(tok::l_brace))
2048 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002049 // If the next token looks like a base or member initializer, assume that
2050 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002051 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2052 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2053 Diag(Loc, diag::err_ctor_init_missing_comma)
2054 << FixItHint::CreateInsertion(Loc, ", ");
2055 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002056 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00002057 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00002058 SkipUntil(tok::l_brace, true, true);
2059 break;
2060 }
2061 } while (true);
2062
Mike Stump11289f42009-09-09 15:08:12 +00002063 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002064 MemInitializers.data(), MemInitializers.size(),
2065 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002066}
2067
2068/// ParseMemInitializer - Parse a C++ member initializer, which is
2069/// part of a constructor initializer that explicitly initializes one
2070/// member or base class (C++ [class.base.init]). See
2071/// ParseConstructorInitializer for an example.
2072///
2073/// [C++] mem-initializer:
2074/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump11289f42009-09-09 15:08:12 +00002075///
Douglas Gregore8381c02008-11-05 04:29:56 +00002076/// [C++] mem-initializer-id:
2077/// '::'[opt] nested-name-specifier[opt] class-name
2078/// identifier
John McCall48871652010-08-21 09:40:31 +00002079Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002080 // parse '::'[opt] nested-name-specifier[opt]
2081 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002082 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
2083 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002084 if (Tok.is(tok::annot_template_id)) {
2085 TemplateIdAnnotation *TemplateId
2086 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +00002087 if (TemplateId->Kind == TNK_Type_template ||
2088 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002089 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002090 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002091 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002092 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002093 }
2094 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002095 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002096 return true;
2097 }
Mike Stump11289f42009-09-09 15:08:12 +00002098
Douglas Gregore8381c02008-11-05 04:29:56 +00002099 // Get the identifier. This may be a member name or a class name,
2100 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002101 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregore8381c02008-11-05 04:29:56 +00002102 SourceLocation IdLoc = ConsumeToken();
2103
2104 // Parse the '('.
2105 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002106 Diag(Tok, diag::err_expected_lparen);
Douglas Gregore8381c02008-11-05 04:29:56 +00002107 return true;
2108 }
2109 SourceLocation LParenLoc = ConsumeParen();
2110
2111 // Parse the optional expression-list.
Sebastian Redl511ed552008-11-25 22:21:31 +00002112 ExprVector ArgExprs(Actions);
Douglas Gregore8381c02008-11-05 04:29:56 +00002113 CommaLocsTy CommaLocs;
2114 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2115 SkipUntil(tok::r_paren);
2116 return true;
2117 }
2118
2119 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2120
Douglas Gregor44e7df62011-01-04 00:32:56 +00002121 SourceLocation EllipsisLoc;
2122 if (Tok.is(tok::ellipsis))
2123 EllipsisLoc = ConsumeToken();
2124
Douglas Gregor0be31a22010-07-02 17:43:08 +00002125 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002126 TemplateTypeTy, IdLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +00002127 LParenLoc, ArgExprs.take(),
Douglas Gregor44e7df62011-01-04 00:32:56 +00002128 ArgExprs.size(), RParenLoc,
2129 EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002130}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002131
Sebastian Redl965b0e32011-03-05 14:45:16 +00002132/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002133///
Douglas Gregor356513d2008-12-01 18:00:20 +00002134/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002135/// dynamic-exception-specification
2136/// noexcept-specification
2137///
2138/// noexcept-specification:
2139/// 'noexcept'
2140/// 'noexcept' '(' constant-expression ')'
2141ExceptionSpecificationType
2142Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange,
2143 llvm::SmallVectorImpl<ParsedType> &DynamicExceptions,
2144 llvm::SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
2145 ExprResult &NoexceptExpr) {
2146 ExceptionSpecificationType Result = EST_None;
2147
2148 // See if there's a dynamic specification.
2149 if (Tok.is(tok::kw_throw)) {
2150 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2151 DynamicExceptions,
2152 DynamicExceptionRanges);
2153 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2154 "Produced different number of exception types and ranges.");
2155 }
2156
2157 // If there's no noexcept specification, we're done.
2158 if (Tok.isNot(tok::kw_noexcept))
2159 return Result;
2160
2161 // If we already had a dynamic specification, parse the noexcept for,
2162 // recovery, but emit a diagnostic and don't store the results.
2163 SourceRange NoexceptRange;
2164 ExceptionSpecificationType NoexceptType = EST_None;
2165
2166 SourceLocation KeywordLoc = ConsumeToken();
2167 if (Tok.is(tok::l_paren)) {
2168 // There is an argument.
2169 SourceLocation LParenLoc = ConsumeParen();
2170 NoexceptType = EST_ComputedNoexcept;
2171 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002172 // The argument must be contextually convertible to bool. We use
2173 // ActOnBooleanCondition for this purpose.
2174 if (!NoexceptExpr.isInvalid())
2175 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2176 NoexceptExpr.get());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002177 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2178 NoexceptRange = SourceRange(KeywordLoc, RParenLoc);
2179 } else {
2180 // There is no argument.
2181 NoexceptType = EST_BasicNoexcept;
2182 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2183 }
2184
2185 if (Result == EST_None) {
2186 SpecificationRange = NoexceptRange;
2187 Result = NoexceptType;
2188
2189 // If there's a dynamic specification after a noexcept specification,
2190 // parse that and ignore the results.
2191 if (Tok.is(tok::kw_throw)) {
2192 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2193 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2194 DynamicExceptionRanges);
2195 }
2196 } else {
2197 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2198 }
2199
2200 return Result;
2201}
2202
2203/// ParseDynamicExceptionSpecification - Parse a C++
2204/// dynamic-exception-specification (C++ [except.spec]).
2205///
2206/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00002207/// 'throw' '(' type-id-list [opt] ')'
2208/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00002209///
Douglas Gregor356513d2008-12-01 18:00:20 +00002210/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00002211/// type-id ... [opt]
2212/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002213///
Sebastian Redl965b0e32011-03-05 14:45:16 +00002214ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2215 SourceRange &SpecificationRange,
2216 llvm::SmallVectorImpl<ParsedType> &Exceptions,
2217 llvm::SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002218 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00002219
Sebastian Redl965b0e32011-03-05 14:45:16 +00002220 SpecificationRange.setBegin(ConsumeToken());
Mike Stump11289f42009-09-09 15:08:12 +00002221
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002222 if (!Tok.is(tok::l_paren)) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002223 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2224 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002225 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002226 }
2227 SourceLocation LParenLoc = ConsumeParen();
2228
Douglas Gregor356513d2008-12-01 18:00:20 +00002229 // Parse throw(...), a Microsoft extension that means "this function
2230 // can throw anything".
2231 if (Tok.is(tok::ellipsis)) {
2232 SourceLocation EllipsisLoc = ConsumeToken();
2233 if (!getLang().Microsoft)
2234 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl965b0e32011-03-05 14:45:16 +00002235 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2236 SpecificationRange.setEnd(RParenLoc);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002237 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00002238 }
2239
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002240 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00002241 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002242 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00002243 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00002244
Douglas Gregor830837d2010-12-20 23:57:46 +00002245 if (Tok.is(tok::ellipsis)) {
2246 // C++0x [temp.variadic]p5:
2247 // - In a dynamic-exception-specification (15.4); the pattern is a
2248 // type-id.
2249 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002250 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00002251 if (!Res.isInvalid())
2252 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2253 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00002254
Sebastian Redld6434562009-05-29 18:02:33 +00002255 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002256 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00002257 Ranges.push_back(Range);
2258 }
Douglas Gregor830837d2010-12-20 23:57:46 +00002259
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002260 if (Tok.is(tok::comma))
2261 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002262 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002263 break;
2264 }
2265
Sebastian Redl965b0e32011-03-05 14:45:16 +00002266 SpecificationRange.setEnd(MatchRHSPunctuation(tok::r_paren, LParenLoc));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002267 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002268}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002269
Douglas Gregor7fb25412010-10-01 18:44:50 +00002270/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2271/// function declaration.
2272TypeResult Parser::ParseTrailingReturnType() {
2273 assert(Tok.is(tok::arrow) && "expected arrow");
2274
2275 ConsumeToken();
2276
2277 // FIXME: Need to suppress declarations when parsing this typename.
2278 // Otherwise in this function definition:
2279 //
2280 // auto f() -> struct X {}
2281 //
2282 // struct X is parsed as class definition because of the trailing
2283 // brace.
2284
2285 SourceRange Range;
2286 return ParseTypeName(&Range);
2287}
2288
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002289/// \brief We have just started parsing the definition of a new class,
2290/// so push that class onto our stack of classes that is currently
2291/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00002292Sema::ParsingClassState
2293Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002294 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002295 "Nested class without outer class");
Douglas Gregoredf8f392010-01-16 20:52:59 +00002296 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCallc1465822011-02-14 07:13:47 +00002297 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002298}
2299
2300/// \brief Deallocate the given parsed class and all of its nested
2301/// classes.
2302void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00002303 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2304 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002305 delete Class;
2306}
2307
2308/// \brief Pop the top class of the stack of classes that are
2309/// currently being parsed.
2310///
2311/// This routine should be called when we have finished parsing the
2312/// definition of a class, but have not yet popped the Scope
2313/// associated with the class's definition.
2314///
2315/// \returns true if the class we've popped is a top-level class,
2316/// false otherwise.
John McCallc1465822011-02-14 07:13:47 +00002317void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002318 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00002319
John McCallc1465822011-02-14 07:13:47 +00002320 Actions.PopParsingClass(state);
2321
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002322 ParsingClass *Victim = ClassStack.top();
2323 ClassStack.pop();
2324 if (Victim->TopLevelClass) {
2325 // Deallocate all of the nested classes of this class,
2326 // recursively: we don't need to keep any of this information.
2327 DeallocateParsedClasses(Victim);
2328 return;
Mike Stump11289f42009-09-09 15:08:12 +00002329 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002330 assert(!ClassStack.empty() && "Missing top-level class?");
2331
Douglas Gregorefc46952010-10-12 16:25:54 +00002332 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002333 // The victim is a nested class, but we will not need to perform
2334 // any processing after the definition of this class since it has
2335 // no members whose handling was delayed. Therefore, we can just
2336 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00002337 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002338 return;
2339 }
2340
2341 // This nested class has some members that will need to be processed
2342 // after the top-level class is completely defined. Therefore, add
2343 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002344 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00002345 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00002346 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002347}
Alexis Hunt96d5c762009-11-21 08:43:09 +00002348
2349/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
2350/// parses standard attributes.
2351///
2352/// [C++0x] attribute-specifier:
2353/// '[' '[' attribute-list ']' ']'
2354///
2355/// [C++0x] attribute-list:
2356/// attribute[opt]
2357/// attribute-list ',' attribute[opt]
2358///
2359/// [C++0x] attribute:
2360/// attribute-token attribute-argument-clause[opt]
2361///
2362/// [C++0x] attribute-token:
2363/// identifier
2364/// attribute-scoped-token
2365///
2366/// [C++0x] attribute-scoped-token:
2367/// attribute-namespace '::' identifier
2368///
2369/// [C++0x] attribute-namespace:
2370/// identifier
2371///
2372/// [C++0x] attribute-argument-clause:
2373/// '(' balanced-token-seq ')'
2374///
2375/// [C++0x] balanced-token-seq:
2376/// balanced-token
2377/// balanced-token-seq balanced-token
2378///
2379/// [C++0x] balanced-token:
2380/// '(' balanced-token-seq ')'
2381/// '[' balanced-token-seq ']'
2382/// '{' balanced-token-seq '}'
2383/// any token but '(', ')', '[', ']', '{', or '}'
John McCall53fa7142010-12-24 02:08:15 +00002384void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2385 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002386 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2387 && "Not a C++0x attribute list");
2388
2389 SourceLocation StartLoc = Tok.getLocation(), Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002390
2391 ConsumeBracket();
2392 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002393
Alexis Hunt96d5c762009-11-21 08:43:09 +00002394 if (Tok.is(tok::comma)) {
2395 Diag(Tok.getLocation(), diag::err_expected_ident);
2396 ConsumeToken();
2397 }
2398
2399 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2400 // attribute not present
2401 if (Tok.is(tok::comma)) {
2402 ConsumeToken();
2403 continue;
2404 }
2405
2406 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2407 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002408
Alexis Hunt96d5c762009-11-21 08:43:09 +00002409 // scoped attribute
2410 if (Tok.is(tok::coloncolon)) {
2411 ConsumeToken();
2412
2413 if (!Tok.is(tok::identifier)) {
2414 Diag(Tok.getLocation(), diag::err_expected_ident);
2415 SkipUntil(tok::r_square, tok::comma, true, true);
2416 continue;
2417 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002418
Alexis Hunt96d5c762009-11-21 08:43:09 +00002419 ScopeName = AttrName;
2420 ScopeLoc = AttrLoc;
2421
2422 AttrName = Tok.getIdentifierInfo();
2423 AttrLoc = ConsumeToken();
2424 }
2425
2426 bool AttrParsed = false;
2427 // No scoped names are supported; ideally we could put all non-standard
2428 // attributes into namespaces.
2429 if (!ScopeName) {
2430 switch(AttributeList::getKind(AttrName))
2431 {
2432 // No arguments
Alexis Hunt54a02542009-11-25 04:20:27 +00002433 case AttributeList::AT_carries_dependency:
Anders Carlssone30621b2011-01-23 21:33:18 +00002434 case AttributeList::AT_noreturn: {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002435 if (Tok.is(tok::l_paren)) {
2436 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2437 << AttrName->getName();
2438 break;
2439 }
2440
John McCall084e83d2011-03-24 11:26:52 +00002441 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0,
2442 SourceLocation(), 0, 0, false, true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002443 AttrParsed = true;
2444 break;
2445 }
2446
2447 // One argument; must be a type-id or assignment-expression
2448 case AttributeList::AT_aligned: {
2449 if (Tok.isNot(tok::l_paren)) {
2450 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2451 << AttrName->getName();
2452 break;
2453 }
2454 SourceLocation ParamLoc = ConsumeParen();
2455
John McCalldadc5752010-08-24 06:29:42 +00002456 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002457
2458 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2459
2460 ExprVector ArgExprs(Actions);
2461 ArgExprs.push_back(ArgExpr.release());
John McCall084e83d2011-03-24 11:26:52 +00002462 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc,
2463 0, ParamLoc, ArgExprs.take(), 1,
2464 false, true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002465
2466 AttrParsed = true;
2467 break;
2468 }
2469
2470 // Silence warnings
2471 default: break;
2472 }
2473 }
2474
2475 // Skip the entire parameter clause, if any
2476 if (!AttrParsed && Tok.is(tok::l_paren)) {
2477 ConsumeParen();
2478 // SkipUntil maintains the balancedness of tokens.
2479 SkipUntil(tok::r_paren, false);
2480 }
2481 }
2482
2483 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2484 SkipUntil(tok::r_square, false);
2485 Loc = Tok.getLocation();
2486 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2487 SkipUntil(tok::r_square, false);
2488
John McCall53fa7142010-12-24 02:08:15 +00002489 attrs.Range = SourceRange(StartLoc, Loc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002490}
2491
2492/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2493/// attribute.
2494///
2495/// FIXME: Simply returns an alignof() expression if the argument is a
2496/// type. Ideally, the type should be propagated directly into Sema.
2497///
2498/// [C++0x] 'align' '(' type-id ')'
2499/// [C++0x] 'align' '(' assignment-expression ')'
John McCalldadc5752010-08-24 06:29:42 +00002500ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002501 if (isTypeIdInParens()) {
John McCallfaf5fb42010-08-26 23:41:50 +00002502 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002503 SourceLocation TypeLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002504 ParsedType Ty = ParseTypeName().get();
Alexis Hunt96d5c762009-11-21 08:43:09 +00002505 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbournee190dee2011-03-11 19:24:49 +00002506 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2507 Ty.getAsOpaquePtr(), TypeRange);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002508 } else
2509 return ParseConstantExpression();
2510}
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002511
2512/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2513///
2514/// [MS] ms-attribute:
2515/// '[' token-seq ']'
2516///
2517/// [MS] ms-attribute-seq:
2518/// ms-attribute[opt]
2519/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00002520void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2521 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002522 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2523
2524 while (Tok.is(tok::l_square)) {
2525 ConsumeBracket();
2526 SkipUntil(tok::r_square, true, true);
John McCall53fa7142010-12-24 02:08:15 +00002527 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002528 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2529 }
2530}