blob: ce227c6ff2384decad8b06d7412080593a615b84 [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 Kremenek1e377652010-02-11 02:19:13 +000072 llvm::OwningPtr<AttributeList> AttrList;
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 Kremenek1e377652010-02-11 02:19:13 +000077 AttrList.reset(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,
115 LBrace, AttrList.get());
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///
John McCalld226f652010-08-21 09:40:31 +0000186Decl *Parser::ParseLinkage(ParsingDeclSpec &DS,
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000187 unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000188 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000189 llvm::SmallString<8> LangBuffer;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000190 // LangBuffer is guaranteed to be big enough.
Douglas Gregor453091c2010-03-16 22:30:13 +0000191 bool Invalid = false;
192 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
193 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000194 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000195
196 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000197
Douglas Gregor074149e2009-01-05 19:45:36 +0000198 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000199 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000200 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Douglas Gregor074149e2009-01-05 19:45:36 +0000201 /*FIXME: */SourceLocation(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000202 Loc, Lang,
Mike Stump1eb44332009-09-09 15:08:12 +0000203 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000204 : SourceLocation());
205
Sean Huntbbd37c62009-11-21 08:43:09 +0000206 CXX0XAttributeList Attr;
207 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
208 Attr = ParseCXX0XAttributes();
209 }
Francois Pichet334d47e2010-10-11 12:59:39 +0000210 if (getLang().Microsoft && Tok.is(tok::l_square))
211 ParseMicrosoftAttributes();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000212
Douglas Gregor074149e2009-01-05 19:45:36 +0000213 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000214 DS.setExternInLinkageSpec(true);
Douglas Gregor09a63c92010-08-24 14:14:45 +0000215 ParseExternalDeclaration(Attr, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000216 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000217 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000218 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000219
Douglas Gregor63a01132010-02-07 08:38:28 +0000220 DS.abort();
221
Sean Huntbbd37c62009-11-21 08:43:09 +0000222 if (Attr.HasAttr)
223 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
224 << Attr.Range;
225
Douglas Gregorf44515a2008-12-16 22:23:02 +0000226 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000227 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000228 CXX0XAttributeList Attr;
229 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
230 Attr = ParseCXX0XAttributes();
Francois Pichet334d47e2010-10-11 12:59:39 +0000231 if (getLang().Microsoft && Tok.is(tok::l_square))
232 ParseMicrosoftAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +0000233 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000234 }
235
Douglas Gregorf44515a2008-12-16 22:23:02 +0000236 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000237 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000238}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000239
Douglas Gregorf780abc2008-12-30 03:27:21 +0000240/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
241/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000242Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000243 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
Chris Lattner2f274772009-01-06 06:55:51 +0000255 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000256 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-11-21 08:43:09 +0000257 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
258
259 if (Attr.HasAttr)
260 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
261 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000262
263 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000264 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000265 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000266}
267
268/// ParseUsingDirective - Parse C++ using-directive, assumes
269/// that current token is 'namespace' and 'using' was already parsed.
270///
271/// using-directive: [C++ 7.3.p4: namespace.udir]
272/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
273/// namespace-name ;
274/// [GNU] using-directive:
275/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
276/// namespace-name attributes[opt] ;
277///
John McCalld226f652010-08-21 09:40:31 +0000278Decl *Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000279 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000280 SourceLocation &DeclEnd,
281 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000282 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
283
284 // Eat 'namespace'.
285 SourceLocation NamespcLoc = ConsumeToken();
286
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000287 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000288 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000289 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000290 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000291
Douglas Gregorf780abc2008-12-30 03:27:21 +0000292 CXXScopeSpec SS;
293 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000294 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000295
Douglas Gregorf780abc2008-12-30 03:27:21 +0000296 IdentifierInfo *NamespcName = 0;
297 SourceLocation IdentLoc = SourceLocation();
298
299 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000300 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000301 Diag(Tok, diag::err_expected_namespace_name);
302 // If there was invalid namespace name, skip to end of decl, and eat ';'.
303 SkipUntil(tok::semi);
304 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000305 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Chris Lattner823c44e2009-01-06 07:27:21 +0000308 // Parse identifier.
309 NamespcName = Tok.getIdentifierInfo();
310 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Chris Lattner823c44e2009-01-06 07:27:21 +0000312 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000313 bool GNUAttr = false;
314 if (Tok.is(tok::kw___attribute)) {
315 GNUAttr = true;
316 Attr = addAttributeLists(Attr, ParseGNUAttributes());
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Chris Lattner823c44e2009-01-06 07:27:21 +0000319 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000320 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000321 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000322 GNUAttr ? diag::err_expected_semi_after_attribute_list
323 : diag::err_expected_semi_after_namespace_name,
324 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000325
Douglas Gregor23c94db2010-07-02 17:43:08 +0000326 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000327 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000328}
329
330/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
331/// 'using' was already seen.
332///
333/// using-declaration: [C++ 7.3.p3: namespace.udecl]
334/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000335/// unqualified-id
336/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000337///
John McCalld226f652010-08-21 09:40:31 +0000338Decl *Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000339 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000340 SourceLocation &DeclEnd,
341 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000342 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000343 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000344 bool IsTypeName;
345
346 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000347 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000348 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000349 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000350 ConsumeToken();
351 IsTypeName = true;
352 }
353 else
354 IsTypeName = false;
355
356 // Parse nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000357 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000358
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000359 // Check nested-name specifier.
360 if (SS.isInvalid()) {
361 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000362 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000363 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000364
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000365 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000366 // destructor names and allow the action module to diagnose any semantic
367 // errors.
368 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000369 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000370 /*EnteringContext=*/false,
371 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000372 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000373 ParsedType(),
Douglas Gregor12c118a2009-11-04 16:30:06 +0000374 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000375 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000376 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000377 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000378
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000379 // Parse (optional) attributes (most likely GNU strong-using extension).
Ted Kremenek1e377652010-02-11 02:19:13 +0000380 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000381 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000382 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000384 // Eat ';'.
385 DeclEnd = Tok.getLocation();
386 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000387 AttrList ? "attributes list" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000388 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000389
Douglas Gregor23c94db2010-07-02 17:43:08 +0000390 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, Name,
Ted Kremenek1e377652010-02-11 02:19:13 +0000391 AttrList.get(), IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000392}
393
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000394/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
395///
396/// static_assert-declaration:
397/// static_assert ( constant-expression , string-literal ) ;
398///
John McCalld226f652010-08-21 09:40:31 +0000399Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000400 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
401 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000403 if (Tok.isNot(tok::l_paren)) {
404 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000405 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000408 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000409
John McCall60d7b3a2010-08-24 06:29:42 +0000410 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000411 if (AssertExpr.isInvalid()) {
412 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000413 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Anders Carlssonad5f9602009-03-13 23:29:20 +0000416 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000417 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000418
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000419 if (Tok.isNot(tok::string_literal)) {
420 Diag(Tok, diag::err_expected_string_literal);
421 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000422 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
John McCall60d7b3a2010-08-24 06:29:42 +0000425 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000426 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000427 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000428
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000429 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattner97144fc2009-04-02 04:16:50 +0000431 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000432 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000433
John McCall9ae2f072010-08-23 23:25:46 +0000434 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
435 AssertExpr.take(),
436 AssertMessage.take());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000437}
438
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000439/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
440///
441/// 'decltype' ( expression )
442///
443void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
444 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
445
446 SourceLocation StartLoc = ConsumeToken();
447 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000448
449 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000450 "decltype")) {
451 SkipUntil(tok::r_paren);
452 return;
453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000455 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000457 // C++0x [dcl.type.simple]p4:
458 // The operand of the decltype specifier is an unevaluated operand.
459 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000460 Sema::Unevaluated);
John McCall60d7b3a2010-08-24 06:29:42 +0000461 ExprResult Result = ParseExpression();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000462 if (Result.isInvalid()) {
463 SkipUntil(tok::r_paren);
464 return;
465 }
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000467 // Match the ')'
468 SourceLocation RParenLoc;
469 if (Tok.is(tok::r_paren))
470 RParenLoc = ConsumeParen();
471 else
472 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000474 if (RParenLoc.isInvalid())
475 return;
476
477 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000478 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000479 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000480 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000481 DiagID, Result.release()))
482 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000483}
484
Douglas Gregor42a552f2008-11-05 20:51:48 +0000485/// ParseClassName - Parse a C++ class-name, which names a class. Note
486/// that we only check that the result names a type; semantic analysis
487/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000488/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000489/// found.
490///
491/// class-name: [C++ 9.1]
492/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000493/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000494///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000495Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000496 CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000497 // Check whether we have a template-id that names a type.
498 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000499 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000500 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000501 if (TemplateId->Kind == TNK_Type_template ||
502 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000503 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000504
505 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000506 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000507 EndLocation = Tok.getAnnotationEndLoc();
508 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000509
510 if (Type)
511 return Type;
512 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000513 }
514
515 // Fall through to produce an error below.
516 }
517
Douglas Gregor42a552f2008-11-05 20:51:48 +0000518 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000519 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000520 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000521 }
522
Douglas Gregor84d0a192010-01-12 21:28:44 +0000523 IdentifierInfo *Id = Tok.getIdentifierInfo();
524 SourceLocation IdLoc = ConsumeToken();
525
526 if (Tok.is(tok::less)) {
527 // It looks the user intended to write a template-id here, but the
528 // template-name was wrong. Try to fix that.
529 TemplateNameKind TNK = TNK_Type_template;
530 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000531 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor84d0a192010-01-12 21:28:44 +0000532 SS, Template, TNK)) {
533 Diag(IdLoc, diag::err_unknown_template_name)
534 << Id;
535 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000536
Douglas Gregor84d0a192010-01-12 21:28:44 +0000537 if (!Template)
538 return true;
539
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000540 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000541 UnqualifiedId TemplateName;
542 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000543
Douglas Gregor84d0a192010-01-12 21:28:44 +0000544 // Parse the full template-id, then turn it into a type.
545 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
546 SourceLocation(), true))
547 return true;
548 if (TNK == TNK_Dependent_template_name)
549 AnnotateTemplateIdTokenAsType(SS);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000550
Douglas Gregor84d0a192010-01-12 21:28:44 +0000551 // If we didn't end up with a typename token, there's nothing more we
552 // can do.
553 if (Tok.isNot(tok::annot_typename))
554 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000555
Douglas Gregor84d0a192010-01-12 21:28:44 +0000556 // Retrieve the type from the annotation token, consume that token, and
557 // return.
558 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000559 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000560 ConsumeToken();
561 return Type;
562 }
563
Douglas Gregor42a552f2008-11-05 20:51:48 +0000564 // We have an identifier; check whether it is actually a type.
John McCallb3d87482010-08-24 05:47:05 +0000565 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000566 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000567 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000568 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000569 }
570
571 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000572 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000573
574 // Fake up a Declarator to use with ActOnTypeName.
575 DeclSpec DS;
576 DS.SetRangeStart(IdLoc);
577 DS.SetRangeEnd(EndLocation);
578 DS.getTypeSpecScope() = *SS;
579
580 const char *PrevSpec = 0;
581 unsigned DiagID;
582 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
583
584 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
585 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000586}
587
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000588/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
589/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
590/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000591/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000592///
593/// class-specifier: [C++ class]
594/// class-head '{' member-specification[opt] '}'
595/// class-head '{' member-specification[opt] '}' attributes[opt]
596/// class-head:
597/// class-key identifier[opt] base-clause[opt]
598/// class-key nested-name-specifier identifier base-clause[opt]
599/// class-key nested-name-specifier[opt] simple-template-id
600/// base-clause[opt]
601/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000602/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000603/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000604/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000605/// simple-template-id base-clause[opt]
606/// class-key:
607/// 'class'
608/// 'struct'
609/// 'union'
610///
611/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000612/// class-key ::[opt] nested-name-specifier[opt] identifier
613/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
614/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000615///
616/// Note that the C++ class-specifier and elaborated-type-specifier,
617/// together, subsume the C99 struct-or-union-specifier:
618///
619/// struct-or-union-specifier: [C99 6.7.2.1]
620/// struct-or-union identifier[opt] '{' struct-contents '}'
621/// struct-or-union identifier
622/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
623/// '}' attributes[opt]
624/// [GNU] struct-or-union attributes[opt] identifier
625/// struct-or-union:
626/// 'struct'
627/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000628void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
629 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000630 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000631 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000632 DeclSpec::TST TagType;
633 if (TagTokKind == tok::kw_struct)
634 TagType = DeclSpec::TST_struct;
635 else if (TagTokKind == tok::kw_class)
636 TagType = DeclSpec::TST_class;
637 else {
638 assert(TagTokKind == tok::kw_union && "Not a class specifier");
639 TagType = DeclSpec::TST_union;
640 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000641
Douglas Gregor374929f2009-09-18 15:37:17 +0000642 if (Tok.is(tok::code_completion)) {
643 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000644 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000645 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000646 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000647
Chandler Carruth926c4b42010-06-28 08:39:25 +0000648 // C++03 [temp.explicit] 14.7.2/8:
649 // The usual access checking rules do not apply to names used to specify
650 // explicit instantiations.
651 //
652 // As an extension we do not perform access checking on the names used to
653 // specify explicit specializations either. This is important to allow
654 // specializing traits classes for private types.
655 bool SuppressingAccessChecks = false;
656 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
657 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
658 Actions.ActOnStartSuppressingAccessChecks();
659 SuppressingAccessChecks = true;
660 }
661
Sean Huntbbd37c62009-11-21 08:43:09 +0000662 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000663 // If attributes exist after tag, parse them.
664 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000665 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000666
Steve Narofff59e17e2008-12-24 20:59:21 +0000667 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000668 while (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000669 AttrList = ParseMicrosoftDeclSpec(AttrList);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000670
Sean Huntbbd37c62009-11-21 08:43:09 +0000671 // If C++0x attributes exist here, parse them.
672 // FIXME: Are we consistent with the ordering of parsing of different
673 // styles of attributes?
674 if (isCXX0XAttributeSpecifier())
675 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Douglas Gregorb117a602009-09-04 05:53:02 +0000677 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
678 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
679 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000680 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000681 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
682 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000683 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000684 Tok.setKind(tok::identifier);
685 }
686
687 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
688 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
689 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000690 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000691 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
692 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000693 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000694 Tok.setKind(tok::identifier);
695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000697 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000698 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000699 if (getLang().CPlusPlus) {
700 // "FOO : BAR" is not a potential typo for "FOO::BAR".
701 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000702
John McCallb3d87482010-08-24 05:47:05 +0000703 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall207014e2010-07-30 06:26:29 +0000704 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000705 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000706 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
707 Diag(Tok, diag::err_expected_ident);
708 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000709
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000710 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
711
Douglas Gregorcc636682009-02-17 23:15:12 +0000712 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000713 IdentifierInfo *Name = 0;
714 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000715 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000716 if (Tok.is(tok::identifier)) {
717 Name = Tok.getIdentifierInfo();
718 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000719
Douglas Gregor5ee37342010-05-30 22:30:21 +0000720 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000721 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000722 // Eat the template argument list and try to continue parsing this as
723 // a class (or template thereof).
724 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000725 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000726 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000727 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000728 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000729 // We couldn't parse the template argument list at all, so don't
730 // try to give any location information for the list.
731 LAngleLoc = RAngleLoc = SourceLocation();
732 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000733
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000734 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000735 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000736 << (TagType == DeclSpec::TST_class? 0
737 : TagType == DeclSpec::TST_struct? 1
738 : 2)
739 << Name
740 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000741
742 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000743 // we've removed its template argument list.
744 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
745 if (TemplateParams && TemplateParams->size() > 1) {
746 TemplateParams->pop_back();
747 } else {
748 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000749 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000750 = ParsedTemplateInfo::NonTemplate;
751 }
752 } else if (TemplateInfo.Kind
753 == ParsedTemplateInfo::ExplicitInstantiation) {
754 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000755 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000756 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000757 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000758 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000759 = SourceLocation();
760 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
761 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000762 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000763 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000764 } else if (Tok.is(tok::annot_template_id)) {
765 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
766 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000767
Douglas Gregorc45c2322009-03-31 00:43:58 +0000768 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000769 // The template-name in the simple-template-id refers to
770 // something other than a class template. Give an appropriate
771 // error message and skip to the ';'.
772 SourceRange Range(NameLoc);
773 if (SS.isNotEmpty())
774 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000775
Douglas Gregor39a8de12009-02-25 19:37:18 +0000776 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
777 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Douglas Gregor39a8de12009-02-25 19:37:18 +0000779 DS.SetTypeSpecError();
780 SkipUntil(tok::semi, false, true);
781 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000782 if (SuppressingAccessChecks)
783 Actions.ActOnStopSuppressingAccessChecks();
784
Douglas Gregor39a8de12009-02-25 19:37:18 +0000785 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000786 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000787 }
788
Chandler Carruth926c4b42010-06-28 08:39:25 +0000789 // As soon as we're finished parsing the class's template-id, turn access
790 // checking back on.
791 if (SuppressingAccessChecks)
792 Actions.ActOnStopSuppressingAccessChecks();
793
John McCall67d1a672009-08-06 02:15:43 +0000794 // There are four options here. If we have 'struct foo;', then this
795 // is either a forward declaration or a friend declaration, which
796 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000797 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000798 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000799 // However, in some contexts, things look like declarations but are just
800 // references, e.g.
801 // new struct s;
802 // or
803 // &T::operator struct s;
804 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +0000805 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000806 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +0000807 TUK = Sema::TUK_Reference;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000808 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000809 if (DS.isFriendSpecified()) {
810 // C++ [class.friend]p2:
811 // A class shall not be defined in a friend declaration.
812 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
813 << SourceRange(DS.getFriendSpecLoc());
814
815 // Skip everything up to the semicolon, so that this looks like a proper
816 // friend class (or template thereof) declaration.
817 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +0000818 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +0000819 } else {
820 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +0000821 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +0000822 }
823 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +0000824 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000825 else
John McCallf312b1e2010-08-26 23:41:50 +0000826 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000827
John McCall207014e2010-07-30 06:26:29 +0000828 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +0000829 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +0000830 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
831 // We have a declaration or reference to an anonymous class.
832 Diag(StartLoc, diag::err_anon_type_definition)
833 << DeclSpec::getSpecifierName(TagType);
834 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000835
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000836 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000837
838 if (TemplateId)
839 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000840 return;
841 }
842
Douglas Gregorddc29e12009-02-06 22:42:48 +0000843 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +0000844 DeclResult TagOrTempResult = true; // invalid
845 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000846
Douglas Gregor402abb52009-05-28 23:31:59 +0000847 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000848 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000849 // Explicit specialization, class template partial specialization,
850 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000851 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000852 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000853 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000854 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000855 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000856 // This is an explicit instantiation of a class template.
857 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000858 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000859 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000860 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000861 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000862 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000863 SS,
John McCall2b5289b2010-08-23 07:28:44 +0000864 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000865 TemplateId->TemplateNameLoc,
866 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000867 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000868 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000869 AttrList);
John McCall74256f52010-04-14 00:24:33 +0000870
871 // Friend template-ids are treated as references unless
872 // they have template headers, in which case they're ill-formed
873 // (FIXME: "template <class T> friend class A<T>::B<int>;").
874 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +0000875 } else if (TUK == Sema::TUK_Reference ||
876 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +0000877 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCallc4e70192009-09-11 04:59:25 +0000878 TypeResult
John McCall2b5289b2010-08-23 07:28:44 +0000879 = Actions.ActOnTemplateIdType(TemplateId->Template,
John McCall6b2becf2009-09-08 17:47:29 +0000880 TemplateId->TemplateNameLoc,
881 TemplateId->LAngleLoc,
882 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000883 TemplateId->RAngleLoc);
884
John McCallc4e70192009-09-11 04:59:25 +0000885 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
886 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000887 } else {
888 // This is an explicit specialization or a class template
889 // partial specialization.
890 TemplateParameterLists FakedParamLists;
891
892 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
893 // This looks like an explicit instantiation, because we have
894 // something like
895 //
896 // template class Foo<X>
897 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000898 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000899 // meant to be an explicit specialization, but the user forgot
900 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +0000901 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000902
Mike Stump1eb44332009-09-09 15:08:12 +0000903 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000904 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000905 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000906 diag::err_explicit_instantiation_with_definition)
907 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000908 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000909
910 // Create a fake template parameter list that contains only
911 // "template<>", so that we treat this construct as a class
912 // template specialization.
913 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000914 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000915 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000916 LAngleLoc,
917 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000918 LAngleLoc));
919 TemplateParams = &FakedParamLists;
920 }
921
922 // Build the class template specialization.
923 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000924 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000925 StartLoc, SS,
John McCall2b5289b2010-08-23 07:28:44 +0000926 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000927 TemplateId->TemplateNameLoc,
928 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000929 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000930 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000931 AttrList,
John McCallf312b1e2010-08-26 23:41:50 +0000932 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000933 TemplateParams? &(*TemplateParams)[0] : 0,
934 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000935 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000936 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000937 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000938 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000939 // Explicit instantiation of a member of a class template
940 // specialization, e.g.,
941 //
942 // template struct Outer<int>::Inner;
943 //
944 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000945 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000946 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000947 TemplateInfo.TemplateLoc,
948 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000949 NameLoc, AttrList);
John McCall9a34edb2010-10-19 01:40:49 +0000950 } else if (TUK == Sema::TUK_Friend &&
951 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
952 TagOrTempResult =
953 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
954 TagType, StartLoc, SS,
955 Name, NameLoc, AttrList,
956 MultiTemplateParamsArg(Actions,
957 TemplateParams? &(*TemplateParams)[0] : 0,
958 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000959 } else {
960 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000961 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000962 // FIXME: Diagnose this particular error.
963 }
964
John McCallc4e70192009-09-11 04:59:25 +0000965 bool IsDependent = false;
966
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000967 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +0000968 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
969 SS, Name, NameLoc, AttrList, AS,
John McCallf312b1e2010-08-26 23:41:50 +0000970 MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000971 TemplateParams? &(*TemplateParams)[0] : 0,
972 TemplateParams? TemplateParams->size() : 0),
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000973 Owned, IsDependent, false,
974 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +0000975
976 // If ActOnTag said the type was dependent, try again with the
977 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +0000978 if (IsDependent) {
979 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000980 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000981 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +0000982 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000983 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000984
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000985 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +0000986 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +0000987 assert(Tok.is(tok::l_brace) ||
988 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000989 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000990 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000991 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000992 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000993 }
994
John McCallb3d87482010-08-24 05:47:05 +0000995 // FIXME: The DeclSpec should keep the locations of both the keyword and the
996 // name (if there is one).
997 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
998
999 const char *PrevSpec = 0;
1000 unsigned DiagID;
1001 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001002 if (!TypeResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +00001003 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc,
1004 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001005 } else if (!TagOrTempResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +00001006 Result = DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
1007 TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001008 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001009 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001010 return;
1011 }
Mike Stump1eb44332009-09-09 15:08:12 +00001012
John McCallb3d87482010-08-24 05:47:05 +00001013 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001014 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001015
Chris Lattner4ed5d912010-02-02 01:23:29 +00001016 // At this point, we've successfully parsed a class-specifier in 'definition'
1017 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1018 // going to look at what comes after it to improve error recovery. If an
1019 // impossible token occurs next, we assume that the programmer forgot a ; at
1020 // the end of the declaration and recover that way.
1021 //
1022 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001023 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001024 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001025 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001026 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001027 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001028 case tok::star: // struct foo {...} * P;
1029 case tok::amp: // struct foo {...} & R = ...
1030 case tok::identifier: // struct foo {...} V ;
1031 case tok::r_paren: //(struct foo {...} ) {4}
1032 case tok::annot_cxxscope: // struct foo {...} a:: b;
1033 case tok::annot_typename: // struct foo {...} a ::b;
1034 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001035 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001036 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001037 ExpectedSemi = false;
1038 break;
1039 // Type qualifiers
1040 case tok::kw_const: // struct foo {...} const x;
1041 case tok::kw_volatile: // struct foo {...} volatile x;
1042 case tok::kw_restrict: // struct foo {...} restrict x;
1043 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001044 // Storage-class specifiers
1045 case tok::kw_static: // struct foo {...} static x;
1046 case tok::kw_extern: // struct foo {...} extern x;
1047 case tok::kw_typedef: // struct foo {...} typedef x;
1048 case tok::kw_register: // struct foo {...} register x;
1049 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001050 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001051 // As shown above, type qualifiers and storage class specifiers absolutely
1052 // can occur after class specifiers according to the grammar. However,
1053 // almost noone actually writes code like this. If we see one of these,
1054 // it is much more likely that someone missed a semi colon and the
1055 // type/storage class specifier we're seeing is part of the *next*
1056 // intended declaration, as in:
1057 //
1058 // struct foo { ... }
1059 // typedef int X;
1060 //
1061 // We'd really like to emit a missing semicolon error instead of emitting
1062 // an error on the 'int' saying that you can't have two type specifiers in
1063 // the same declaration of X. Because of this, we look ahead past this
1064 // token to see if it's a type specifier. If so, we know the code is
1065 // otherwise invalid, so we can produce the expected semi error.
1066 if (!isKnownToBeTypeSpecifier(NextToken()))
1067 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001068 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001069
1070 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001071 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001072 if (!getLang().CPlusPlus)
1073 ExpectedSemi = false;
1074 break;
1075 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001076
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001077 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001078 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1079 TagType == DeclSpec::TST_class ? "class"
1080 : TagType == DeclSpec::TST_struct? "struct" : "union");
1081 // Push this token back into the preprocessor and change our current token
1082 // to ';' so that the rest of the code recovers as though there were an
1083 // ';' after the definition.
1084 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001085 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001086 }
1087 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001088}
1089
Mike Stump1eb44332009-09-09 15:08:12 +00001090/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001091///
1092/// base-clause : [C++ class.derived]
1093/// ':' base-specifier-list
1094/// base-specifier-list:
1095/// base-specifier '...'[opt]
1096/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001097void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001098 assert(Tok.is(tok::colon) && "Not a base clause");
1099 ConsumeToken();
1100
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001101 // Build up an array of parsed base specifiers.
John McCallca0408f2010-08-23 06:44:23 +00001102 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001103
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001104 while (true) {
1105 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001106 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001107 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001108 // Skip the rest of this base specifier, up until the comma or
1109 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001110 SkipUntil(tok::comma, tok::l_brace, true, true);
1111 } else {
1112 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001113 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001114 }
1115
1116 // If the next token is a comma, consume it and keep reading
1117 // base-specifiers.
1118 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001120 // Consume the comma.
1121 ConsumeToken();
1122 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001123
1124 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001125 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001126}
1127
1128/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1129/// one entry in the base class list of a class specifier, for example:
1130/// class foo : public bar, virtual private baz {
1131/// 'public bar' and 'virtual private baz' are each base-specifiers.
1132///
1133/// base-specifier: [C++ class.derived]
1134/// ::[opt] nested-name-specifier[opt] class-name
1135/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1136/// class-name
1137/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1138/// class-name
John McCalld226f652010-08-21 09:40:31 +00001139Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001140 bool IsVirtual = false;
1141 SourceLocation StartLoc = Tok.getLocation();
1142
1143 // Parse the 'virtual' keyword.
1144 if (Tok.is(tok::kw_virtual)) {
1145 ConsumeToken();
1146 IsVirtual = true;
1147 }
1148
1149 // Parse an (optional) access specifier.
1150 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001151 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001152 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001154 // Parse the 'virtual' keyword (again!), in case it came after the
1155 // access specifier.
1156 if (Tok.is(tok::kw_virtual)) {
1157 SourceLocation VirtualLoc = ConsumeToken();
1158 if (IsVirtual) {
1159 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001160 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001161 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001162 }
1163
1164 IsVirtual = true;
1165 }
1166
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001167 // Parse optional '::' and optional nested-name-specifier.
1168 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001169 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001170
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001171 // The location of the base class itself.
1172 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001173
1174 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001175 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001176 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1177 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001178 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001179
1180 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001181 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001183 // Notify semantic analysis that we have parsed a complete
1184 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001185 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001186 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001187}
1188
1189/// getAccessSpecifierIfPresent - Determine whether the next token is
1190/// a C++ access-specifier.
1191///
1192/// access-specifier: [C++ class.derived]
1193/// 'private'
1194/// 'protected'
1195/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001196AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001197 switch (Tok.getKind()) {
1198 default: return AS_none;
1199 case tok::kw_private: return AS_private;
1200 case tok::kw_protected: return AS_protected;
1201 case tok::kw_public: return AS_public;
1202 }
1203}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001204
Eli Friedmand33133c2009-07-22 21:45:50 +00001205void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001206 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001207 // We just declared a member function. If this member function
1208 // has any default arguments, we'll need to parse them later.
1209 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001210 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001211 = DeclaratorInfo.getTypeObject(0).Fun;
1212 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1213 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1214 if (!LateMethod) {
1215 // Push this method onto the stack of late-parsed method
1216 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001217 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1218 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001219 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001220
1221 // Add all of the parameters prior to this one (they don't
1222 // have default arguments).
1223 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1224 for (unsigned I = 0; I < ParamIdx; ++I)
1225 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001226 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001227 }
1228
1229 // Add this parameter to the list of parameters (it or may
1230 // not have a default argument).
1231 LateMethod->DefaultArgs.push_back(
1232 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1233 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1234 }
1235 }
1236}
1237
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001238/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1239///
1240/// member-declaration:
1241/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1242/// function-definition ';'[opt]
1243/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1244/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001245/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001246/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001247/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001248///
1249/// member-declarator-list:
1250/// member-declarator
1251/// member-declarator-list ',' member-declarator
1252///
1253/// member-declarator:
1254/// declarator pure-specifier[opt]
1255/// declarator constant-initializer[opt]
1256/// identifier[opt] ':' constant-expression
1257///
Sebastian Redle2b68332009-04-12 17:16:29 +00001258/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001259/// '= 0'
1260///
1261/// constant-initializer:
1262/// '=' constant-expression
1263///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001264void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001265 const ParsedTemplateInfo &TemplateInfo,
1266 ParsingDeclRAIIObject *TemplateDiags) {
John McCall60fa3cf2009-12-11 02:10:03 +00001267 // Access declarations.
1268 if (!TemplateInfo.Kind &&
1269 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001270 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001271 Tok.is(tok::annot_cxxscope)) {
1272 bool isAccessDecl = false;
1273 if (NextToken().is(tok::identifier))
1274 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1275 else
1276 isAccessDecl = NextToken().is(tok::kw_operator);
1277
1278 if (isAccessDecl) {
1279 // Collect the scope specifier token we annotated earlier.
1280 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001281 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCall60fa3cf2009-12-11 02:10:03 +00001282
1283 // Try to parse an unqualified-id.
1284 UnqualifiedId Name;
John McCallb3d87482010-08-24 05:47:05 +00001285 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001286 SkipUntil(tok::semi);
1287 return;
1288 }
1289
1290 // TODO: recover from mistakenly-qualified operator declarations.
1291 if (ExpectAndConsume(tok::semi,
1292 diag::err_expected_semi_after,
1293 "access declaration",
1294 tok::semi))
1295 return;
1296
Douglas Gregor23c94db2010-07-02 17:43:08 +00001297 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001298 false, SourceLocation(),
1299 SS, Name,
1300 /* AttrList */ 0,
1301 /* IsTypeName */ false,
1302 SourceLocation());
1303 return;
1304 }
1305 }
1306
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001307 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001308 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001309 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001310 SourceLocation DeclEnd;
1311 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001312 return;
1313 }
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Chris Lattner682bf922009-03-29 16:50:03 +00001315 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001316 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001317 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001318 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001319 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001320 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001321 return;
1322 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001323
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001324 // Handle: member-declaration ::= '__extension__' member-declaration
1325 if (Tok.is(tok::kw___extension__)) {
1326 // __extension__ silences extension warnings in the subexpression.
1327 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1328 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001329 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001330 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001331
Chris Lattner4ed5d912010-02-02 01:23:29 +00001332 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1333 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001334 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001335
Sean Huntbbd37c62009-11-21 08:43:09 +00001336 CXX0XAttributeList AttrList;
1337 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001338 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001339 AttrList = ParseCXX0XAttributes();
Francois Pichet334d47e2010-10-11 12:59:39 +00001340 if (getLang().Microsoft && Tok.is(tok::l_square))
1341 ParseMicrosoftAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001342
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001343 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001344 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001345
Sean Huntbbd37c62009-11-21 08:43:09 +00001346 if (AttrList.HasAttr)
1347 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1348 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001350 // Eat 'using'.
1351 SourceLocation UsingLoc = ConsumeToken();
1352
1353 if (Tok.is(tok::kw_namespace)) {
1354 Diag(UsingLoc, diag::err_using_namespace_in_class);
1355 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001356 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001357 SourceLocation DeclEnd;
1358 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001359 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001360 }
1361 return;
1362 }
1363
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001364 SourceLocation DSStart = Tok.getLocation();
1365 // decl-specifier-seq:
1366 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001367 ParsingDeclSpec DS(*this, TemplateDiags);
Sean Huntbbd37c62009-11-21 08:43:09 +00001368 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001369 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001370
John McCallf312b1e2010-08-26 23:41:50 +00001371 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001372 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1373 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1374
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001375 if (Tok.is(tok::semi)) {
1376 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001377 Decl *TheDecl =
John McCallc9068d72010-07-16 08:13:16 +00001378 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1379 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001380 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001381 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001382
John McCall54abf7d2009-11-04 02:18:39 +00001383 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001384
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001385 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001386 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1387 ColonProtectionRAIIObject X(*this);
1388
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001389 // Parse the first declarator.
1390 ParseDeclarator(DeclaratorInfo);
1391 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001392 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001393 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001394 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001395 if (Tok.is(tok::semi))
1396 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001397 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001398 }
1399
John Thompson1b2fc0f2009-11-25 22:58:06 +00001400 // If attributes exist after the declarator, but before an '{', parse them.
1401 if (Tok.is(tok::kw___attribute)) {
1402 SourceLocation Loc;
1403 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1404 DeclaratorInfo.AddAttributes(AttrList, Loc);
1405 }
1406
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001407 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001408 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001409 || (DeclaratorInfo.isFunctionDeclarator() &&
1410 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001411 if (!DeclaratorInfo.isFunctionDeclarator()) {
1412 Diag(Tok, diag::err_func_def_no_params);
1413 ConsumeBrace();
1414 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001415 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001416 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001417
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001418 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1419 Diag(Tok, diag::err_function_declared_typedef);
1420 // This recovery skips the entire function body. It would be nice
1421 // to simply call ParseCXXInlineMethodDef() below, however Sema
1422 // assumes the declarator represents a function, not a typedef.
1423 ConsumeBrace();
1424 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001425 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001426 }
1427
Douglas Gregor37b372b2009-08-20 22:52:58 +00001428 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001429 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001430 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001431 }
1432
1433 // member-declarator-list:
1434 // member-declarator
1435 // member-declarator-list ',' member-declarator
1436
John McCalld226f652010-08-21 09:40:31 +00001437 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001438 ExprResult BitfieldSize;
1439 ExprResult Init;
Sebastian Redle2b68332009-04-12 17:16:29 +00001440 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001441
1442 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001443 // member-declarator:
1444 // declarator pure-specifier[opt]
1445 // declarator constant-initializer[opt]
1446 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001447 if (Tok.is(tok::colon)) {
1448 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001449 BitfieldSize = ParseConstantExpression();
1450 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001451 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001452 }
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001454 // pure-specifier:
1455 // '= 0'
1456 //
1457 // constant-initializer:
1458 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001459 //
1460 // defaulted/deleted function-definition:
1461 // '=' 'default' [TODO]
1462 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001463 if (Tok.is(tok::equal)) {
1464 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001465 if (Tok.is(tok::kw_delete)) {
1466 if (!getLang().CPlusPlus0x)
1467 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
Sebastian Redle2b68332009-04-12 17:16:29 +00001468 ConsumeToken();
1469 Deleted = true;
1470 } else {
1471 Init = ParseInitializer();
1472 if (Init.isInvalid())
1473 SkipUntil(tok::comma, true, true);
1474 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001475 }
1476
Chris Lattnere6563252010-06-13 05:34:18 +00001477 // If a simple-asm-expr is present, parse it.
1478 if (Tok.is(tok::kw_asm)) {
1479 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001480 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001481 if (AsmLabel.isInvalid())
1482 SkipUntil(tok::comma, true, true);
1483
1484 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1485 DeclaratorInfo.SetRangeEnd(Loc);
1486 }
1487
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001488 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001489 if (Tok.is(tok::kw___attribute)) {
1490 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001491 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001492 DeclaratorInfo.AddAttributes(AttrList, Loc);
1493 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001494
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001495 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001496 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001497 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001498
John McCalld226f652010-08-21 09:40:31 +00001499 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001500 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001501 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001502 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001503 /*IsDefinition*/ false,
1504 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001505 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001506 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001507 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001508 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001509 BitfieldSize.release(),
1510 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001511 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001512 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001513 }
Chris Lattner682bf922009-03-29 16:50:03 +00001514 if (ThisDecl)
1515 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001516
Douglas Gregor72b505b2008-12-16 21:30:33 +00001517 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001518 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001519 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001520 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001521 }
1522
John McCall54abf7d2009-11-04 02:18:39 +00001523 DeclaratorInfo.complete(ThisDecl);
1524
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001525 // If we don't have a comma, it is either the end of the list (a ';')
1526 // or an error, bail out.
1527 if (Tok.isNot(tok::comma))
1528 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001530 // Consume the comma.
1531 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001533 // Parse the next declarator.
1534 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001535 BitfieldSize = 0;
1536 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001537 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001539 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001540 if (Tok.is(tok::kw___attribute)) {
1541 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001542 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001543 DeclaratorInfo.AddAttributes(AttrList, Loc);
1544 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001545
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001546 if (Tok.isNot(tok::colon))
1547 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001548 }
1549
Chris Lattnerae50d502010-02-02 00:43:15 +00001550 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1551 // Skip to end of block or statement.
1552 SkipUntil(tok::r_brace, true, true);
1553 // If we stopped at a ';', eat it.
1554 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001555 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001556 }
1557
Douglas Gregor23c94db2010-07-02 17:43:08 +00001558 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001559 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001560}
1561
1562/// ParseCXXMemberSpecification - Parse the class definition.
1563///
1564/// member-specification:
1565/// member-declaration member-specification[opt]
1566/// access-specifier ':' member-specification[opt]
1567///
1568void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001569 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001570 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001571 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001572 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001573
John McCallf312b1e2010-08-26 23:41:50 +00001574 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1575 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Douglas Gregor26997fd2010-01-16 20:52:59 +00001577 // Determine whether this is a non-nested class. Note that local
1578 // classes are *not* considered to be nested classes.
1579 bool NonNestedClass = true;
1580 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001581 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001582 if (S->isClassScope()) {
1583 // We're inside a class scope, so this is a nested class.
1584 NonNestedClass = false;
1585 break;
1586 }
1587
1588 if ((S->getFlags() & Scope::FnScope)) {
1589 // If we're in a function or function template declared in the
1590 // body of a class, then this is a local class rather than a
1591 // nested class.
1592 const Scope *Parent = S->getParent();
1593 if (Parent->isTemplateParamScope())
1594 Parent = Parent->getParent();
1595 if (Parent->isClassScope())
1596 break;
1597 }
1598 }
1599 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001600
1601 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001602 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001603
Douglas Gregor6569d682009-05-27 23:11:45 +00001604 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001605 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001606
Douglas Gregorddc29e12009-02-06 22:42:48 +00001607 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001608 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001609
1610 if (Tok.is(tok::colon)) {
1611 ParseBaseClause(TagDecl);
1612
1613 if (!Tok.is(tok::l_brace)) {
1614 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001615
1616 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001617 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001618 return;
1619 }
1620 }
1621
1622 assert(Tok.is(tok::l_brace));
1623
1624 SourceLocation LBraceLoc = ConsumeBrace();
1625
John McCall42a4f662010-05-28 08:11:17 +00001626 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001627 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001628
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001629 // C++ 11p3: Members of a class defined with the keyword class are private
1630 // by default. Members of a class defined with the keywords struct or union
1631 // are public by default.
1632 AccessSpecifier CurAS;
1633 if (TagType == DeclSpec::TST_class)
1634 CurAS = AS_private;
1635 else
1636 CurAS = AS_public;
1637
Douglas Gregor07976d22010-06-21 22:31:09 +00001638 SourceLocation RBraceLoc;
1639 if (TagDecl) {
1640 // While we still have something to read, read the member-declarations.
1641 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1642 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Douglas Gregor07976d22010-06-21 22:31:09 +00001644 // Check for extraneous top-level semicolon.
1645 if (Tok.is(tok::semi)) {
1646 Diag(Tok, diag::ext_extra_struct_semi)
1647 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1648 << FixItHint::CreateRemoval(Tok.getLocation());
1649 ConsumeToken();
1650 continue;
1651 }
1652
1653 AccessSpecifier AS = getAccessSpecifierIfPresent();
1654 if (AS != AS_none) {
1655 // Current token is a C++ access specifier.
1656 CurAS = AS;
1657 SourceLocation ASLoc = Tok.getLocation();
1658 ConsumeToken();
1659 if (Tok.is(tok::colon))
1660 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1661 else
1662 Diag(Tok, diag::err_expected_colon);
1663 ConsumeToken();
1664 continue;
1665 }
1666
1667 // FIXME: Make sure we don't have a template here.
1668
1669 // Parse all the comma separated declarators.
1670 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001671 }
1672
Douglas Gregor07976d22010-06-21 22:31:09 +00001673 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1674 } else {
1675 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001676 }
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001678 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001679 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001680 if (Tok.is(tok::kw___attribute))
Douglas Gregor0b4c9b52010-03-29 14:42:08 +00001681 AttrList.reset(ParseGNUAttributes());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001682
John McCall42a4f662010-05-28 08:11:17 +00001683 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001684 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001685 LBraceLoc, RBraceLoc,
1686 AttrList.get());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001687
1688 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1689 // complete within function bodies, default arguments,
1690 // exception-specifications, and constructor ctor-initializers (including
1691 // such things in nested classes).
1692 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001693 // FIXME: Only function bodies and constructor ctor-initializers are
1694 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001695 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001696 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001697 // are complete and we can parse the delayed portions of method
1698 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001699 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001700 ParseLexedMethodDeclarations(getCurrentClass());
1701 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001702 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001703 }
1704
John McCall42a4f662010-05-28 08:11:17 +00001705 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001706 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001707
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001708 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001709 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001710 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001711}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001712
1713/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1714/// which explicitly initializes the members or base classes of a
1715/// class (C++ [class.base.init]). For example, the three initializers
1716/// after the ':' in the Derived constructor below:
1717///
1718/// @code
1719/// class Base { };
1720/// class Derived : Base {
1721/// int x;
1722/// float f;
1723/// public:
1724/// Derived(float f) : Base(), x(17), f(f) { }
1725/// };
1726/// @endcode
1727///
Mike Stump1eb44332009-09-09 15:08:12 +00001728/// [C++] ctor-initializer:
1729/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001730///
Mike Stump1eb44332009-09-09 15:08:12 +00001731/// [C++] mem-initializer-list:
1732/// mem-initializer
1733/// mem-initializer , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00001734void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001735 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1736
1737 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001738
John McCallca0408f2010-08-23 06:44:23 +00001739 llvm::SmallVector<CXXBaseOrMemberInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001740 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001741
Douglas Gregor7ad83902008-11-05 04:29:56 +00001742 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00001743 if (Tok.is(tok::code_completion)) {
1744 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1745 MemInitializers.data(),
1746 MemInitializers.size());
1747 ConsumeCodeCompletionToken();
1748 } else {
1749 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1750 if (!MemInit.isInvalid())
1751 MemInitializers.push_back(MemInit.get());
1752 else
1753 AnyErrors = true;
1754 }
1755
Douglas Gregor7ad83902008-11-05 04:29:56 +00001756 if (Tok.is(tok::comma))
1757 ConsumeToken();
1758 else if (Tok.is(tok::l_brace))
1759 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00001760 // If the next token looks like a base or member initializer, assume that
1761 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00001762 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
1763 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
1764 Diag(Loc, diag::err_ctor_init_missing_comma)
1765 << FixItHint::CreateInsertion(Loc, ", ");
1766 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001767 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001768 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001769 SkipUntil(tok::l_brace, true, true);
1770 break;
1771 }
1772 } while (true);
1773
Mike Stump1eb44332009-09-09 15:08:12 +00001774 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001775 MemInitializers.data(), MemInitializers.size(),
1776 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001777}
1778
1779/// ParseMemInitializer - Parse a C++ member initializer, which is
1780/// part of a constructor initializer that explicitly initializes one
1781/// member or base class (C++ [class.base.init]). See
1782/// ParseConstructorInitializer for an example.
1783///
1784/// [C++] mem-initializer:
1785/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001786///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001787/// [C++] mem-initializer-id:
1788/// '::'[opt] nested-name-specifier[opt] class-name
1789/// identifier
John McCalld226f652010-08-21 09:40:31 +00001790Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001791 // parse '::'[opt] nested-name-specifier[opt]
1792 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001793 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1794 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00001795 if (Tok.is(tok::annot_template_id)) {
1796 TemplateIdAnnotation *TemplateId
1797 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001798 if (TemplateId->Kind == TNK_Type_template ||
1799 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001800 AnnotateTemplateIdTokenAsType(&SS);
1801 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00001802 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001803 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001804 }
1805 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001806 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001807 return true;
1808 }
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Douglas Gregor7ad83902008-11-05 04:29:56 +00001810 // Get the identifier. This may be a member name or a class name,
1811 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001812 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001813 SourceLocation IdLoc = ConsumeToken();
1814
1815 // Parse the '('.
1816 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001817 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001818 return true;
1819 }
1820 SourceLocation LParenLoc = ConsumeParen();
1821
1822 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001823 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001824 CommaLocsTy CommaLocs;
1825 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1826 SkipUntil(tok::r_paren);
1827 return true;
1828 }
1829
1830 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1831
Douglas Gregor23c94db2010-07-02 17:43:08 +00001832 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00001833 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001834 LParenLoc, ArgExprs.take(),
Douglas Gregora1a04782010-09-09 16:33:13 +00001835 ArgExprs.size(), RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001836}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001837
1838/// ParseExceptionSpecification - Parse a C++ exception-specification
1839/// (C++ [except.spec]).
1840///
Douglas Gregora4745612008-12-01 18:00:20 +00001841/// exception-specification:
1842/// 'throw' '(' type-id-list [opt] ')'
1843/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001844///
Douglas Gregora4745612008-12-01 18:00:20 +00001845/// type-id-list:
1846/// type-id
1847/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001848///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001849bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
John McCallb3d87482010-08-24 05:47:05 +00001850 llvm::SmallVectorImpl<ParsedType>
Sebastian Redlef65f062009-05-29 18:02:33 +00001851 &Exceptions,
John McCallb3d87482010-08-24 05:47:05 +00001852 llvm::SmallVectorImpl<SourceRange>
Sebastian Redlef65f062009-05-29 18:02:33 +00001853 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001854 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001855 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001856
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001857 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001859 if (!Tok.is(tok::l_paren)) {
1860 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1861 }
1862 SourceLocation LParenLoc = ConsumeParen();
1863
Douglas Gregora4745612008-12-01 18:00:20 +00001864 // Parse throw(...), a Microsoft extension that means "this function
1865 // can throw anything".
1866 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001867 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001868 SourceLocation EllipsisLoc = ConsumeToken();
1869 if (!getLang().Microsoft)
1870 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001871 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001872 return false;
1873 }
1874
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001875 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001876 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001877 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001878 TypeResult Res(ParseTypeName(&Range));
1879 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001880 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001881 Ranges.push_back(Range);
1882 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001883 if (Tok.is(tok::comma))
1884 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001885 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001886 break;
1887 }
1888
Sebastian Redlab197ba2009-02-09 18:23:29 +00001889 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001890 return false;
1891}
Douglas Gregor6569d682009-05-27 23:11:45 +00001892
Douglas Gregordab60ad2010-10-01 18:44:50 +00001893/// ParseTrailingReturnType - Parse a trailing return type on a new-style
1894/// function declaration.
1895TypeResult Parser::ParseTrailingReturnType() {
1896 assert(Tok.is(tok::arrow) && "expected arrow");
1897
1898 ConsumeToken();
1899
1900 // FIXME: Need to suppress declarations when parsing this typename.
1901 // Otherwise in this function definition:
1902 //
1903 // auto f() -> struct X {}
1904 //
1905 // struct X is parsed as class definition because of the trailing
1906 // brace.
1907
1908 SourceRange Range;
1909 return ParseTypeName(&Range);
1910}
1911
Douglas Gregor6569d682009-05-27 23:11:45 +00001912/// \brief We have just started parsing the definition of a new class,
1913/// so push that class onto our stack of classes that is currently
1914/// being parsed.
John McCalld226f652010-08-21 09:40:31 +00001915void Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001916 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001917 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001918 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001919}
1920
1921/// \brief Deallocate the given parsed class and all of its nested
1922/// classes.
1923void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00001924 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
1925 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00001926 delete Class;
1927}
1928
1929/// \brief Pop the top class of the stack of classes that are
1930/// currently being parsed.
1931///
1932/// This routine should be called when we have finished parsing the
1933/// definition of a class, but have not yet popped the Scope
1934/// associated with the class's definition.
1935///
1936/// \returns true if the class we've popped is a top-level class,
1937/// false otherwise.
1938void Parser::PopParsingClass() {
1939 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Douglas Gregor6569d682009-05-27 23:11:45 +00001941 ParsingClass *Victim = ClassStack.top();
1942 ClassStack.pop();
1943 if (Victim->TopLevelClass) {
1944 // Deallocate all of the nested classes of this class,
1945 // recursively: we don't need to keep any of this information.
1946 DeallocateParsedClasses(Victim);
1947 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001948 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001949 assert(!ClassStack.empty() && "Missing top-level class?");
1950
Douglas Gregord54eb442010-10-12 16:25:54 +00001951 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00001952 // The victim is a nested class, but we will not need to perform
1953 // any processing after the definition of this class since it has
1954 // no members whose handling was delayed. Therefore, we can just
1955 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00001956 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00001957 return;
1958 }
1959
1960 // This nested class has some members that will need to be processed
1961 // after the top-level class is completely defined. Therefore, add
1962 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001963 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00001964 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00001965 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00001966}
Sean Huntbbd37c62009-11-21 08:43:09 +00001967
1968/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1969/// parses standard attributes.
1970///
1971/// [C++0x] attribute-specifier:
1972/// '[' '[' attribute-list ']' ']'
1973///
1974/// [C++0x] attribute-list:
1975/// attribute[opt]
1976/// attribute-list ',' attribute[opt]
1977///
1978/// [C++0x] attribute:
1979/// attribute-token attribute-argument-clause[opt]
1980///
1981/// [C++0x] attribute-token:
1982/// identifier
1983/// attribute-scoped-token
1984///
1985/// [C++0x] attribute-scoped-token:
1986/// attribute-namespace '::' identifier
1987///
1988/// [C++0x] attribute-namespace:
1989/// identifier
1990///
1991/// [C++0x] attribute-argument-clause:
1992/// '(' balanced-token-seq ')'
1993///
1994/// [C++0x] balanced-token-seq:
1995/// balanced-token
1996/// balanced-token-seq balanced-token
1997///
1998/// [C++0x] balanced-token:
1999/// '(' balanced-token-seq ')'
2000/// '[' balanced-token-seq ']'
2001/// '{' balanced-token-seq '}'
2002/// any token but '(', ')', '[', ']', '{', or '}'
2003CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
2004 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2005 && "Not a C++0x attribute list");
2006
2007 SourceLocation StartLoc = Tok.getLocation(), Loc;
2008 AttributeList *CurrAttr = 0;
2009
2010 ConsumeBracket();
2011 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002012
Sean Huntbbd37c62009-11-21 08:43:09 +00002013 if (Tok.is(tok::comma)) {
2014 Diag(Tok.getLocation(), diag::err_expected_ident);
2015 ConsumeToken();
2016 }
2017
2018 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2019 // attribute not present
2020 if (Tok.is(tok::comma)) {
2021 ConsumeToken();
2022 continue;
2023 }
2024
2025 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2026 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002027
Sean Huntbbd37c62009-11-21 08:43:09 +00002028 // scoped attribute
2029 if (Tok.is(tok::coloncolon)) {
2030 ConsumeToken();
2031
2032 if (!Tok.is(tok::identifier)) {
2033 Diag(Tok.getLocation(), diag::err_expected_ident);
2034 SkipUntil(tok::r_square, tok::comma, true, true);
2035 continue;
2036 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002037
Sean Huntbbd37c62009-11-21 08:43:09 +00002038 ScopeName = AttrName;
2039 ScopeLoc = AttrLoc;
2040
2041 AttrName = Tok.getIdentifierInfo();
2042 AttrLoc = ConsumeToken();
2043 }
2044
2045 bool AttrParsed = false;
2046 // No scoped names are supported; ideally we could put all non-standard
2047 // attributes into namespaces.
2048 if (!ScopeName) {
2049 switch(AttributeList::getKind(AttrName))
2050 {
2051 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002052 case AttributeList::AT_base_check:
2053 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00002054 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00002055 case AttributeList::AT_hiding:
2056 case AttributeList::AT_noreturn:
2057 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002058 if (Tok.is(tok::l_paren)) {
2059 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2060 << AttrName->getName();
2061 break;
2062 }
2063
2064 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
2065 SourceLocation(), 0, 0, CurrAttr, false,
2066 true);
2067 AttrParsed = true;
2068 break;
2069 }
2070
2071 // One argument; must be a type-id or assignment-expression
2072 case AttributeList::AT_aligned: {
2073 if (Tok.isNot(tok::l_paren)) {
2074 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2075 << AttrName->getName();
2076 break;
2077 }
2078 SourceLocation ParamLoc = ConsumeParen();
2079
John McCall60d7b3a2010-08-24 06:29:42 +00002080 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002081
2082 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2083
2084 ExprVector ArgExprs(Actions);
2085 ArgExprs.push_back(ArgExpr.release());
2086 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
2087 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
2088 false, true);
2089
2090 AttrParsed = true;
2091 break;
2092 }
2093
2094 // Silence warnings
2095 default: break;
2096 }
2097 }
2098
2099 // Skip the entire parameter clause, if any
2100 if (!AttrParsed && Tok.is(tok::l_paren)) {
2101 ConsumeParen();
2102 // SkipUntil maintains the balancedness of tokens.
2103 SkipUntil(tok::r_paren, false);
2104 }
2105 }
2106
2107 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2108 SkipUntil(tok::r_square, false);
2109 Loc = Tok.getLocation();
2110 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2111 SkipUntil(tok::r_square, false);
2112
2113 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
2114 return Attr;
2115}
2116
2117/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2118/// attribute.
2119///
2120/// FIXME: Simply returns an alignof() expression if the argument is a
2121/// type. Ideally, the type should be propagated directly into Sema.
2122///
2123/// [C++0x] 'align' '(' type-id ')'
2124/// [C++0x] 'align' '(' assignment-expression ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002125ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002126 if (isTypeIdInParens()) {
John McCallf312b1e2010-08-26 23:41:50 +00002127 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sean Huntbbd37c62009-11-21 08:43:09 +00002128 SourceLocation TypeLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002129 ParsedType Ty = ParseTypeName().get();
Sean Huntbbd37c62009-11-21 08:43:09 +00002130 SourceRange TypeRange(Start, Tok.getLocation());
John McCallb3d87482010-08-24 05:47:05 +00002131 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true,
2132 Ty.getAsOpaquePtr(), TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002133 } else
2134 return ParseConstantExpression();
2135}
Francois Pichet334d47e2010-10-11 12:59:39 +00002136
2137/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2138///
2139/// [MS] ms-attribute:
2140/// '[' token-seq ']'
2141///
2142/// [MS] ms-attribute-seq:
2143/// ms-attribute[opt]
2144/// ms-attribute ms-attribute-seq
2145void Parser::ParseMicrosoftAttributes() {
2146 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2147
2148 while (Tok.is(tok::l_square)) {
2149 ConsumeBracket();
2150 SkipUntil(tok::r_square, true, true);
2151 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2152 }
2153}