blob: 1bc5815f21e657e26c784ac1aeba999065c4c9e9 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000022using namespace clang;
23
24/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000025/// may either be a top level namespace or a block-level namespace alias. If
26/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000027///
28/// namespace-definition: [C++ 7.3: basic.namespace]
29/// named-namespace-definition
30/// unnamed-namespace-definition
31///
32/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000033/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000034///
35/// named-namespace-definition:
36/// original-namespace-definition
37/// extension-namespace-definition
38///
39/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' identifier attributes[opt]
41/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000042///
43/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' original-namespace-name
45/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000046///
Chris Lattner8f08cb72007-08-25 06:57:03 +000047/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
48/// 'namespace' identifier '=' qualified-namespace-specifier ';'
49///
John McCalld226f652010-08-21 09:40:31 +000050Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000051 SourceLocation &DeclEnd,
52 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000053 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000054 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000055
Douglas Gregor49f40bd2009-09-18 19:03:04 +000056 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000057 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +000058 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +000059 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000060
Chris Lattner8f08cb72007-08-25 06:57:03 +000061 SourceLocation IdentLoc;
62 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000063
64 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner04d66662007-10-09 17:33:22 +000066 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 Ident = Tok.getIdentifierInfo();
68 IdentLoc = ConsumeToken(); // eat the identifier.
69 }
Mike Stump1eb44332009-09-09 15:08:12 +000070
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // Read label attributes, if present.
Ted Kremenek8113ecf2010-11-10 05:59:39 +000072 AttributeList *AttrList = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
75
Chris Lattner8f08cb72007-08-25 06:57:03 +000076 // FIXME: save these somewhere.
Ted Kremenek8113ecf2010-11-10 05:59:39 +000077 AttrList = ParseGNUAttributes();
Douglas Gregor6a588dd2009-06-17 19:49:00 +000078 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 if (Tok.is(tok::equal)) {
81 if (AttrList)
82 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000083 if (InlineLoc.isValid())
84 Diag(InlineLoc, diag::err_inline_namespace_alias)
85 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086
Chris Lattner97144fc2009-04-02 04:16:50 +000087 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000088 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner51448322009-03-29 14:02:43 +000090 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000091 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000092 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +000093 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +000094 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Chris Lattner51448322009-03-29 14:02:43 +000096 SourceLocation LBrace = ConsumeBrace();
97
Douglas Gregor23c94db2010-07-02 17:43:08 +000098 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
99 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
100 getCurScope()->getFnParent()) {
Douglas Gregor95f1b152010-05-14 05:08:22 +0000101 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
102 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000103 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000104 }
105
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000106 // If we're still good, complain about inline namespaces in non-C++0x now.
107 if (!getLang().CPlusPlus0x && InlineLoc.isValid())
108 Diag(InlineLoc, diag::ext_inline_namespace);
109
Chris Lattner51448322009-03-29 14:02:43 +0000110 // Enter a scope for the namespace.
111 ParseScope NamespaceScope(this, Scope::DeclScope);
112
John McCalld226f652010-08-21 09:40:31 +0000113 Decl *NamespcDecl =
Sebastian Redld078e642010-08-27 23:12:46 +0000114 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, IdentLoc, Ident,
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000115 LBrace, AttrList);
Chris Lattner51448322009-03-29 14:02:43 +0000116
John McCallf312b1e2010-08-26 23:41:50 +0000117 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
118 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Sean Huntbbd37c62009-11-21 08:43:09 +0000120 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
121 CXX0XAttributeList Attr;
122 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
123 Attr = ParseCXX0XAttributes();
Francois Pichet334d47e2010-10-11 12:59:39 +0000124 if (getLang().Microsoft && Tok.is(tok::l_square))
125 ParseMicrosoftAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +0000126 ParseExternalDeclaration(Attr);
127 }
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Chris Lattner51448322009-03-29 14:02:43 +0000129 // Leave the namespace scope.
130 NamespaceScope.Exit();
131
Chris Lattner97144fc2009-04-02 04:16:50 +0000132 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
133 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000134
Chris Lattner97144fc2009-04-02 04:16:50 +0000135 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000136 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000137}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000138
Anders Carlssonf67606a2009-03-28 04:07:16 +0000139/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
140/// alias definition.
141///
John McCalld226f652010-08-21 09:40:31 +0000142Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000143 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000144 IdentifierInfo *Alias,
145 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000146 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anders Carlssonf67606a2009-03-28 04:07:16 +0000148 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000150 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000151 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000152 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000153 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000154
Anders Carlssonf67606a2009-03-28 04:07:16 +0000155 CXXScopeSpec SS;
156 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000157 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000158
159 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
160 Diag(Tok, diag::err_expected_namespace_name);
161 // Skip to end of the definition and eat the ';'.
162 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000163 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000164 }
165
166 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000167 IdentifierInfo *Ident = Tok.getIdentifierInfo();
168 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Anders Carlssonf67606a2009-03-28 04:07:16 +0000170 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000171 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000172 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
173 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Douglas Gregor23c94db2010-07-02 17:43:08 +0000175 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000176 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000177}
178
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000179/// ParseLinkage - We know that the current token is a string_literal
180/// and just before that, that extern was seen.
181///
182/// linkage-specification: [C++ 7.5p2: dcl.link]
183/// 'extern' string-literal '{' declaration-seq[opt] '}'
184/// 'extern' string-literal declaration
185///
Chris Lattner7d642712010-11-09 20:15:55 +0000186Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000187 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000188 llvm::SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000189 bool Invalid = false;
190 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
191 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000192 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000193
194 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000195
Douglas Gregor074149e2009-01-05 19:45:36 +0000196 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000197 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000198 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Douglas Gregor074149e2009-01-05 19:45:36 +0000199 /*FIXME: */SourceLocation(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000200 Loc, Lang,
Mike Stump1eb44332009-09-09 15:08:12 +0000201 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000202 : SourceLocation());
203
Sean Huntbbd37c62009-11-21 08:43:09 +0000204 CXX0XAttributeList Attr;
Chris Lattner7d642712010-11-09 20:15:55 +0000205 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +0000206 Attr = ParseCXX0XAttributes();
Chris Lattner7d642712010-11-09 20:15:55 +0000207
Francois Pichet334d47e2010-10-11 12:59:39 +0000208 if (getLang().Microsoft && Tok.is(tok::l_square))
209 ParseMicrosoftAttributes();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000210
Douglas Gregor074149e2009-01-05 19:45:36 +0000211 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000212 DS.setExternInLinkageSpec(true);
Douglas Gregor09a63c92010-08-24 14:14:45 +0000213 ParseExternalDeclaration(Attr, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000214 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000215 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000216 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000217
Douglas Gregor63a01132010-02-07 08:38:28 +0000218 DS.abort();
219
Sean Huntbbd37c62009-11-21 08:43:09 +0000220 if (Attr.HasAttr)
221 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
222 << Attr.Range;
223
Douglas Gregorf44515a2008-12-16 22:23:02 +0000224 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000225 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000226 CXX0XAttributeList Attr;
227 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
228 Attr = ParseCXX0XAttributes();
Francois Pichet334d47e2010-10-11 12:59:39 +0000229 if (getLang().Microsoft && Tok.is(tok::l_square))
230 ParseMicrosoftAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +0000231 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000232 }
233
Douglas Gregorf44515a2008-12-16 22:23:02 +0000234 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Chris Lattner7d642712010-11-09 20:15:55 +0000235 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
236 RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000237}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000238
Douglas Gregorf780abc2008-12-30 03:27:21 +0000239/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
240/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000241Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000242 const ParsedTemplateInfo &TemplateInfo,
243 SourceLocation &DeclEnd,
244 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000245 assert(Tok.is(tok::kw_using) && "Not using token");
246
247 // Eat 'using'.
248 SourceLocation UsingLoc = ConsumeToken();
249
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000250 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000251 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000252 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000253 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000254
John McCall78b81052010-11-10 02:40:36 +0000255 // 'using namespace' means this is a using-directive.
256 if (Tok.is(tok::kw_namespace)) {
257 // Template parameters are always an error here.
258 if (TemplateInfo.Kind) {
259 SourceRange R = TemplateInfo.getSourceRange();
260 Diag(UsingLoc, diag::err_templated_using_directive)
261 << R << FixItHint::CreateRemoval(R);
262 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000263
John McCall78b81052010-11-10 02:40:36 +0000264 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
265 }
266
267 // Otherwise, it must be a using-declaration.
268
269 // Using declarations can't have attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +0000270 if (Attr.HasAttr)
271 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
272 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000273
John McCall78b81052010-11-10 02:40:36 +0000274 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000275}
276
277/// ParseUsingDirective - Parse C++ using-directive, assumes
278/// that current token is 'namespace' and 'using' was already parsed.
279///
280/// using-directive: [C++ 7.3.p4: namespace.udir]
281/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
282/// namespace-name ;
283/// [GNU] using-directive:
284/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
285/// namespace-name attributes[opt] ;
286///
John McCalld226f652010-08-21 09:40:31 +0000287Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000288 SourceLocation UsingLoc,
289 SourceLocation &DeclEnd,
290 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000291 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
292
293 // Eat 'namespace'.
294 SourceLocation NamespcLoc = ConsumeToken();
295
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000296 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000297 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000298 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000299 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000300
Douglas Gregorf780abc2008-12-30 03:27:21 +0000301 CXXScopeSpec SS;
302 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000303 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000304
Douglas Gregorf780abc2008-12-30 03:27:21 +0000305 IdentifierInfo *NamespcName = 0;
306 SourceLocation IdentLoc = SourceLocation();
307
308 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000309 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000310 Diag(Tok, diag::err_expected_namespace_name);
311 // If there was invalid namespace name, skip to end of decl, and eat ';'.
312 SkipUntil(tok::semi);
313 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000314 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Chris Lattner823c44e2009-01-06 07:27:21 +0000317 // Parse identifier.
318 NamespcName = Tok.getIdentifierInfo();
319 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Chris Lattner823c44e2009-01-06 07:27:21 +0000321 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000322 bool GNUAttr = false;
323 if (Tok.is(tok::kw___attribute)) {
324 GNUAttr = true;
325 Attr = addAttributeLists(Attr, ParseGNUAttributes());
326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattner823c44e2009-01-06 07:27:21 +0000328 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000329 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000330 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000331 GNUAttr ? diag::err_expected_semi_after_attribute_list
332 : diag::err_expected_semi_after_namespace_name,
333 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000334
Douglas Gregor23c94db2010-07-02 17:43:08 +0000335 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000336 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000337}
338
339/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
340/// 'using' was already seen.
341///
342/// using-declaration: [C++ 7.3.p3: namespace.udecl]
343/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000344/// unqualified-id
345/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000346///
John McCalld226f652010-08-21 09:40:31 +0000347Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000348 const ParsedTemplateInfo &TemplateInfo,
349 SourceLocation UsingLoc,
350 SourceLocation &DeclEnd,
351 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000352 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000353 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000354 bool IsTypeName;
355
John McCall78b81052010-11-10 02:40:36 +0000356 // TODO: in C++0x, if we have template parameters this must be a
357 // template alias:
358 // template <...> using id = type;
359
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000360 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000361 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000362 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000363 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000364 ConsumeToken();
365 IsTypeName = true;
366 }
367 else
368 IsTypeName = false;
369
370 // Parse nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000371 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000372
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000373 // Check nested-name specifier.
374 if (SS.isInvalid()) {
375 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000376 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000377 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000378
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000379 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000380 // destructor names and allow the action module to diagnose any semantic
381 // errors.
382 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000383 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000384 /*EnteringContext=*/false,
385 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000386 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000387 ParsedType(),
Douglas Gregor12c118a2009-11-04 16:30:06 +0000388 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000389 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000390 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000391 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000392
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000393 // Parse (optional) attributes (most likely GNU strong-using extension).
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000394 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000395 if (Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000396 AttrList = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000398 // Eat ';'.
399 DeclEnd = Tok.getLocation();
400 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000401 AttrList ? "attributes list" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000402 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000403
John McCall78b81052010-11-10 02:40:36 +0000404 // Diagnose an attempt to declare a templated using-declaration.
405 if (TemplateInfo.Kind) {
406 SourceRange R = TemplateInfo.getSourceRange();
407 Diag(UsingLoc, diag::err_templated_using_declaration)
408 << R << FixItHint::CreateRemoval(R);
409
410 // Unfortunately, we have to bail out instead of recovering by
411 // ignoring the parameters, just in case the nested name specifier
412 // depends on the parameters.
413 return 0;
414 }
415
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000416 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
417 Name, AttrList, IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000418}
419
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000420/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
421///
422/// static_assert-declaration:
423/// static_assert ( constant-expression , string-literal ) ;
424///
John McCalld226f652010-08-21 09:40:31 +0000425Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000426 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
427 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000429 if (Tok.isNot(tok::l_paren)) {
430 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000431 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000434 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000435
John McCall60d7b3a2010-08-24 06:29:42 +0000436 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000437 if (AssertExpr.isInvalid()) {
438 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000439 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anders Carlssonad5f9602009-03-13 23:29:20 +0000442 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000443 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000444
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000445 if (Tok.isNot(tok::string_literal)) {
446 Diag(Tok, diag::err_expected_string_literal);
447 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000448 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
John McCall60d7b3a2010-08-24 06:29:42 +0000451 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000452 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000453 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000454
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000455 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner97144fc2009-04-02 04:16:50 +0000457 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000458 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000459
John McCall9ae2f072010-08-23 23:25:46 +0000460 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
461 AssertExpr.take(),
462 AssertMessage.take());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000463}
464
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000465/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
466///
467/// 'decltype' ( expression )
468///
469void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
470 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
471
472 SourceLocation StartLoc = ConsumeToken();
473 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000474
475 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000476 "decltype")) {
477 SkipUntil(tok::r_paren);
478 return;
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000481 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000483 // C++0x [dcl.type.simple]p4:
484 // The operand of the decltype specifier is an unevaluated operand.
485 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000486 Sema::Unevaluated);
John McCall60d7b3a2010-08-24 06:29:42 +0000487 ExprResult Result = ParseExpression();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000488 if (Result.isInvalid()) {
489 SkipUntil(tok::r_paren);
490 return;
491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000493 // Match the ')'
494 SourceLocation RParenLoc;
495 if (Tok.is(tok::r_paren))
496 RParenLoc = ConsumeParen();
497 else
498 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000500 if (RParenLoc.isInvalid())
501 return;
502
503 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000504 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000505 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000506 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000507 DiagID, Result.release()))
508 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000509}
510
Douglas Gregor42a552f2008-11-05 20:51:48 +0000511/// ParseClassName - Parse a C++ class-name, which names a class. Note
512/// that we only check that the result names a type; semantic analysis
513/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000514/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000515/// found.
516///
517/// class-name: [C++ 9.1]
518/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000519/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000520///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000521Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000522 CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000523 // Check whether we have a template-id that names a type.
524 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000525 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000526 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000527 if (TemplateId->Kind == TNK_Type_template ||
528 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000529 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000530
531 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000532 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000533 EndLocation = Tok.getAnnotationEndLoc();
534 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000535
536 if (Type)
537 return Type;
538 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000539 }
540
541 // Fall through to produce an error below.
542 }
543
Douglas Gregor42a552f2008-11-05 20:51:48 +0000544 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000545 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000546 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000547 }
548
Douglas Gregor84d0a192010-01-12 21:28:44 +0000549 IdentifierInfo *Id = Tok.getIdentifierInfo();
550 SourceLocation IdLoc = ConsumeToken();
551
552 if (Tok.is(tok::less)) {
553 // It looks the user intended to write a template-id here, but the
554 // template-name was wrong. Try to fix that.
555 TemplateNameKind TNK = TNK_Type_template;
556 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000557 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor84d0a192010-01-12 21:28:44 +0000558 SS, Template, TNK)) {
559 Diag(IdLoc, diag::err_unknown_template_name)
560 << Id;
561 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000562
Douglas Gregor84d0a192010-01-12 21:28:44 +0000563 if (!Template)
564 return true;
565
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000566 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000567 UnqualifiedId TemplateName;
568 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000569
Douglas Gregor84d0a192010-01-12 21:28:44 +0000570 // Parse the full template-id, then turn it into a type.
571 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
572 SourceLocation(), true))
573 return true;
574 if (TNK == TNK_Dependent_template_name)
575 AnnotateTemplateIdTokenAsType(SS);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000576
Douglas Gregor84d0a192010-01-12 21:28:44 +0000577 // If we didn't end up with a typename token, there's nothing more we
578 // can do.
579 if (Tok.isNot(tok::annot_typename))
580 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000581
Douglas Gregor84d0a192010-01-12 21:28:44 +0000582 // Retrieve the type from the annotation token, consume that token, and
583 // return.
584 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000585 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000586 ConsumeToken();
587 return Type;
588 }
589
Douglas Gregor42a552f2008-11-05 20:51:48 +0000590 // We have an identifier; check whether it is actually a type.
John McCallb3d87482010-08-24 05:47:05 +0000591 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000592 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000593 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000594 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000595 }
596
597 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000598 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000599
600 // Fake up a Declarator to use with ActOnTypeName.
601 DeclSpec DS;
602 DS.SetRangeStart(IdLoc);
603 DS.SetRangeEnd(EndLocation);
604 DS.getTypeSpecScope() = *SS;
605
606 const char *PrevSpec = 0;
607 unsigned DiagID;
608 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
609
610 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
611 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000612}
613
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000614/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
615/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
616/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000617/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000618///
619/// class-specifier: [C++ class]
620/// class-head '{' member-specification[opt] '}'
621/// class-head '{' member-specification[opt] '}' attributes[opt]
622/// class-head:
623/// class-key identifier[opt] base-clause[opt]
624/// class-key nested-name-specifier identifier base-clause[opt]
625/// class-key nested-name-specifier[opt] simple-template-id
626/// base-clause[opt]
627/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000628/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000629/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000630/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000631/// simple-template-id base-clause[opt]
632/// class-key:
633/// 'class'
634/// 'struct'
635/// 'union'
636///
637/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000638/// class-key ::[opt] nested-name-specifier[opt] identifier
639/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
640/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000641///
642/// Note that the C++ class-specifier and elaborated-type-specifier,
643/// together, subsume the C99 struct-or-union-specifier:
644///
645/// struct-or-union-specifier: [C99 6.7.2.1]
646/// struct-or-union identifier[opt] '{' struct-contents '}'
647/// struct-or-union identifier
648/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
649/// '}' attributes[opt]
650/// [GNU] struct-or-union attributes[opt] identifier
651/// struct-or-union:
652/// 'struct'
653/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000654void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
655 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000656 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000657 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000658 DeclSpec::TST TagType;
659 if (TagTokKind == tok::kw_struct)
660 TagType = DeclSpec::TST_struct;
661 else if (TagTokKind == tok::kw_class)
662 TagType = DeclSpec::TST_class;
663 else {
664 assert(TagTokKind == tok::kw_union && "Not a class specifier");
665 TagType = DeclSpec::TST_union;
666 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000667
Douglas Gregor374929f2009-09-18 15:37:17 +0000668 if (Tok.is(tok::code_completion)) {
669 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000670 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000671 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000672 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000673
Chandler Carruth926c4b42010-06-28 08:39:25 +0000674 // C++03 [temp.explicit] 14.7.2/8:
675 // The usual access checking rules do not apply to names used to specify
676 // explicit instantiations.
677 //
678 // As an extension we do not perform access checking on the names used to
679 // specify explicit specializations either. This is important to allow
680 // specializing traits classes for private types.
681 bool SuppressingAccessChecks = false;
682 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
683 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
684 Actions.ActOnStartSuppressingAccessChecks();
685 SuppressingAccessChecks = true;
686 }
687
Sean Huntbbd37c62009-11-21 08:43:09 +0000688 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000689 // If attributes exist after tag, parse them.
690 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000691 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000692
Steve Narofff59e17e2008-12-24 20:59:21 +0000693 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000694 while (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000695 AttrList = ParseMicrosoftDeclSpec(AttrList);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000696
Sean Huntbbd37c62009-11-21 08:43:09 +0000697 // If C++0x attributes exist here, parse them.
698 // FIXME: Are we consistent with the ordering of parsing of different
699 // styles of attributes?
700 if (isCXX0XAttributeSpecifier())
701 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Douglas Gregorb117a602009-09-04 05:53:02 +0000703 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
704 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
705 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000706 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000707 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
708 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000709 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000710 Tok.setKind(tok::identifier);
711 }
712
713 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
714 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
715 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000716 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000717 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
718 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000719 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000720 Tok.setKind(tok::identifier);
721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000723 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000724 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000725 if (getLang().CPlusPlus) {
726 // "FOO : BAR" is not a potential typo for "FOO::BAR".
727 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000728
John McCallb3d87482010-08-24 05:47:05 +0000729 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall207014e2010-07-30 06:26:29 +0000730 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000731 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000732 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
733 Diag(Tok, diag::err_expected_ident);
734 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000735
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000736 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
737
Douglas Gregorcc636682009-02-17 23:15:12 +0000738 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000739 IdentifierInfo *Name = 0;
740 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000741 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000742 if (Tok.is(tok::identifier)) {
743 Name = Tok.getIdentifierInfo();
744 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000745
Douglas Gregor5ee37342010-05-30 22:30:21 +0000746 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000747 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000748 // Eat the template argument list and try to continue parsing this as
749 // a class (or template thereof).
750 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000751 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000752 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000753 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000754 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000755 // We couldn't parse the template argument list at all, so don't
756 // try to give any location information for the list.
757 LAngleLoc = RAngleLoc = SourceLocation();
758 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000759
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000760 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000761 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000762 << (TagType == DeclSpec::TST_class? 0
763 : TagType == DeclSpec::TST_struct? 1
764 : 2)
765 << Name
766 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000767
768 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000769 // we've removed its template argument list.
770 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
771 if (TemplateParams && TemplateParams->size() > 1) {
772 TemplateParams->pop_back();
773 } else {
774 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000775 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000776 = ParsedTemplateInfo::NonTemplate;
777 }
778 } else if (TemplateInfo.Kind
779 == ParsedTemplateInfo::ExplicitInstantiation) {
780 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000781 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000782 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000783 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000784 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000785 = SourceLocation();
786 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
787 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000788 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000789 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000790 } else if (Tok.is(tok::annot_template_id)) {
791 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
792 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000793
Douglas Gregorc45c2322009-03-31 00:43:58 +0000794 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000795 // The template-name in the simple-template-id refers to
796 // something other than a class template. Give an appropriate
797 // error message and skip to the ';'.
798 SourceRange Range(NameLoc);
799 if (SS.isNotEmpty())
800 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000801
Douglas Gregor39a8de12009-02-25 19:37:18 +0000802 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
803 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Douglas Gregor39a8de12009-02-25 19:37:18 +0000805 DS.SetTypeSpecError();
806 SkipUntil(tok::semi, false, true);
807 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000808 if (SuppressingAccessChecks)
809 Actions.ActOnStopSuppressingAccessChecks();
810
Douglas Gregor39a8de12009-02-25 19:37:18 +0000811 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000812 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000813 }
814
Chandler Carruth926c4b42010-06-28 08:39:25 +0000815 // As soon as we're finished parsing the class's template-id, turn access
816 // checking back on.
817 if (SuppressingAccessChecks)
818 Actions.ActOnStopSuppressingAccessChecks();
819
John McCall67d1a672009-08-06 02:15:43 +0000820 // There are four options here. If we have 'struct foo;', then this
821 // is either a forward declaration or a friend declaration, which
822 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000823 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000824 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000825 // However, in some contexts, things look like declarations but are just
826 // references, e.g.
827 // new struct s;
828 // or
829 // &T::operator struct s;
830 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +0000831 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000832 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +0000833 TUK = Sema::TUK_Reference;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000834 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000835 if (DS.isFriendSpecified()) {
836 // C++ [class.friend]p2:
837 // A class shall not be defined in a friend declaration.
838 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
839 << SourceRange(DS.getFriendSpecLoc());
840
841 // Skip everything up to the semicolon, so that this looks like a proper
842 // friend class (or template thereof) declaration.
843 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +0000844 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +0000845 } else {
846 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +0000847 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +0000848 }
849 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +0000850 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000851 else
John McCallf312b1e2010-08-26 23:41:50 +0000852 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000853
John McCall207014e2010-07-30 06:26:29 +0000854 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +0000855 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +0000856 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
857 // We have a declaration or reference to an anonymous class.
858 Diag(StartLoc, diag::err_anon_type_definition)
859 << DeclSpec::getSpecifierName(TagType);
860 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000861
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000862 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000863
864 if (TemplateId)
865 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000866 return;
867 }
868
Douglas Gregorddc29e12009-02-06 22:42:48 +0000869 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +0000870 DeclResult TagOrTempResult = true; // invalid
871 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000872
Douglas Gregor402abb52009-05-28 23:31:59 +0000873 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000874 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000875 // Explicit specialization, class template partial specialization,
876 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000877 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000878 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000879 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000880 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000881 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000882 // This is an explicit instantiation of a class template.
883 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000884 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000885 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000886 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000887 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000888 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000889 SS,
John McCall2b5289b2010-08-23 07:28:44 +0000890 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000891 TemplateId->TemplateNameLoc,
892 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000893 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000894 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000895 AttrList);
John McCall74256f52010-04-14 00:24:33 +0000896
897 // Friend template-ids are treated as references unless
898 // they have template headers, in which case they're ill-formed
899 // (FIXME: "template <class T> friend class A<T>::B<int>;").
900 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +0000901 } else if (TUK == Sema::TUK_Reference ||
902 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +0000903 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCallc4e70192009-09-11 04:59:25 +0000904 TypeResult
John McCall2b5289b2010-08-23 07:28:44 +0000905 = Actions.ActOnTemplateIdType(TemplateId->Template,
John McCall6b2becf2009-09-08 17:47:29 +0000906 TemplateId->TemplateNameLoc,
907 TemplateId->LAngleLoc,
908 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000909 TemplateId->RAngleLoc);
910
Craig Silverstein45ab4b52010-11-18 08:32:02 +0000911 TypeResult = Actions.ActOnTagTemplateIdType(SS, TypeResult, TUK,
John McCallc4e70192009-09-11 04:59:25 +0000912 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000913 } else {
914 // This is an explicit specialization or a class template
915 // partial specialization.
916 TemplateParameterLists FakedParamLists;
917
918 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
919 // This looks like an explicit instantiation, because we have
920 // something like
921 //
922 // template class Foo<X>
923 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000924 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000925 // meant to be an explicit specialization, but the user forgot
926 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +0000927 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000928
Mike Stump1eb44332009-09-09 15:08:12 +0000929 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000930 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000931 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000932 diag::err_explicit_instantiation_with_definition)
933 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000934 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000935
936 // Create a fake template parameter list that contains only
937 // "template<>", so that we treat this construct as a class
938 // template specialization.
939 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000940 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000941 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000942 LAngleLoc,
943 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000944 LAngleLoc));
945 TemplateParams = &FakedParamLists;
946 }
947
948 // Build the class template specialization.
949 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000950 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000951 StartLoc, SS,
John McCall2b5289b2010-08-23 07:28:44 +0000952 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000953 TemplateId->TemplateNameLoc,
954 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000955 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000956 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000957 AttrList,
John McCallf312b1e2010-08-26 23:41:50 +0000958 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000959 TemplateParams? &(*TemplateParams)[0] : 0,
960 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000961 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000962 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000963 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000964 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000965 // Explicit instantiation of a member of a class template
966 // specialization, e.g.,
967 //
968 // template struct Outer<int>::Inner;
969 //
970 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000971 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000972 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000973 TemplateInfo.TemplateLoc,
974 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000975 NameLoc, AttrList);
John McCall9a34edb2010-10-19 01:40:49 +0000976 } else if (TUK == Sema::TUK_Friend &&
977 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
978 TagOrTempResult =
979 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
980 TagType, StartLoc, SS,
981 Name, NameLoc, AttrList,
982 MultiTemplateParamsArg(Actions,
983 TemplateParams? &(*TemplateParams)[0] : 0,
984 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000985 } else {
986 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000987 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000988 // FIXME: Diagnose this particular error.
989 }
990
John McCallc4e70192009-09-11 04:59:25 +0000991 bool IsDependent = false;
992
John McCalla25c4082010-10-19 18:40:57 +0000993 // Don't pass down template parameter lists if this is just a tag
994 // reference. For example, we don't need the template parameters here:
995 // template <class T> class A *makeA(T t);
996 MultiTemplateParamsArg TParams;
997 if (TUK != Sema::TUK_Reference && TemplateParams)
998 TParams =
999 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1000
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001001 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001002 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
1003 SS, Name, NameLoc, AttrList, AS,
John McCalla25c4082010-10-19 18:40:57 +00001004 TParams, Owned, IsDependent, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001005 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001006
1007 // If ActOnTag said the type was dependent, try again with the
1008 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001009 if (IsDependent) {
1010 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001011 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001012 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001013 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001014 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001015
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001016 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001017 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001018 assert(Tok.is(tok::l_brace) ||
1019 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001020 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001021 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001022 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001023 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001024 }
1025
John McCallb3d87482010-08-24 05:47:05 +00001026 // FIXME: The DeclSpec should keep the locations of both the keyword and the
1027 // name (if there is one).
1028 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1029
1030 const char *PrevSpec = 0;
1031 unsigned DiagID;
1032 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001033 if (!TypeResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +00001034 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc,
1035 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001036 } else if (!TagOrTempResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +00001037 Result = DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
1038 TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001039 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001040 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001041 return;
1042 }
Mike Stump1eb44332009-09-09 15:08:12 +00001043
John McCallb3d87482010-08-24 05:47:05 +00001044 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001045 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001046
Chris Lattner4ed5d912010-02-02 01:23:29 +00001047 // At this point, we've successfully parsed a class-specifier in 'definition'
1048 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1049 // going to look at what comes after it to improve error recovery. If an
1050 // impossible token occurs next, we assume that the programmer forgot a ; at
1051 // the end of the declaration and recover that way.
1052 //
1053 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001054 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001055 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001056 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001057 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001058 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001059 case tok::star: // struct foo {...} * P;
1060 case tok::amp: // struct foo {...} & R = ...
1061 case tok::identifier: // struct foo {...} V ;
1062 case tok::r_paren: //(struct foo {...} ) {4}
1063 case tok::annot_cxxscope: // struct foo {...} a:: b;
1064 case tok::annot_typename: // struct foo {...} a ::b;
1065 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001066 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001067 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001068 ExpectedSemi = false;
1069 break;
1070 // Type qualifiers
1071 case tok::kw_const: // struct foo {...} const x;
1072 case tok::kw_volatile: // struct foo {...} volatile x;
1073 case tok::kw_restrict: // struct foo {...} restrict x;
1074 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001075 // Storage-class specifiers
1076 case tok::kw_static: // struct foo {...} static x;
1077 case tok::kw_extern: // struct foo {...} extern x;
1078 case tok::kw_typedef: // struct foo {...} typedef x;
1079 case tok::kw_register: // struct foo {...} register x;
1080 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001081 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001082 // As shown above, type qualifiers and storage class specifiers absolutely
1083 // can occur after class specifiers according to the grammar. However,
1084 // almost noone actually writes code like this. If we see one of these,
1085 // it is much more likely that someone missed a semi colon and the
1086 // type/storage class specifier we're seeing is part of the *next*
1087 // intended declaration, as in:
1088 //
1089 // struct foo { ... }
1090 // typedef int X;
1091 //
1092 // We'd really like to emit a missing semicolon error instead of emitting
1093 // an error on the 'int' saying that you can't have two type specifiers in
1094 // the same declaration of X. Because of this, we look ahead past this
1095 // token to see if it's a type specifier. If so, we know the code is
1096 // otherwise invalid, so we can produce the expected semi error.
1097 if (!isKnownToBeTypeSpecifier(NextToken()))
1098 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001099 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001100
1101 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001102 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001103 if (!getLang().CPlusPlus)
1104 ExpectedSemi = false;
1105 break;
1106 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001107
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001108 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001109 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1110 TagType == DeclSpec::TST_class ? "class"
1111 : TagType == DeclSpec::TST_struct? "struct" : "union");
1112 // Push this token back into the preprocessor and change our current token
1113 // to ';' so that the rest of the code recovers as though there were an
1114 // ';' after the definition.
1115 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001116 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001117 }
1118 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001119}
1120
Mike Stump1eb44332009-09-09 15:08:12 +00001121/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001122///
1123/// base-clause : [C++ class.derived]
1124/// ':' base-specifier-list
1125/// base-specifier-list:
1126/// base-specifier '...'[opt]
1127/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001128void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001129 assert(Tok.is(tok::colon) && "Not a base clause");
1130 ConsumeToken();
1131
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001132 // Build up an array of parsed base specifiers.
John McCallca0408f2010-08-23 06:44:23 +00001133 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001134
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001135 while (true) {
1136 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001137 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001138 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001139 // Skip the rest of this base specifier, up until the comma or
1140 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001141 SkipUntil(tok::comma, tok::l_brace, true, true);
1142 } else {
1143 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001144 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001145 }
1146
1147 // If the next token is a comma, consume it and keep reading
1148 // base-specifiers.
1149 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001151 // Consume the comma.
1152 ConsumeToken();
1153 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001154
1155 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001156 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001157}
1158
1159/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1160/// one entry in the base class list of a class specifier, for example:
1161/// class foo : public bar, virtual private baz {
1162/// 'public bar' and 'virtual private baz' are each base-specifiers.
1163///
1164/// base-specifier: [C++ class.derived]
1165/// ::[opt] nested-name-specifier[opt] class-name
1166/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1167/// class-name
1168/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1169/// class-name
John McCalld226f652010-08-21 09:40:31 +00001170Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001171 bool IsVirtual = false;
1172 SourceLocation StartLoc = Tok.getLocation();
1173
1174 // Parse the 'virtual' keyword.
1175 if (Tok.is(tok::kw_virtual)) {
1176 ConsumeToken();
1177 IsVirtual = true;
1178 }
1179
1180 // Parse an (optional) access specifier.
1181 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001182 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001183 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001185 // Parse the 'virtual' keyword (again!), in case it came after the
1186 // access specifier.
1187 if (Tok.is(tok::kw_virtual)) {
1188 SourceLocation VirtualLoc = ConsumeToken();
1189 if (IsVirtual) {
1190 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001191 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001192 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001193 }
1194
1195 IsVirtual = true;
1196 }
1197
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001198 // Parse optional '::' and optional nested-name-specifier.
1199 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001200 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001201
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001202 // The location of the base class itself.
1203 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001204
1205 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001206 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001207 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1208 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001209 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001210
1211 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001212 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001214 // Notify semantic analysis that we have parsed a complete
1215 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001216 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001217 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001218}
1219
1220/// getAccessSpecifierIfPresent - Determine whether the next token is
1221/// a C++ access-specifier.
1222///
1223/// access-specifier: [C++ class.derived]
1224/// 'private'
1225/// 'protected'
1226/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001227AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001228 switch (Tok.getKind()) {
1229 default: return AS_none;
1230 case tok::kw_private: return AS_private;
1231 case tok::kw_protected: return AS_protected;
1232 case tok::kw_public: return AS_public;
1233 }
1234}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001235
Eli Friedmand33133c2009-07-22 21:45:50 +00001236void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001237 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001238 // We just declared a member function. If this member function
1239 // has any default arguments, we'll need to parse them later.
1240 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001241 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001242 = DeclaratorInfo.getTypeObject(0).Fun;
1243 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1244 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1245 if (!LateMethod) {
1246 // Push this method onto the stack of late-parsed method
1247 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001248 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1249 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001250 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001251
1252 // Add all of the parameters prior to this one (they don't
1253 // have default arguments).
1254 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1255 for (unsigned I = 0; I < ParamIdx; ++I)
1256 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001257 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001258 }
1259
1260 // Add this parameter to the list of parameters (it or may
1261 // not have a default argument).
1262 LateMethod->DefaultArgs.push_back(
1263 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1264 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1265 }
1266 }
1267}
1268
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001269/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1270///
1271/// member-declaration:
1272/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1273/// function-definition ';'[opt]
1274/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1275/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001276/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001277/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001278/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001279///
1280/// member-declarator-list:
1281/// member-declarator
1282/// member-declarator-list ',' member-declarator
1283///
1284/// member-declarator:
1285/// declarator pure-specifier[opt]
1286/// declarator constant-initializer[opt]
1287/// identifier[opt] ':' constant-expression
1288///
Sebastian Redle2b68332009-04-12 17:16:29 +00001289/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001290/// '= 0'
1291///
1292/// constant-initializer:
1293/// '=' constant-expression
1294///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001295void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001296 const ParsedTemplateInfo &TemplateInfo,
1297 ParsingDeclRAIIObject *TemplateDiags) {
John McCall60fa3cf2009-12-11 02:10:03 +00001298 // Access declarations.
1299 if (!TemplateInfo.Kind &&
1300 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001301 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001302 Tok.is(tok::annot_cxxscope)) {
1303 bool isAccessDecl = false;
1304 if (NextToken().is(tok::identifier))
1305 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1306 else
1307 isAccessDecl = NextToken().is(tok::kw_operator);
1308
1309 if (isAccessDecl) {
1310 // Collect the scope specifier token we annotated earlier.
1311 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001312 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCall60fa3cf2009-12-11 02:10:03 +00001313
1314 // Try to parse an unqualified-id.
1315 UnqualifiedId Name;
John McCallb3d87482010-08-24 05:47:05 +00001316 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001317 SkipUntil(tok::semi);
1318 return;
1319 }
1320
1321 // TODO: recover from mistakenly-qualified operator declarations.
1322 if (ExpectAndConsume(tok::semi,
1323 diag::err_expected_semi_after,
1324 "access declaration",
1325 tok::semi))
1326 return;
1327
Douglas Gregor23c94db2010-07-02 17:43:08 +00001328 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001329 false, SourceLocation(),
1330 SS, Name,
1331 /* AttrList */ 0,
1332 /* IsTypeName */ false,
1333 SourceLocation());
1334 return;
1335 }
1336 }
1337
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001338 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001339 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001340 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001341 SourceLocation DeclEnd;
1342 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001343 return;
1344 }
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Chris Lattner682bf922009-03-29 16:50:03 +00001346 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001347 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001348 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001349 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001350 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001351 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001352 return;
1353 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001354
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001355 // Handle: member-declaration ::= '__extension__' member-declaration
1356 if (Tok.is(tok::kw___extension__)) {
1357 // __extension__ silences extension warnings in the subexpression.
1358 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1359 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001360 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001361 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001362
Chris Lattner4ed5d912010-02-02 01:23:29 +00001363 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1364 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001365 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001366
Sean Huntbbd37c62009-11-21 08:43:09 +00001367 CXX0XAttributeList AttrList;
1368 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001369 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001370 AttrList = ParseCXX0XAttributes();
Francois Pichet334d47e2010-10-11 12:59:39 +00001371 if (getLang().Microsoft && Tok.is(tok::l_square))
1372 ParseMicrosoftAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001373
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001374 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001375 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001376
Sean Huntbbd37c62009-11-21 08:43:09 +00001377 if (AttrList.HasAttr)
1378 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1379 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001381 // Eat 'using'.
1382 SourceLocation UsingLoc = ConsumeToken();
1383
1384 if (Tok.is(tok::kw_namespace)) {
1385 Diag(UsingLoc, diag::err_using_namespace_in_class);
1386 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001387 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001388 SourceLocation DeclEnd;
1389 // Otherwise, it must be using-declaration.
John McCall78b81052010-11-10 02:40:36 +00001390 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1391 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001392 }
1393 return;
1394 }
1395
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001396 SourceLocation DSStart = Tok.getLocation();
1397 // decl-specifier-seq:
1398 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001399 ParsingDeclSpec DS(*this, TemplateDiags);
Sean Huntbbd37c62009-11-21 08:43:09 +00001400 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001401 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001402
John McCallf312b1e2010-08-26 23:41:50 +00001403 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001404 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1405 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1406
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001407 if (Tok.is(tok::semi)) {
1408 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001409 Decl *TheDecl =
John McCallc9068d72010-07-16 08:13:16 +00001410 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1411 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001412 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001413 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001414
John McCall54abf7d2009-11-04 02:18:39 +00001415 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001416
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001417 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001418 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1419 ColonProtectionRAIIObject X(*this);
1420
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001421 // Parse the first declarator.
1422 ParseDeclarator(DeclaratorInfo);
1423 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001424 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001425 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001426 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001427 if (Tok.is(tok::semi))
1428 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001429 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001430 }
1431
John Thompson1b2fc0f2009-11-25 22:58:06 +00001432 // If attributes exist after the declarator, but before an '{', parse them.
1433 if (Tok.is(tok::kw___attribute)) {
1434 SourceLocation Loc;
1435 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1436 DeclaratorInfo.AddAttributes(AttrList, Loc);
1437 }
1438
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001439 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001440 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001441 || (DeclaratorInfo.isFunctionDeclarator() &&
1442 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001443 if (!DeclaratorInfo.isFunctionDeclarator()) {
1444 Diag(Tok, diag::err_func_def_no_params);
1445 ConsumeBrace();
1446 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001447 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001448 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001449
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001450 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1451 Diag(Tok, diag::err_function_declared_typedef);
1452 // This recovery skips the entire function body. It would be nice
1453 // to simply call ParseCXXInlineMethodDef() below, however Sema
1454 // assumes the declarator represents a function, not a typedef.
1455 ConsumeBrace();
1456 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001457 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001458 }
1459
Douglas Gregor37b372b2009-08-20 22:52:58 +00001460 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001461 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001462 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001463 }
1464
1465 // member-declarator-list:
1466 // member-declarator
1467 // member-declarator-list ',' member-declarator
1468
John McCalld226f652010-08-21 09:40:31 +00001469 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001470 ExprResult BitfieldSize;
1471 ExprResult Init;
Sebastian Redle2b68332009-04-12 17:16:29 +00001472 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001473
1474 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001475 // member-declarator:
1476 // declarator pure-specifier[opt]
1477 // declarator constant-initializer[opt]
1478 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001479 if (Tok.is(tok::colon)) {
1480 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001481 BitfieldSize = ParseConstantExpression();
1482 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001483 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001484 }
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001486 // pure-specifier:
1487 // '= 0'
1488 //
1489 // constant-initializer:
1490 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001491 //
1492 // defaulted/deleted function-definition:
1493 // '=' 'default' [TODO]
1494 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001495 if (Tok.is(tok::equal)) {
1496 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001497 if (Tok.is(tok::kw_delete)) {
1498 if (!getLang().CPlusPlus0x)
1499 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
Sebastian Redle2b68332009-04-12 17:16:29 +00001500 ConsumeToken();
1501 Deleted = true;
1502 } else {
1503 Init = ParseInitializer();
1504 if (Init.isInvalid())
1505 SkipUntil(tok::comma, true, true);
1506 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001507 }
1508
Chris Lattnere6563252010-06-13 05:34:18 +00001509 // If a simple-asm-expr is present, parse it.
1510 if (Tok.is(tok::kw_asm)) {
1511 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001512 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001513 if (AsmLabel.isInvalid())
1514 SkipUntil(tok::comma, true, true);
1515
1516 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1517 DeclaratorInfo.SetRangeEnd(Loc);
1518 }
1519
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001520 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001521 if (Tok.is(tok::kw___attribute)) {
1522 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001523 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001524 DeclaratorInfo.AddAttributes(AttrList, Loc);
1525 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001526
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001527 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001528 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001529 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001530
John McCalld226f652010-08-21 09:40:31 +00001531 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001532 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001533 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001534 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001535 /*IsDefinition*/ false,
1536 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001537 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001538 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001539 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001540 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001541 BitfieldSize.release(),
1542 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001543 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001544 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001545 }
Chris Lattner682bf922009-03-29 16:50:03 +00001546 if (ThisDecl)
1547 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001548
Douglas Gregor72b505b2008-12-16 21:30:33 +00001549 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001550 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001551 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001552 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001553 }
1554
John McCall54abf7d2009-11-04 02:18:39 +00001555 DeclaratorInfo.complete(ThisDecl);
1556
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001557 // If we don't have a comma, it is either the end of the list (a ';')
1558 // or an error, bail out.
1559 if (Tok.isNot(tok::comma))
1560 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001562 // Consume the comma.
1563 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001565 // Parse the next declarator.
1566 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001567 BitfieldSize = 0;
1568 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001569 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001571 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001572 if (Tok.is(tok::kw___attribute)) {
1573 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001574 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001575 DeclaratorInfo.AddAttributes(AttrList, Loc);
1576 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001577
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001578 if (Tok.isNot(tok::colon))
1579 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001580 }
1581
Chris Lattnerae50d502010-02-02 00:43:15 +00001582 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1583 // Skip to end of block or statement.
1584 SkipUntil(tok::r_brace, true, true);
1585 // If we stopped at a ';', eat it.
1586 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001587 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001588 }
1589
Douglas Gregor23c94db2010-07-02 17:43:08 +00001590 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001591 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001592}
1593
1594/// ParseCXXMemberSpecification - Parse the class definition.
1595///
1596/// member-specification:
1597/// member-declaration member-specification[opt]
1598/// access-specifier ':' member-specification[opt]
1599///
1600void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001601 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001602 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001603 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001604 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001605
John McCallf312b1e2010-08-26 23:41:50 +00001606 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1607 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Douglas Gregor26997fd2010-01-16 20:52:59 +00001609 // Determine whether this is a non-nested class. Note that local
1610 // classes are *not* considered to be nested classes.
1611 bool NonNestedClass = true;
1612 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001613 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001614 if (S->isClassScope()) {
1615 // We're inside a class scope, so this is a nested class.
1616 NonNestedClass = false;
1617 break;
1618 }
1619
1620 if ((S->getFlags() & Scope::FnScope)) {
1621 // If we're in a function or function template declared in the
1622 // body of a class, then this is a local class rather than a
1623 // nested class.
1624 const Scope *Parent = S->getParent();
1625 if (Parent->isTemplateParamScope())
1626 Parent = Parent->getParent();
1627 if (Parent->isClassScope())
1628 break;
1629 }
1630 }
1631 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001632
1633 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001634 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001635
Douglas Gregor6569d682009-05-27 23:11:45 +00001636 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001637 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001638
Douglas Gregorddc29e12009-02-06 22:42:48 +00001639 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001640 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001641
1642 if (Tok.is(tok::colon)) {
1643 ParseBaseClause(TagDecl);
1644
1645 if (!Tok.is(tok::l_brace)) {
1646 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001647
1648 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001649 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001650 return;
1651 }
1652 }
1653
1654 assert(Tok.is(tok::l_brace));
1655
1656 SourceLocation LBraceLoc = ConsumeBrace();
1657
John McCall42a4f662010-05-28 08:11:17 +00001658 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001659 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001660
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001661 // C++ 11p3: Members of a class defined with the keyword class are private
1662 // by default. Members of a class defined with the keywords struct or union
1663 // are public by default.
1664 AccessSpecifier CurAS;
1665 if (TagType == DeclSpec::TST_class)
1666 CurAS = AS_private;
1667 else
1668 CurAS = AS_public;
1669
Douglas Gregor07976d22010-06-21 22:31:09 +00001670 SourceLocation RBraceLoc;
1671 if (TagDecl) {
1672 // While we still have something to read, read the member-declarations.
1673 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1674 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Douglas Gregor07976d22010-06-21 22:31:09 +00001676 // Check for extraneous top-level semicolon.
1677 if (Tok.is(tok::semi)) {
1678 Diag(Tok, diag::ext_extra_struct_semi)
1679 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1680 << FixItHint::CreateRemoval(Tok.getLocation());
1681 ConsumeToken();
1682 continue;
1683 }
1684
1685 AccessSpecifier AS = getAccessSpecifierIfPresent();
1686 if (AS != AS_none) {
1687 // Current token is a C++ access specifier.
1688 CurAS = AS;
1689 SourceLocation ASLoc = Tok.getLocation();
1690 ConsumeToken();
1691 if (Tok.is(tok::colon))
1692 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1693 else
1694 Diag(Tok, diag::err_expected_colon);
1695 ConsumeToken();
1696 continue;
1697 }
1698
1699 // FIXME: Make sure we don't have a template here.
1700
1701 // Parse all the comma separated declarators.
1702 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001703 }
1704
Douglas Gregor07976d22010-06-21 22:31:09 +00001705 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1706 } else {
1707 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001710 // If attributes exist after class contents, parse them.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001711 AttributeList *AttrList = 0;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001712 if (Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001713 AttrList = ParseGNUAttributes();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001714
John McCall42a4f662010-05-28 08:11:17 +00001715 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001716 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001717 LBraceLoc, RBraceLoc,
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001718 AttrList);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001719
1720 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1721 // complete within function bodies, default arguments,
1722 // exception-specifications, and constructor ctor-initializers (including
1723 // such things in nested classes).
1724 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001725 // FIXME: Only function bodies and constructor ctor-initializers are
1726 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001727 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001728 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001729 // are complete and we can parse the delayed portions of method
1730 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001731 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001732 ParseLexedMethodDeclarations(getCurrentClass());
1733 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001734 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001735 }
1736
John McCall42a4f662010-05-28 08:11:17 +00001737 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001738 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001739
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001740 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001741 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001742 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001743}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001744
1745/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1746/// which explicitly initializes the members or base classes of a
1747/// class (C++ [class.base.init]). For example, the three initializers
1748/// after the ':' in the Derived constructor below:
1749///
1750/// @code
1751/// class Base { };
1752/// class Derived : Base {
1753/// int x;
1754/// float f;
1755/// public:
1756/// Derived(float f) : Base(), x(17), f(f) { }
1757/// };
1758/// @endcode
1759///
Mike Stump1eb44332009-09-09 15:08:12 +00001760/// [C++] ctor-initializer:
1761/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001762///
Mike Stump1eb44332009-09-09 15:08:12 +00001763/// [C++] mem-initializer-list:
1764/// mem-initializer
1765/// mem-initializer , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00001766void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001767 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1768
1769 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001770
John McCallca0408f2010-08-23 06:44:23 +00001771 llvm::SmallVector<CXXBaseOrMemberInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001772 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001773
Douglas Gregor7ad83902008-11-05 04:29:56 +00001774 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00001775 if (Tok.is(tok::code_completion)) {
1776 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1777 MemInitializers.data(),
1778 MemInitializers.size());
1779 ConsumeCodeCompletionToken();
1780 } else {
1781 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1782 if (!MemInit.isInvalid())
1783 MemInitializers.push_back(MemInit.get());
1784 else
1785 AnyErrors = true;
1786 }
1787
Douglas Gregor7ad83902008-11-05 04:29:56 +00001788 if (Tok.is(tok::comma))
1789 ConsumeToken();
1790 else if (Tok.is(tok::l_brace))
1791 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00001792 // If the next token looks like a base or member initializer, assume that
1793 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00001794 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
1795 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
1796 Diag(Loc, diag::err_ctor_init_missing_comma)
1797 << FixItHint::CreateInsertion(Loc, ", ");
1798 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001799 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001800 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001801 SkipUntil(tok::l_brace, true, true);
1802 break;
1803 }
1804 } while (true);
1805
Mike Stump1eb44332009-09-09 15:08:12 +00001806 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001807 MemInitializers.data(), MemInitializers.size(),
1808 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001809}
1810
1811/// ParseMemInitializer - Parse a C++ member initializer, which is
1812/// part of a constructor initializer that explicitly initializes one
1813/// member or base class (C++ [class.base.init]). See
1814/// ParseConstructorInitializer for an example.
1815///
1816/// [C++] mem-initializer:
1817/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001818///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001819/// [C++] mem-initializer-id:
1820/// '::'[opt] nested-name-specifier[opt] class-name
1821/// identifier
John McCalld226f652010-08-21 09:40:31 +00001822Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001823 // parse '::'[opt] nested-name-specifier[opt]
1824 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001825 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1826 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00001827 if (Tok.is(tok::annot_template_id)) {
1828 TemplateIdAnnotation *TemplateId
1829 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001830 if (TemplateId->Kind == TNK_Type_template ||
1831 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001832 AnnotateTemplateIdTokenAsType(&SS);
1833 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00001834 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001835 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001836 }
1837 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001838 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001839 return true;
1840 }
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Douglas Gregor7ad83902008-11-05 04:29:56 +00001842 // Get the identifier. This may be a member name or a class name,
1843 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001844 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001845 SourceLocation IdLoc = ConsumeToken();
1846
1847 // Parse the '('.
1848 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001849 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001850 return true;
1851 }
1852 SourceLocation LParenLoc = ConsumeParen();
1853
1854 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001855 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001856 CommaLocsTy CommaLocs;
1857 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1858 SkipUntil(tok::r_paren);
1859 return true;
1860 }
1861
1862 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1863
Douglas Gregor23c94db2010-07-02 17:43:08 +00001864 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00001865 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001866 LParenLoc, ArgExprs.take(),
Douglas Gregora1a04782010-09-09 16:33:13 +00001867 ArgExprs.size(), RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001868}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001869
1870/// ParseExceptionSpecification - Parse a C++ exception-specification
1871/// (C++ [except.spec]).
1872///
Douglas Gregora4745612008-12-01 18:00:20 +00001873/// exception-specification:
1874/// 'throw' '(' type-id-list [opt] ')'
1875/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001876///
Douglas Gregora4745612008-12-01 18:00:20 +00001877/// type-id-list:
1878/// type-id
1879/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001880///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001881bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
John McCallb3d87482010-08-24 05:47:05 +00001882 llvm::SmallVectorImpl<ParsedType>
Sebastian Redlef65f062009-05-29 18:02:33 +00001883 &Exceptions,
John McCallb3d87482010-08-24 05:47:05 +00001884 llvm::SmallVectorImpl<SourceRange>
Sebastian Redlef65f062009-05-29 18:02:33 +00001885 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001886 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001887 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001889 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001891 if (!Tok.is(tok::l_paren)) {
1892 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1893 }
1894 SourceLocation LParenLoc = ConsumeParen();
1895
Douglas Gregora4745612008-12-01 18:00:20 +00001896 // Parse throw(...), a Microsoft extension that means "this function
1897 // can throw anything".
1898 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001899 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001900 SourceLocation EllipsisLoc = ConsumeToken();
1901 if (!getLang().Microsoft)
1902 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001903 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001904 return false;
1905 }
1906
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001907 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001908 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001909 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001910 TypeResult Res(ParseTypeName(&Range));
1911 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001912 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001913 Ranges.push_back(Range);
1914 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001915 if (Tok.is(tok::comma))
1916 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001917 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001918 break;
1919 }
1920
Sebastian Redlab197ba2009-02-09 18:23:29 +00001921 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001922 return false;
1923}
Douglas Gregor6569d682009-05-27 23:11:45 +00001924
Douglas Gregordab60ad2010-10-01 18:44:50 +00001925/// ParseTrailingReturnType - Parse a trailing return type on a new-style
1926/// function declaration.
1927TypeResult Parser::ParseTrailingReturnType() {
1928 assert(Tok.is(tok::arrow) && "expected arrow");
1929
1930 ConsumeToken();
1931
1932 // FIXME: Need to suppress declarations when parsing this typename.
1933 // Otherwise in this function definition:
1934 //
1935 // auto f() -> struct X {}
1936 //
1937 // struct X is parsed as class definition because of the trailing
1938 // brace.
1939
1940 SourceRange Range;
1941 return ParseTypeName(&Range);
1942}
1943
Douglas Gregor6569d682009-05-27 23:11:45 +00001944/// \brief We have just started parsing the definition of a new class,
1945/// so push that class onto our stack of classes that is currently
1946/// being parsed.
John McCalld226f652010-08-21 09:40:31 +00001947void Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001948 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001949 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001950 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001951}
1952
1953/// \brief Deallocate the given parsed class and all of its nested
1954/// classes.
1955void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00001956 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
1957 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00001958 delete Class;
1959}
1960
1961/// \brief Pop the top class of the stack of classes that are
1962/// currently being parsed.
1963///
1964/// This routine should be called when we have finished parsing the
1965/// definition of a class, but have not yet popped the Scope
1966/// associated with the class's definition.
1967///
1968/// \returns true if the class we've popped is a top-level class,
1969/// false otherwise.
1970void Parser::PopParsingClass() {
1971 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Douglas Gregor6569d682009-05-27 23:11:45 +00001973 ParsingClass *Victim = ClassStack.top();
1974 ClassStack.pop();
1975 if (Victim->TopLevelClass) {
1976 // Deallocate all of the nested classes of this class,
1977 // recursively: we don't need to keep any of this information.
1978 DeallocateParsedClasses(Victim);
1979 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001980 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001981 assert(!ClassStack.empty() && "Missing top-level class?");
1982
Douglas Gregord54eb442010-10-12 16:25:54 +00001983 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00001984 // The victim is a nested class, but we will not need to perform
1985 // any processing after the definition of this class since it has
1986 // no members whose handling was delayed. Therefore, we can just
1987 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00001988 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00001989 return;
1990 }
1991
1992 // This nested class has some members that will need to be processed
1993 // after the top-level class is completely defined. Therefore, add
1994 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001995 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00001996 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00001997 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00001998}
Sean Huntbbd37c62009-11-21 08:43:09 +00001999
2000/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
2001/// parses standard attributes.
2002///
2003/// [C++0x] attribute-specifier:
2004/// '[' '[' attribute-list ']' ']'
2005///
2006/// [C++0x] attribute-list:
2007/// attribute[opt]
2008/// attribute-list ',' attribute[opt]
2009///
2010/// [C++0x] attribute:
2011/// attribute-token attribute-argument-clause[opt]
2012///
2013/// [C++0x] attribute-token:
2014/// identifier
2015/// attribute-scoped-token
2016///
2017/// [C++0x] attribute-scoped-token:
2018/// attribute-namespace '::' identifier
2019///
2020/// [C++0x] attribute-namespace:
2021/// identifier
2022///
2023/// [C++0x] attribute-argument-clause:
2024/// '(' balanced-token-seq ')'
2025///
2026/// [C++0x] balanced-token-seq:
2027/// balanced-token
2028/// balanced-token-seq balanced-token
2029///
2030/// [C++0x] balanced-token:
2031/// '(' balanced-token-seq ')'
2032/// '[' balanced-token-seq ']'
2033/// '{' balanced-token-seq '}'
2034/// any token but '(', ')', '[', ']', '{', or '}'
2035CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
2036 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2037 && "Not a C++0x attribute list");
2038
2039 SourceLocation StartLoc = Tok.getLocation(), Loc;
2040 AttributeList *CurrAttr = 0;
2041
2042 ConsumeBracket();
2043 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002044
Sean Huntbbd37c62009-11-21 08:43:09 +00002045 if (Tok.is(tok::comma)) {
2046 Diag(Tok.getLocation(), diag::err_expected_ident);
2047 ConsumeToken();
2048 }
2049
2050 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2051 // attribute not present
2052 if (Tok.is(tok::comma)) {
2053 ConsumeToken();
2054 continue;
2055 }
2056
2057 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2058 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002059
Sean Huntbbd37c62009-11-21 08:43:09 +00002060 // scoped attribute
2061 if (Tok.is(tok::coloncolon)) {
2062 ConsumeToken();
2063
2064 if (!Tok.is(tok::identifier)) {
2065 Diag(Tok.getLocation(), diag::err_expected_ident);
2066 SkipUntil(tok::r_square, tok::comma, true, true);
2067 continue;
2068 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002069
Sean Huntbbd37c62009-11-21 08:43:09 +00002070 ScopeName = AttrName;
2071 ScopeLoc = AttrLoc;
2072
2073 AttrName = Tok.getIdentifierInfo();
2074 AttrLoc = ConsumeToken();
2075 }
2076
2077 bool AttrParsed = false;
2078 // No scoped names are supported; ideally we could put all non-standard
2079 // attributes into namespaces.
2080 if (!ScopeName) {
2081 switch(AttributeList::getKind(AttrName))
2082 {
2083 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002084 case AttributeList::AT_base_check:
2085 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00002086 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00002087 case AttributeList::AT_hiding:
2088 case AttributeList::AT_noreturn:
2089 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002090 if (Tok.is(tok::l_paren)) {
2091 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2092 << AttrName->getName();
2093 break;
2094 }
2095
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002096 CurrAttr = AttrFactory.Create(AttrName, AttrLoc, 0, AttrLoc, 0,
2097 SourceLocation(), 0, 0, CurrAttr, false,
2098 true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002099 AttrParsed = true;
2100 break;
2101 }
2102
2103 // One argument; must be a type-id or assignment-expression
2104 case AttributeList::AT_aligned: {
2105 if (Tok.isNot(tok::l_paren)) {
2106 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2107 << AttrName->getName();
2108 break;
2109 }
2110 SourceLocation ParamLoc = ConsumeParen();
2111
John McCall60d7b3a2010-08-24 06:29:42 +00002112 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002113
2114 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2115
2116 ExprVector ArgExprs(Actions);
2117 ArgExprs.push_back(ArgExpr.release());
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002118 CurrAttr = AttrFactory.Create(AttrName, AttrLoc, 0, AttrLoc,
2119 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
2120 false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002121
2122 AttrParsed = true;
2123 break;
2124 }
2125
2126 // Silence warnings
2127 default: break;
2128 }
2129 }
2130
2131 // Skip the entire parameter clause, if any
2132 if (!AttrParsed && Tok.is(tok::l_paren)) {
2133 ConsumeParen();
2134 // SkipUntil maintains the balancedness of tokens.
2135 SkipUntil(tok::r_paren, false);
2136 }
2137 }
2138
2139 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2140 SkipUntil(tok::r_square, false);
2141 Loc = Tok.getLocation();
2142 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2143 SkipUntil(tok::r_square, false);
2144
2145 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
2146 return Attr;
2147}
2148
2149/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2150/// attribute.
2151///
2152/// FIXME: Simply returns an alignof() expression if the argument is a
2153/// type. Ideally, the type should be propagated directly into Sema.
2154///
2155/// [C++0x] 'align' '(' type-id ')'
2156/// [C++0x] 'align' '(' assignment-expression ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002157ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002158 if (isTypeIdInParens()) {
John McCallf312b1e2010-08-26 23:41:50 +00002159 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sean Huntbbd37c62009-11-21 08:43:09 +00002160 SourceLocation TypeLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002161 ParsedType Ty = ParseTypeName().get();
Sean Huntbbd37c62009-11-21 08:43:09 +00002162 SourceRange TypeRange(Start, Tok.getLocation());
John McCallb3d87482010-08-24 05:47:05 +00002163 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true,
2164 Ty.getAsOpaquePtr(), TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002165 } else
2166 return ParseConstantExpression();
2167}
Francois Pichet334d47e2010-10-11 12:59:39 +00002168
2169/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2170///
2171/// [MS] ms-attribute:
2172/// '[' token-seq ']'
2173///
2174/// [MS] ms-attribute-seq:
2175/// ms-attribute[opt]
2176/// ms-attribute ms-attribute-seq
2177void Parser::ParseMicrosoftAttributes() {
2178 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2179
2180 while (Tok.is(tok::l_square)) {
2181 ConsumeBracket();
2182 SkipUntil(tok::r_square, true, true);
2183 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2184 }
2185}