blob: 3619b8e813104e6ac6b8804bbf4e83c7b6b6bc3f [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"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000019#include "clang/Parse/Template.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000021using namespace clang;
22
23/// ParseNamespace - We know that the current token is a namespace keyword. This
24/// may either be a top level namespace or a block-level namespace alias.
25///
26/// namespace-definition: [C++ 7.3: basic.namespace]
27/// named-namespace-definition
28/// unnamed-namespace-definition
29///
30/// unnamed-namespace-definition:
31/// 'namespace' attributes[opt] '{' namespace-body '}'
32///
33/// named-namespace-definition:
34/// original-namespace-definition
35/// extension-namespace-definition
36///
37/// original-namespace-definition:
38/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
39///
40/// extension-namespace-definition:
41/// 'namespace' original-namespace-name '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000042///
Chris Lattner8f08cb72007-08-25 06:57:03 +000043/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
44/// 'namespace' identifier '=' qualified-namespace-specifier ';'
45///
Chris Lattner97144fc2009-04-02 04:16:50 +000046Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
47 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000048 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000049 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor49f40bd2009-09-18 19:03:04 +000051 if (Tok.is(tok::code_completion)) {
52 Actions.CodeCompleteNamespaceDecl(CurScope);
53 ConsumeToken();
54 }
55
Chris Lattner8f08cb72007-08-25 06:57:03 +000056 SourceLocation IdentLoc;
57 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000058
59 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000060
Chris Lattner04d66662007-10-09 17:33:22 +000061 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000062 Ident = Tok.getIdentifierInfo();
63 IdentLoc = ConsumeToken(); // eat the identifier.
64 }
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 // Read label attributes, if present.
Ted Kremenek1e377652010-02-11 02:19:13 +000067 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000068 if (Tok.is(tok::kw___attribute)) {
69 attrTok = Tok;
70
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // FIXME: save these somewhere.
Ted Kremenek1e377652010-02-11 02:19:13 +000072 AttrList.reset(ParseGNUAttributes());
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor6a588dd2009-06-17 19:49:00 +000075 if (Tok.is(tok::equal)) {
76 if (AttrList)
77 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
78
Chris Lattner97144fc2009-04-02 04:16:50 +000079 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner51448322009-03-29 14:02:43 +000082 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000083 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000084 diag::err_expected_ident_lbrace);
85 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000086 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner51448322009-03-29 14:02:43 +000088 SourceLocation LBrace = ConsumeBrace();
89
90 // Enter a scope for the namespace.
91 ParseScope NamespaceScope(this, Scope::DeclScope);
92
93 DeclPtrTy NamespcDecl =
Ted Kremenek1e377652010-02-11 02:19:13 +000094 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace,
95 AttrList.get());
Chris Lattner51448322009-03-29 14:02:43 +000096
97 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
98 PP.getSourceManager(),
99 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Sean Huntbbd37c62009-11-21 08:43:09 +0000101 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
102 CXX0XAttributeList Attr;
103 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
104 Attr = ParseCXX0XAttributes();
105 ParseExternalDeclaration(Attr);
106 }
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Chris Lattner51448322009-03-29 14:02:43 +0000108 // Leave the namespace scope.
109 NamespaceScope.Exit();
110
Chris Lattner97144fc2009-04-02 04:16:50 +0000111 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
112 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000113
Chris Lattner97144fc2009-04-02 04:16:50 +0000114 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000115 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000116}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000117
Anders Carlssonf67606a2009-03-28 04:07:16 +0000118/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
119/// alias definition.
120///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000121Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000122 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000123 IdentifierInfo *Alias,
124 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000125 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Anders Carlssonf67606a2009-03-28 04:07:16 +0000127 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000129 if (Tok.is(tok::code_completion)) {
130 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
131 ConsumeToken();
132 }
133
Anders Carlssonf67606a2009-03-28 04:07:16 +0000134 CXXScopeSpec SS;
135 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000136 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000137
138 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
139 Diag(Tok, diag::err_expected_namespace_name);
140 // Skip to end of the definition and eat the ';'.
141 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000142 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000143 }
144
145 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000146 IdentifierInfo *Ident = Tok.getIdentifierInfo();
147 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Anders Carlssonf67606a2009-03-28 04:07:16 +0000149 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000150 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000151 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
152 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
154 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000155 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000156}
157
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000158/// ParseLinkage - We know that the current token is a string_literal
159/// and just before that, that extern was seen.
160///
161/// linkage-specification: [C++ 7.5p2: dcl.link]
162/// 'extern' string-literal '{' declaration-seq[opt] '}'
163/// 'extern' string-literal declaration
164///
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000165Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
166 unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000167 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000168 llvm::SmallVector<char, 8> LangBuffer;
169 // LangBuffer is guaranteed to be big enough.
Benjamin Kramerddeea562010-02-27 13:44:12 +0000170 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000171
172 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000173
Douglas Gregor074149e2009-01-05 19:45:36 +0000174 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000175 DeclPtrTy LinkageSpec
176 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor074149e2009-01-05 19:45:36 +0000177 /*FIXME: */SourceLocation(),
Benjamin Kramerddeea562010-02-27 13:44:12 +0000178 Loc, Lang.data(), Lang.size(),
Mike Stump1eb44332009-09-09 15:08:12 +0000179 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000180 : SourceLocation());
181
Sean Huntbbd37c62009-11-21 08:43:09 +0000182 CXX0XAttributeList Attr;
183 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
184 Attr = ParseCXX0XAttributes();
185 }
186
Douglas Gregor074149e2009-01-05 19:45:36 +0000187 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000188 ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000189 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000190 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000191 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000192
Douglas Gregor63a01132010-02-07 08:38:28 +0000193 DS.abort();
194
Sean Huntbbd37c62009-11-21 08:43:09 +0000195 if (Attr.HasAttr)
196 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
197 << Attr.Range;
198
Douglas Gregorf44515a2008-12-16 22:23:02 +0000199 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000200 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000201 CXX0XAttributeList Attr;
202 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
203 Attr = ParseCXX0XAttributes();
204 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000205 }
206
Douglas Gregorf44515a2008-12-16 22:23:02 +0000207 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000208 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000209}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000210
Douglas Gregorf780abc2008-12-30 03:27:21 +0000211/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
212/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000213Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000214 SourceLocation &DeclEnd,
215 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000216 assert(Tok.is(tok::kw_using) && "Not using token");
217
218 // Eat 'using'.
219 SourceLocation UsingLoc = ConsumeToken();
220
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000221 if (Tok.is(tok::code_completion)) {
222 Actions.CodeCompleteUsing(CurScope);
223 ConsumeToken();
224 }
225
Chris Lattner2f274772009-01-06 06:55:51 +0000226 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000227 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-11-21 08:43:09 +0000228 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
229
230 if (Attr.HasAttr)
231 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
232 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000233
234 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000235 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000236 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000237}
238
239/// ParseUsingDirective - Parse C++ using-directive, assumes
240/// that current token is 'namespace' and 'using' was already parsed.
241///
242/// using-directive: [C++ 7.3.p4: namespace.udir]
243/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
244/// namespace-name ;
245/// [GNU] using-directive:
246/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
247/// namespace-name attributes[opt] ;
248///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000249Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000250 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000251 SourceLocation &DeclEnd,
252 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000253 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
254
255 // Eat 'namespace'.
256 SourceLocation NamespcLoc = ConsumeToken();
257
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000258 if (Tok.is(tok::code_completion)) {
259 Actions.CodeCompleteUsingDirective(CurScope);
260 ConsumeToken();
261 }
262
Douglas Gregorf780abc2008-12-30 03:27:21 +0000263 CXXScopeSpec SS;
264 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000265 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000266
Douglas Gregorf780abc2008-12-30 03:27:21 +0000267 IdentifierInfo *NamespcName = 0;
268 SourceLocation IdentLoc = SourceLocation();
269
270 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000271 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000272 Diag(Tok, diag::err_expected_namespace_name);
273 // If there was invalid namespace name, skip to end of decl, and eat ';'.
274 SkipUntil(tok::semi);
275 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000276 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000277 }
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Chris Lattner823c44e2009-01-06 07:27:21 +0000279 // Parse identifier.
280 NamespcName = Tok.getIdentifierInfo();
281 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Chris Lattner823c44e2009-01-06 07:27:21 +0000283 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000284 bool GNUAttr = false;
285 if (Tok.is(tok::kw___attribute)) {
286 GNUAttr = true;
287 Attr = addAttributeLists(Attr, ParseGNUAttributes());
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattner823c44e2009-01-06 07:27:21 +0000290 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000291 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000292 ExpectAndConsume(tok::semi,
Sean Huntbbd37c62009-11-21 08:43:09 +0000293 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner6869d8e2009-06-14 00:07:48 +0000294 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000295
296 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000297 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000298}
299
300/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
301/// 'using' was already seen.
302///
303/// using-declaration: [C++ 7.3.p3: namespace.udecl]
304/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000305/// unqualified-id
306/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000307///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000308Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000309 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000310 SourceLocation &DeclEnd,
311 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000312 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000313 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000314 bool IsTypeName;
315
316 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000317 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000318 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000319 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000320 ConsumeToken();
321 IsTypeName = true;
322 }
323 else
324 IsTypeName = false;
325
326 // Parse nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000327 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000328
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000329 // Check nested-name specifier.
330 if (SS.isInvalid()) {
331 SkipUntil(tok::semi);
332 return DeclPtrTy();
333 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000334
335 // Parse the unqualified-id. We allow parsing of both constructor and
336 // destructor names and allow the action module to diagnose any semantic
337 // errors.
338 UnqualifiedId Name;
339 if (ParseUnqualifiedId(SS,
340 /*EnteringContext=*/false,
341 /*AllowDestructorName=*/true,
342 /*AllowConstructorName=*/true,
343 /*ObjectType=*/0,
344 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000345 SkipUntil(tok::semi);
346 return DeclPtrTy();
347 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000348
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000349 // Parse (optional) attributes (most likely GNU strong-using extension).
Ted Kremenek1e377652010-02-11 02:19:13 +0000350 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000351 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000352 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000354 // Eat ';'.
355 DeclEnd = Tok.getLocation();
356 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000357 AttrList ? "attributes list" : "using declaration",
358 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000359
John McCall60fa3cf2009-12-11 02:10:03 +0000360 return Actions.ActOnUsingDeclaration(CurScope, AS, true, UsingLoc, SS, Name,
Ted Kremenek1e377652010-02-11 02:19:13 +0000361 AttrList.get(), IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000362}
363
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000364/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
365///
366/// static_assert-declaration:
367/// static_assert ( constant-expression , string-literal ) ;
368///
Chris Lattner97144fc2009-04-02 04:16:50 +0000369Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000370 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
371 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000373 if (Tok.isNot(tok::l_paren)) {
374 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000375 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000378 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000379
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000380 OwningExprResult AssertExpr(ParseConstantExpression());
381 if (AssertExpr.isInvalid()) {
382 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000383 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Anders Carlssonad5f9602009-03-13 23:29:20 +0000386 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000387 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000388
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000389 if (Tok.isNot(tok::string_literal)) {
390 Diag(Tok, diag::err_expected_string_literal);
391 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000392 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000395 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000396 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000397 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000398
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000399 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattner97144fc2009-04-02 04:16:50 +0000401 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000402 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
403
Mike Stump1eb44332009-09-09 15:08:12 +0000404 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000405 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000406}
407
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000408/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
409///
410/// 'decltype' ( expression )
411///
412void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
413 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
414
415 SourceLocation StartLoc = ConsumeToken();
416 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
418 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000419 "decltype")) {
420 SkipUntil(tok::r_paren);
421 return;
422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000424 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000426 // C++0x [dcl.type.simple]p4:
427 // The operand of the decltype specifier is an unevaluated operand.
428 EnterExpressionEvaluationContext Unevaluated(Actions,
429 Action::Unevaluated);
430 OwningExprResult Result = ParseExpression();
431 if (Result.isInvalid()) {
432 SkipUntil(tok::r_paren);
433 return;
434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000436 // Match the ')'
437 SourceLocation RParenLoc;
438 if (Tok.is(tok::r_paren))
439 RParenLoc = ConsumeParen();
440 else
441 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000443 if (RParenLoc.isInvalid())
444 return;
445
446 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000447 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000448 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000449 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000450 DiagID, Result.release()))
451 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000452}
453
Douglas Gregor42a552f2008-11-05 20:51:48 +0000454/// ParseClassName - Parse a C++ class-name, which names a class. Note
455/// that we only check that the result names a type; semantic analysis
456/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000457/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000458/// found.
459///
460/// class-name: [C++ 9.1]
461/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000462/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000463///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000464Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Douglas Gregor124b8782010-02-16 19:09:40 +0000465 const CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000466 // Check whether we have a template-id that names a type.
467 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000468 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000469 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000470 if (TemplateId->Kind == TNK_Type_template ||
471 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000472 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000473
474 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
475 TypeTy *Type = Tok.getAnnotationValue();
476 EndLocation = Tok.getAnnotationEndLoc();
477 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000478
479 if (Type)
480 return Type;
481 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000482 }
483
484 // Fall through to produce an error below.
485 }
486
Douglas Gregor42a552f2008-11-05 20:51:48 +0000487 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000488 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000489 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000490 }
491
Douglas Gregor84d0a192010-01-12 21:28:44 +0000492 IdentifierInfo *Id = Tok.getIdentifierInfo();
493 SourceLocation IdLoc = ConsumeToken();
494
495 if (Tok.is(tok::less)) {
496 // It looks the user intended to write a template-id here, but the
497 // template-name was wrong. Try to fix that.
498 TemplateNameKind TNK = TNK_Type_template;
499 TemplateTy Template;
500 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, CurScope,
501 SS, Template, TNK)) {
502 Diag(IdLoc, diag::err_unknown_template_name)
503 << Id;
504 }
505
506 if (!Template)
507 return true;
508
509 // Form the template name
510 UnqualifiedId TemplateName;
511 TemplateName.setIdentifier(Id, IdLoc);
512
513 // Parse the full template-id, then turn it into a type.
514 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
515 SourceLocation(), true))
516 return true;
517 if (TNK == TNK_Dependent_template_name)
518 AnnotateTemplateIdTokenAsType(SS);
519
520 // If we didn't end up with a typename token, there's nothing more we
521 // can do.
522 if (Tok.isNot(tok::annot_typename))
523 return true;
524
525 // Retrieve the type from the annotation token, consume that token, and
526 // return.
527 EndLocation = Tok.getAnnotationEndLoc();
528 TypeTy *Type = Tok.getAnnotationValue();
529 ConsumeToken();
530 return Type;
531 }
532
Douglas Gregor42a552f2008-11-05 20:51:48 +0000533 // We have an identifier; check whether it is actually a type.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000534 TypeTy *Type = Actions.getTypeName(*Id, IdLoc, CurScope, SS, true);
535 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000536 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000537 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000538 }
539
540 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000541 EndLocation = IdLoc;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000542 return Type;
543}
544
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000545/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
546/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
547/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000548/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000549///
550/// class-specifier: [C++ class]
551/// class-head '{' member-specification[opt] '}'
552/// class-head '{' member-specification[opt] '}' attributes[opt]
553/// class-head:
554/// class-key identifier[opt] base-clause[opt]
555/// class-key nested-name-specifier identifier base-clause[opt]
556/// class-key nested-name-specifier[opt] simple-template-id
557/// base-clause[opt]
558/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000559/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000560/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000561/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000562/// simple-template-id base-clause[opt]
563/// class-key:
564/// 'class'
565/// 'struct'
566/// 'union'
567///
568/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000569/// class-key ::[opt] nested-name-specifier[opt] identifier
570/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
571/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000572///
573/// Note that the C++ class-specifier and elaborated-type-specifier,
574/// together, subsume the C99 struct-or-union-specifier:
575///
576/// struct-or-union-specifier: [C99 6.7.2.1]
577/// struct-or-union identifier[opt] '{' struct-contents '}'
578/// struct-or-union identifier
579/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
580/// '}' attributes[opt]
581/// [GNU] struct-or-union attributes[opt] identifier
582/// struct-or-union:
583/// 'struct'
584/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000585void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
586 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000587 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000588 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000589 DeclSpec::TST TagType;
590 if (TagTokKind == tok::kw_struct)
591 TagType = DeclSpec::TST_struct;
592 else if (TagTokKind == tok::kw_class)
593 TagType = DeclSpec::TST_class;
594 else {
595 assert(TagTokKind == tok::kw_union && "Not a class specifier");
596 TagType = DeclSpec::TST_union;
597 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000598
Douglas Gregor374929f2009-09-18 15:37:17 +0000599 if (Tok.is(tok::code_completion)) {
600 // Code completion for a struct, class, or union name.
601 Actions.CodeCompleteTag(CurScope, TagType);
602 ConsumeToken();
603 }
604
Sean Huntbbd37c62009-11-21 08:43:09 +0000605 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000606 // If attributes exist after tag, parse them.
607 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000608 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000609
Steve Narofff59e17e2008-12-24 20:59:21 +0000610 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000611 if (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000612 AttrList = ParseMicrosoftDeclSpec(AttrList);
613
614 // If C++0x attributes exist here, parse them.
615 // FIXME: Are we consistent with the ordering of parsing of different
616 // styles of attributes?
617 if (isCXX0XAttributeSpecifier())
618 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Douglas Gregorb117a602009-09-04 05:53:02 +0000620 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
621 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
622 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000623 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000624 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
625 // properly.
626 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
627 Tok.setKind(tok::identifier);
628 }
629
630 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
631 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
632 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000633 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000634 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
635 // properly.
636 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
637 Tok.setKind(tok::identifier);
638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000640 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000641 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000642 if (getLang().CPlusPlus) {
643 // "FOO : BAR" is not a potential typo for "FOO::BAR".
644 ColonProtectionRAIIObject X(*this);
645
John McCall9ba61662010-02-26 08:45:28 +0000646 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
647 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000648 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
649 Diag(Tok, diag::err_expected_ident);
650 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000651
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000652 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
653
Douglas Gregorcc636682009-02-17 23:15:12 +0000654 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000655 IdentifierInfo *Name = 0;
656 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000657 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000658 if (Tok.is(tok::identifier)) {
659 Name = Tok.getIdentifierInfo();
660 NameLoc = ConsumeToken();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000661
662 if (Tok.is(tok::less)) {
663 // The name was supposed to refer to a template, but didn't.
664 // Eat the template argument list and try to continue parsing this as
665 // a class (or template thereof).
666 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000667 SourceLocation LAngleLoc, RAngleLoc;
668 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
669 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000670 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000671 // We couldn't parse the template argument list at all, so don't
672 // try to give any location information for the list.
673 LAngleLoc = RAngleLoc = SourceLocation();
674 }
675
676 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000677 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000678 << (TagType == DeclSpec::TST_class? 0
679 : TagType == DeclSpec::TST_struct? 1
680 : 2)
681 << Name
682 << SourceRange(LAngleLoc, RAngleLoc);
683
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000684 // Strip off the last template parameter list if it was empty, since
685 // we've removed its template argument list.
686 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
687 if (TemplateParams && TemplateParams->size() > 1) {
688 TemplateParams->pop_back();
689 } else {
690 TemplateParams = 0;
691 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
692 = ParsedTemplateInfo::NonTemplate;
693 }
694 } else if (TemplateInfo.Kind
695 == ParsedTemplateInfo::ExplicitInstantiation) {
696 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000697 TemplateParams = 0;
698 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
699 = ParsedTemplateInfo::NonTemplate;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000700 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
701 = SourceLocation();
702 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
703 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000704 }
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000705
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000706
707 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000708 } else if (Tok.is(tok::annot_template_id)) {
709 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
710 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000711
Douglas Gregorc45c2322009-03-31 00:43:58 +0000712 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000713 // The template-name in the simple-template-id refers to
714 // something other than a class template. Give an appropriate
715 // error message and skip to the ';'.
716 SourceRange Range(NameLoc);
717 if (SS.isNotEmpty())
718 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000719
Douglas Gregor39a8de12009-02-25 19:37:18 +0000720 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
721 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Douglas Gregor39a8de12009-02-25 19:37:18 +0000723 DS.SetTypeSpecError();
724 SkipUntil(tok::semi, false, true);
725 TemplateId->Destroy();
726 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000727 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000728 }
729
John McCall67d1a672009-08-06 02:15:43 +0000730 // There are four options here. If we have 'struct foo;', then this
731 // is either a forward declaration or a friend declaration, which
732 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000733 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000734 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000735 // However, in some contexts, things look like declarations but are just
736 // references, e.g.
737 // new struct s;
738 // or
739 // &T::operator struct s;
740 // For these, SuppressDeclarations is true.
John McCall0f434ec2009-07-31 02:45:11 +0000741 Action::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000742 if (SuppressDeclarations)
743 TUK = Action::TUK_Reference;
744 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000745 if (DS.isFriendSpecified()) {
746 // C++ [class.friend]p2:
747 // A class shall not be defined in a friend declaration.
748 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
749 << SourceRange(DS.getFriendSpecLoc());
750
751 // Skip everything up to the semicolon, so that this looks like a proper
752 // friend class (or template thereof) declaration.
753 SkipUntil(tok::semi, true, true);
754 TUK = Action::TUK_Friend;
755 } else {
756 // Okay, this is a class definition.
757 TUK = Action::TUK_Definition;
758 }
759 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000760 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000761 else
John McCall0f434ec2009-07-31 02:45:11 +0000762 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000763
John McCall0f434ec2009-07-31 02:45:11 +0000764 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000765 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000766 Diag(StartLoc, diag::err_anon_type_definition)
767 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000768
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000769 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000770
771 if (TemplateId)
772 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000773 return;
774 }
775
Douglas Gregorddc29e12009-02-06 22:42:48 +0000776 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000777 Action::DeclResult TagOrTempResult = true; // invalid
778 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000779
John McCall0f434ec2009-07-31 02:45:11 +0000780 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000781 // to turn that template-id into a type.
782
Douglas Gregor402abb52009-05-28 23:31:59 +0000783 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000784 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000785 // Explicit specialization, class template partial specialization,
786 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000787 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000788 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000789 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000790 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000791 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000792 // This is an explicit instantiation of a class template.
793 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000794 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000795 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000796 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000797 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000798 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000799 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000800 TemplateTy::make(TemplateId->Template),
801 TemplateId->TemplateNameLoc,
802 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000803 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000804 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000805 AttrList);
Douglas Gregorfc9cd612009-09-26 20:57:03 +0000806 } else if (TUK == Action::TUK_Reference) {
John McCallc4e70192009-09-11 04:59:25 +0000807 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000808 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
809 TemplateId->TemplateNameLoc,
810 TemplateId->LAngleLoc,
811 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000812 TemplateId->RAngleLoc);
813
John McCallc4e70192009-09-11 04:59:25 +0000814 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
815 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000816 } else {
817 // This is an explicit specialization or a class template
818 // partial specialization.
819 TemplateParameterLists FakedParamLists;
820
821 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
822 // This looks like an explicit instantiation, because we have
823 // something like
824 //
825 // template class Foo<X>
826 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000827 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000828 // meant to be an explicit specialization, but the user forgot
829 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000830 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000831
Mike Stump1eb44332009-09-09 15:08:12 +0000832 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000833 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000834 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000835 diag::err_explicit_instantiation_with_definition)
836 << SourceRange(TemplateInfo.TemplateLoc)
837 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
838
839 // Create a fake template parameter list that contains only
840 // "template<>", so that we treat this construct as a class
841 // template specialization.
842 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000843 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000844 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000845 LAngleLoc,
846 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000847 LAngleLoc));
848 TemplateParams = &FakedParamLists;
849 }
850
851 // Build the class template specialization.
852 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000853 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000854 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000855 TemplateTy::make(TemplateId->Template),
856 TemplateId->TemplateNameLoc,
857 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000858 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000859 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000860 AttrList,
Mike Stump1eb44332009-09-09 15:08:12 +0000861 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000862 TemplateParams? &(*TemplateParams)[0] : 0,
863 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000864 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000865 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000866 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000867 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000868 // Explicit instantiation of a member of a class template
869 // specialization, e.g.,
870 //
871 // template struct Outer<int>::Inner;
872 //
873 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000874 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000875 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000876 TemplateInfo.TemplateLoc,
877 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000878 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000879 } else {
880 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000881 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000882 // FIXME: Diagnose this particular error.
883 }
884
John McCallc4e70192009-09-11 04:59:25 +0000885 bool IsDependent = false;
886
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000887 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000888 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000889 Name, NameLoc, AttrList, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000890 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000891 TemplateParams? &(*TemplateParams)[0] : 0,
892 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000893 Owned, IsDependent);
894
895 // If ActOnTag said the type was dependent, try again with the
896 // less common call.
897 if (IsDependent)
898 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
899 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000900 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000901
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000902 // If there is a body, parse it and inform the actions module.
John McCallbd0dfa52009-12-19 21:48:58 +0000903 if (TUK == Action::TUK_Definition) {
904 assert(Tok.is(tok::l_brace) ||
905 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000906 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000907 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000908 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000909 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000910 }
911
John McCallc4e70192009-09-11 04:59:25 +0000912 void *Result;
913 if (!TypeResult.isInvalid()) {
914 TagType = DeclSpec::TST_typename;
915 Result = TypeResult.get();
916 Owned = false;
917 } else if (!TagOrTempResult.isInvalid()) {
918 Result = TagOrTempResult.get().getAs<void>();
919 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000920 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000921 return;
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
John McCallfec54012009-08-03 20:12:06 +0000924 const char *PrevSpec = 0;
925 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000926
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000927 // FIXME: The DeclSpec should keep the locations of both the keyword and the
928 // name (if there is one).
929 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
930
931 if (DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000932 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000933 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000934
935 // At this point, we've successfully parsed a class-specifier in 'definition'
936 // form (e.g. "struct foo { int x; }". While we could just return here, we're
937 // going to look at what comes after it to improve error recovery. If an
938 // impossible token occurs next, we assume that the programmer forgot a ; at
939 // the end of the declaration and recover that way.
940 //
941 // This switch enumerates the valid "follow" set for definition.
942 if (TUK == Action::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000943 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000944 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000945 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000946 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +0000947 case tok::star: // struct foo {...} * P;
948 case tok::amp: // struct foo {...} & R = ...
949 case tok::identifier: // struct foo {...} V ;
950 case tok::r_paren: //(struct foo {...} ) {4}
951 case tok::annot_cxxscope: // struct foo {...} a:: b;
952 case tok::annot_typename: // struct foo {...} a ::b;
953 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +0000954 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +0000955 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000956 ExpectedSemi = false;
957 break;
958 // Type qualifiers
959 case tok::kw_const: // struct foo {...} const x;
960 case tok::kw_volatile: // struct foo {...} volatile x;
961 case tok::kw_restrict: // struct foo {...} restrict x;
962 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +0000963 // Storage-class specifiers
964 case tok::kw_static: // struct foo {...} static x;
965 case tok::kw_extern: // struct foo {...} extern x;
966 case tok::kw_typedef: // struct foo {...} typedef x;
967 case tok::kw_register: // struct foo {...} register x;
968 case tok::kw_auto: // struct foo {...} auto x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000969 // As shown above, type qualifiers and storage class specifiers absolutely
970 // can occur after class specifiers according to the grammar. However,
971 // almost noone actually writes code like this. If we see one of these,
972 // it is much more likely that someone missed a semi colon and the
973 // type/storage class specifier we're seeing is part of the *next*
974 // intended declaration, as in:
975 //
976 // struct foo { ... }
977 // typedef int X;
978 //
979 // We'd really like to emit a missing semicolon error instead of emitting
980 // an error on the 'int' saying that you can't have two type specifiers in
981 // the same declaration of X. Because of this, we look ahead past this
982 // token to see if it's a type specifier. If so, we know the code is
983 // otherwise invalid, so we can produce the expected semi error.
984 if (!isKnownToBeTypeSpecifier(NextToken()))
985 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000986 break;
987
988 case tok::r_brace: // struct bar { struct foo {...} }
989 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000990 if (!getLang().CPlusPlus)
991 ExpectedSemi = false;
992 break;
993 }
994
995 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +0000996 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
997 TagType == DeclSpec::TST_class ? "class"
998 : TagType == DeclSpec::TST_struct? "struct" : "union");
999 // Push this token back into the preprocessor and change our current token
1000 // to ';' so that the rest of the code recovers as though there were an
1001 // ';' after the definition.
1002 PP.EnterToken(Tok);
1003 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001004 }
1005 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001006}
1007
Mike Stump1eb44332009-09-09 15:08:12 +00001008/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001009///
1010/// base-clause : [C++ class.derived]
1011/// ':' base-specifier-list
1012/// base-specifier-list:
1013/// base-specifier '...'[opt]
1014/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +00001015void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001016 assert(Tok.is(tok::colon) && "Not a base clause");
1017 ConsumeToken();
1018
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001019 // Build up an array of parsed base specifiers.
1020 llvm::SmallVector<BaseTy *, 8> BaseInfo;
1021
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001022 while (true) {
1023 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001024 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001025 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001026 // Skip the rest of this base specifier, up until the comma or
1027 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001028 SkipUntil(tok::comma, tok::l_brace, true, true);
1029 } else {
1030 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001031 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001032 }
1033
1034 // If the next token is a comma, consume it and keep reading
1035 // base-specifiers.
1036 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001038 // Consume the comma.
1039 ConsumeToken();
1040 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001041
1042 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001043 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001044}
1045
1046/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1047/// one entry in the base class list of a class specifier, for example:
1048/// class foo : public bar, virtual private baz {
1049/// 'public bar' and 'virtual private baz' are each base-specifiers.
1050///
1051/// base-specifier: [C++ class.derived]
1052/// ::[opt] nested-name-specifier[opt] class-name
1053/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1054/// class-name
1055/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1056/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +00001057Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001058 bool IsVirtual = false;
1059 SourceLocation StartLoc = Tok.getLocation();
1060
1061 // Parse the 'virtual' keyword.
1062 if (Tok.is(tok::kw_virtual)) {
1063 ConsumeToken();
1064 IsVirtual = true;
1065 }
1066
1067 // Parse an (optional) access specifier.
1068 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001069 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001070 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001072 // Parse the 'virtual' keyword (again!), in case it came after the
1073 // access specifier.
1074 if (Tok.is(tok::kw_virtual)) {
1075 SourceLocation VirtualLoc = ConsumeToken();
1076 if (IsVirtual) {
1077 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001078 Diag(VirtualLoc, diag::err_dup_virtual)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001079 << CodeModificationHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001080 }
1081
1082 IsVirtual = true;
1083 }
1084
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001085 // Parse optional '::' and optional nested-name-specifier.
1086 CXXScopeSpec SS;
Douglas Gregord2b43bf2010-03-02 00:25:00 +00001087 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
1088 /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001089
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001090 // The location of the base class itself.
1091 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001092
1093 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001094 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001095 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1096 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001097 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001098
1099 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001100 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001102 // Notify semantic analysis that we have parsed a complete
1103 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001104 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001105 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001106}
1107
1108/// getAccessSpecifierIfPresent - Determine whether the next token is
1109/// a C++ access-specifier.
1110///
1111/// access-specifier: [C++ class.derived]
1112/// 'private'
1113/// 'protected'
1114/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001115AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001116 switch (Tok.getKind()) {
1117 default: return AS_none;
1118 case tok::kw_private: return AS_private;
1119 case tok::kw_protected: return AS_protected;
1120 case tok::kw_public: return AS_public;
1121 }
1122}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001123
Eli Friedmand33133c2009-07-22 21:45:50 +00001124void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1125 DeclPtrTy ThisDecl) {
1126 // We just declared a member function. If this member function
1127 // has any default arguments, we'll need to parse them later.
1128 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001129 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001130 = DeclaratorInfo.getTypeObject(0).Fun;
1131 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1132 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1133 if (!LateMethod) {
1134 // Push this method onto the stack of late-parsed method
1135 // declarations.
1136 getCurrentClass().MethodDecls.push_back(
1137 LateParsedMethodDeclaration(ThisDecl));
1138 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +00001139 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001140
1141 // Add all of the parameters prior to this one (they don't
1142 // have default arguments).
1143 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1144 for (unsigned I = 0; I < ParamIdx; ++I)
1145 LateMethod->DefaultArgs.push_back(
1146 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1147 }
1148
1149 // Add this parameter to the list of parameters (it or may
1150 // not have a default argument).
1151 LateMethod->DefaultArgs.push_back(
1152 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1153 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1154 }
1155 }
1156}
1157
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001158/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1159///
1160/// member-declaration:
1161/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1162/// function-definition ';'[opt]
1163/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1164/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001165/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001166/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001167/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001168///
1169/// member-declarator-list:
1170/// member-declarator
1171/// member-declarator-list ',' member-declarator
1172///
1173/// member-declarator:
1174/// declarator pure-specifier[opt]
1175/// declarator constant-initializer[opt]
1176/// identifier[opt] ':' constant-expression
1177///
Sebastian Redle2b68332009-04-12 17:16:29 +00001178/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001179/// '= 0'
1180///
1181/// constant-initializer:
1182/// '=' constant-expression
1183///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001184void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1185 const ParsedTemplateInfo &TemplateInfo) {
John McCall60fa3cf2009-12-11 02:10:03 +00001186 // Access declarations.
1187 if (!TemplateInfo.Kind &&
1188 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001189 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001190 Tok.is(tok::annot_cxxscope)) {
1191 bool isAccessDecl = false;
1192 if (NextToken().is(tok::identifier))
1193 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1194 else
1195 isAccessDecl = NextToken().is(tok::kw_operator);
1196
1197 if (isAccessDecl) {
1198 // Collect the scope specifier token we annotated earlier.
1199 CXXScopeSpec SS;
1200 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1201
1202 // Try to parse an unqualified-id.
1203 UnqualifiedId Name;
1204 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1205 SkipUntil(tok::semi);
1206 return;
1207 }
1208
1209 // TODO: recover from mistakenly-qualified operator declarations.
1210 if (ExpectAndConsume(tok::semi,
1211 diag::err_expected_semi_after,
1212 "access declaration",
1213 tok::semi))
1214 return;
1215
1216 Actions.ActOnUsingDeclaration(CurScope, AS,
1217 false, SourceLocation(),
1218 SS, Name,
1219 /* AttrList */ 0,
1220 /* IsTypeName */ false,
1221 SourceLocation());
1222 return;
1223 }
1224 }
1225
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001226 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001227 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001228 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001229 SourceLocation DeclEnd;
1230 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001231 return;
1232 }
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Chris Lattner682bf922009-03-29 16:50:03 +00001234 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001235 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001236 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001237 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001238 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001239 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001240 return;
1241 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001242
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001243 // Handle: member-declaration ::= '__extension__' member-declaration
1244 if (Tok.is(tok::kw___extension__)) {
1245 // __extension__ silences extension warnings in the subexpression.
1246 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1247 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001248 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001249 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001250
Chris Lattner4ed5d912010-02-02 01:23:29 +00001251 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1252 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001253 ColonProtectionRAIIObject X(*this);
1254
Sean Huntbbd37c62009-11-21 08:43:09 +00001255 CXX0XAttributeList AttrList;
1256 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001257 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001258 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001259
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001260 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001261 // FIXME: Check for template aliases
Sean Huntbbd37c62009-11-21 08:43:09 +00001262
1263 if (AttrList.HasAttr)
1264 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1265 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001267 // Eat 'using'.
1268 SourceLocation UsingLoc = ConsumeToken();
1269
1270 if (Tok.is(tok::kw_namespace)) {
1271 Diag(UsingLoc, diag::err_using_namespace_in_class);
1272 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001273 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001274 SourceLocation DeclEnd;
1275 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001276 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001277 }
1278 return;
1279 }
1280
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001281 SourceLocation DSStart = Tok.getLocation();
1282 // decl-specifier-seq:
1283 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001284 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +00001285 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001286 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001287
John McCalldd4a3b02009-09-16 22:47:08 +00001288 Action::MultiTemplateParamsArg TemplateParams(Actions,
1289 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1290 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1291
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001292 if (Tok.is(tok::semi)) {
1293 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001294 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001295 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001296 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001297
John McCall54abf7d2009-11-04 02:18:39 +00001298 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001299
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001300 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001301 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1302 ColonProtectionRAIIObject X(*this);
1303
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001304 // Parse the first declarator.
1305 ParseDeclarator(DeclaratorInfo);
1306 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001307 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001308 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001309 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001310 if (Tok.is(tok::semi))
1311 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001312 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001313 }
1314
John Thompson1b2fc0f2009-11-25 22:58:06 +00001315 // If attributes exist after the declarator, but before an '{', parse them.
1316 if (Tok.is(tok::kw___attribute)) {
1317 SourceLocation Loc;
1318 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1319 DeclaratorInfo.AddAttributes(AttrList, Loc);
1320 }
1321
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001322 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001323 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001324 || (DeclaratorInfo.isFunctionDeclarator() &&
1325 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001326 if (!DeclaratorInfo.isFunctionDeclarator()) {
1327 Diag(Tok, diag::err_func_def_no_params);
1328 ConsumeBrace();
1329 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001330 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001331 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001332
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001333 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1334 Diag(Tok, diag::err_function_declared_typedef);
1335 // This recovery skips the entire function body. It would be nice
1336 // to simply call ParseCXXInlineMethodDef() below, however Sema
1337 // assumes the declarator represents a function, not a typedef.
1338 ConsumeBrace();
1339 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001340 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001341 }
1342
Douglas Gregor37b372b2009-08-20 22:52:58 +00001343 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001344 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001345 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001346 }
1347
1348 // member-declarator-list:
1349 // member-declarator
1350 // member-declarator-list ',' member-declarator
1351
Chris Lattner682bf922009-03-29 16:50:03 +00001352 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001353 OwningExprResult BitfieldSize(Actions);
1354 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001355 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001356
1357 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001358 // member-declarator:
1359 // declarator pure-specifier[opt]
1360 // declarator constant-initializer[opt]
1361 // identifier[opt] ':' constant-expression
1362
1363 if (Tok.is(tok::colon)) {
1364 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001365 BitfieldSize = ParseConstantExpression();
1366 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001367 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001368 }
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001370 // pure-specifier:
1371 // '= 0'
1372 //
1373 // constant-initializer:
1374 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001375 //
1376 // defaulted/deleted function-definition:
1377 // '=' 'default' [TODO]
1378 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001379
1380 if (Tok.is(tok::equal)) {
1381 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001382 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1383 ConsumeToken();
1384 Deleted = true;
1385 } else {
1386 Init = ParseInitializer();
1387 if (Init.isInvalid())
1388 SkipUntil(tok::comma, true, true);
1389 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001390 }
1391
1392 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001393 if (Tok.is(tok::kw___attribute)) {
1394 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001395 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001396 DeclaratorInfo.AddAttributes(AttrList, Loc);
1397 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001398
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001399 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001400 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001401 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001402
1403 DeclPtrTy ThisDecl;
1404 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001405 // TODO: handle initializers, bitfields, 'delete'
1406 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1407 /*IsDefinition*/ false,
1408 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001409 } else {
John McCall67d1a672009-08-06 02:15:43 +00001410 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1411 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001412 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001413 BitfieldSize.release(),
1414 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001415 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001416 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001417 }
Chris Lattner682bf922009-03-29 16:50:03 +00001418 if (ThisDecl)
1419 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001420
Douglas Gregor72b505b2008-12-16 21:30:33 +00001421 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001422 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001423 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001424 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001425 }
1426
John McCall54abf7d2009-11-04 02:18:39 +00001427 DeclaratorInfo.complete(ThisDecl);
1428
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001429 // If we don't have a comma, it is either the end of the list (a ';')
1430 // or an error, bail out.
1431 if (Tok.isNot(tok::comma))
1432 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001434 // Consume the comma.
1435 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001437 // Parse the next declarator.
1438 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001439 BitfieldSize = 0;
1440 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001441 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001443 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001444 if (Tok.is(tok::kw___attribute)) {
1445 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001446 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001447 DeclaratorInfo.AddAttributes(AttrList, Loc);
1448 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001449
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001450 if (Tok.isNot(tok::colon))
1451 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001452 }
1453
Chris Lattnerae50d502010-02-02 00:43:15 +00001454 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1455 // Skip to end of block or statement.
1456 SkipUntil(tok::r_brace, true, true);
1457 // If we stopped at a ';', eat it.
1458 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001459 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001460 }
1461
Chris Lattnerae50d502010-02-02 00:43:15 +00001462 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
1463 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001464}
1465
1466/// ParseCXXMemberSpecification - Parse the class definition.
1467///
1468/// member-specification:
1469/// member-declaration member-specification[opt]
1470/// access-specifier ':' member-specification[opt]
1471///
1472void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001473 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001474 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001475 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001476 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001477
Chris Lattner49f28ca2009-03-05 08:00:35 +00001478 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1479 PP.getSourceManager(),
1480 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Douglas Gregor26997fd2010-01-16 20:52:59 +00001482 // Determine whether this is a non-nested class. Note that local
1483 // classes are *not* considered to be nested classes.
1484 bool NonNestedClass = true;
1485 if (!ClassStack.empty()) {
1486 for (const Scope *S = CurScope; S; S = S->getParent()) {
1487 if (S->isClassScope()) {
1488 // We're inside a class scope, so this is a nested class.
1489 NonNestedClass = false;
1490 break;
1491 }
1492
1493 if ((S->getFlags() & Scope::FnScope)) {
1494 // If we're in a function or function template declared in the
1495 // body of a class, then this is a local class rather than a
1496 // nested class.
1497 const Scope *Parent = S->getParent();
1498 if (Parent->isTemplateParamScope())
1499 Parent = Parent->getParent();
1500 if (Parent->isClassScope())
1501 break;
1502 }
1503 }
1504 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001505
1506 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001507 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001508
Douglas Gregor6569d682009-05-27 23:11:45 +00001509 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001510 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001511
Douglas Gregorddc29e12009-02-06 22:42:48 +00001512 if (TagDecl)
1513 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001514
1515 if (Tok.is(tok::colon)) {
1516 ParseBaseClause(TagDecl);
1517
1518 if (!Tok.is(tok::l_brace)) {
1519 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
1520 return;
1521 }
1522 }
1523
1524 assert(Tok.is(tok::l_brace));
1525
1526 SourceLocation LBraceLoc = ConsumeBrace();
1527
1528 if (!TagDecl) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001529 SkipUntil(tok::r_brace, false, false);
1530 return;
1531 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001532
John McCallf9368152009-12-20 07:58:13 +00001533 Actions.ActOnStartCXXMemberDeclarations(CurScope, TagDecl, LBraceLoc);
1534
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001535 // C++ 11p3: Members of a class defined with the keyword class are private
1536 // by default. Members of a class defined with the keywords struct or union
1537 // are public by default.
1538 AccessSpecifier CurAS;
1539 if (TagType == DeclSpec::TST_class)
1540 CurAS = AS_private;
1541 else
1542 CurAS = AS_public;
1543
1544 // While we still have something to read, read the member-declarations.
1545 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1546 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001548 // Check for extraneous top-level semicolon.
1549 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001550 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001551 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001552 ConsumeToken();
1553 continue;
1554 }
1555
1556 AccessSpecifier AS = getAccessSpecifierIfPresent();
1557 if (AS != AS_none) {
1558 // Current token is a C++ access specifier.
1559 CurAS = AS;
1560 ConsumeToken();
1561 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1562 continue;
1563 }
1564
Douglas Gregor37b372b2009-08-20 22:52:58 +00001565 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001567 // Parse all the comma separated declarators.
1568 ParseCXXClassMemberDeclaration(CurAS);
1569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001571 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001573 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001574 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001575 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001576 AttrList.reset(ParseGNUAttributes()); // FIXME: where should I put them?
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001577
1578 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1579 LBraceLoc, RBraceLoc);
1580
1581 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1582 // complete within function bodies, default arguments,
1583 // exception-specifications, and constructor ctor-initializers (including
1584 // such things in nested classes).
1585 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001586 // FIXME: Only function bodies and constructor ctor-initializers are
1587 // parsed correctly, fix the rest.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001588 if (NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001589 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001590 // are complete and we can parse the delayed portions of method
1591 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001592 ParseLexedMethodDeclarations(getCurrentClass());
1593 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001594 }
1595
1596 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001597 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001598 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001599
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001600 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001601}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001602
1603/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1604/// which explicitly initializes the members or base classes of a
1605/// class (C++ [class.base.init]). For example, the three initializers
1606/// after the ':' in the Derived constructor below:
1607///
1608/// @code
1609/// class Base { };
1610/// class Derived : Base {
1611/// int x;
1612/// float f;
1613/// public:
1614/// Derived(float f) : Base(), x(17), f(f) { }
1615/// };
1616/// @endcode
1617///
Mike Stump1eb44332009-09-09 15:08:12 +00001618/// [C++] ctor-initializer:
1619/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001620///
Mike Stump1eb44332009-09-09 15:08:12 +00001621/// [C++] mem-initializer-list:
1622/// mem-initializer
1623/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001624void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001625 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1626
1627 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Douglas Gregor7ad83902008-11-05 04:29:56 +00001629 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001630 bool AnyErrors = false;
1631
Douglas Gregor7ad83902008-11-05 04:29:56 +00001632 do {
1633 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001634 if (!MemInit.isInvalid())
1635 MemInitializers.push_back(MemInit.get());
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001636 else
1637 AnyErrors = true;
1638
Douglas Gregor7ad83902008-11-05 04:29:56 +00001639 if (Tok.is(tok::comma))
1640 ConsumeToken();
1641 else if (Tok.is(tok::l_brace))
1642 break;
1643 else {
1644 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001645 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001646 SkipUntil(tok::l_brace, true, true);
1647 break;
1648 }
1649 } while (true);
1650
Mike Stump1eb44332009-09-09 15:08:12 +00001651 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001652 MemInitializers.data(), MemInitializers.size(),
1653 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001654}
1655
1656/// ParseMemInitializer - Parse a C++ member initializer, which is
1657/// part of a constructor initializer that explicitly initializes one
1658/// member or base class (C++ [class.base.init]). See
1659/// ParseConstructorInitializer for an example.
1660///
1661/// [C++] mem-initializer:
1662/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001663///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001664/// [C++] mem-initializer-id:
1665/// '::'[opt] nested-name-specifier[opt] class-name
1666/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001667Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001668 // parse '::'[opt] nested-name-specifier[opt]
1669 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001670 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001671 TypeTy *TemplateTypeTy = 0;
1672 if (Tok.is(tok::annot_template_id)) {
1673 TemplateIdAnnotation *TemplateId
1674 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001675 if (TemplateId->Kind == TNK_Type_template ||
1676 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001677 AnnotateTemplateIdTokenAsType(&SS);
1678 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1679 TemplateTypeTy = Tok.getAnnotationValue();
1680 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001681 }
1682 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001683 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001684 return true;
1685 }
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Douglas Gregor7ad83902008-11-05 04:29:56 +00001687 // Get the identifier. This may be a member name or a class name,
1688 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001689 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001690 SourceLocation IdLoc = ConsumeToken();
1691
1692 // Parse the '('.
1693 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001694 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001695 return true;
1696 }
1697 SourceLocation LParenLoc = ConsumeParen();
1698
1699 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001700 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001701 CommaLocsTy CommaLocs;
1702 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1703 SkipUntil(tok::r_paren);
1704 return true;
1705 }
1706
1707 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1708
Fariborz Jahanian96174332009-07-01 19:21:19 +00001709 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1710 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001711 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001712 ArgExprs.size(), CommaLocs.data(),
1713 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001714}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001715
1716/// ParseExceptionSpecification - Parse a C++ exception-specification
1717/// (C++ [except.spec]).
1718///
Douglas Gregora4745612008-12-01 18:00:20 +00001719/// exception-specification:
1720/// 'throw' '(' type-id-list [opt] ')'
1721/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001722///
Douglas Gregora4745612008-12-01 18:00:20 +00001723/// type-id-list:
1724/// type-id
1725/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001726///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001727bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001728 llvm::SmallVector<TypeTy*, 2>
1729 &Exceptions,
1730 llvm::SmallVector<SourceRange, 2>
1731 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001732 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001733 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001735 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001737 if (!Tok.is(tok::l_paren)) {
1738 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1739 }
1740 SourceLocation LParenLoc = ConsumeParen();
1741
Douglas Gregora4745612008-12-01 18:00:20 +00001742 // Parse throw(...), a Microsoft extension that means "this function
1743 // can throw anything".
1744 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001745 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001746 SourceLocation EllipsisLoc = ConsumeToken();
1747 if (!getLang().Microsoft)
1748 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001749 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001750 return false;
1751 }
1752
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001753 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001754 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001755 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001756 TypeResult Res(ParseTypeName(&Range));
1757 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001758 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001759 Ranges.push_back(Range);
1760 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001761 if (Tok.is(tok::comma))
1762 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001763 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001764 break;
1765 }
1766
Sebastian Redlab197ba2009-02-09 18:23:29 +00001767 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001768 return false;
1769}
Douglas Gregor6569d682009-05-27 23:11:45 +00001770
1771/// \brief We have just started parsing the definition of a new class,
1772/// so push that class onto our stack of classes that is currently
1773/// being parsed.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001774void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool NonNestedClass) {
1775 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001776 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001777 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001778}
1779
1780/// \brief Deallocate the given parsed class and all of its nested
1781/// classes.
1782void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1783 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1784 DeallocateParsedClasses(Class->NestedClasses[I]);
1785 delete Class;
1786}
1787
1788/// \brief Pop the top class of the stack of classes that are
1789/// currently being parsed.
1790///
1791/// This routine should be called when we have finished parsing the
1792/// definition of a class, but have not yet popped the Scope
1793/// associated with the class's definition.
1794///
1795/// \returns true if the class we've popped is a top-level class,
1796/// false otherwise.
1797void Parser::PopParsingClass() {
1798 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Douglas Gregor6569d682009-05-27 23:11:45 +00001800 ParsingClass *Victim = ClassStack.top();
1801 ClassStack.pop();
1802 if (Victim->TopLevelClass) {
1803 // Deallocate all of the nested classes of this class,
1804 // recursively: we don't need to keep any of this information.
1805 DeallocateParsedClasses(Victim);
1806 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001807 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001808 assert(!ClassStack.empty() && "Missing top-level class?");
1809
1810 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1811 Victim->NestedClasses.empty()) {
1812 // The victim is a nested class, but we will not need to perform
1813 // any processing after the definition of this class since it has
1814 // no members whose handling was delayed. Therefore, we can just
1815 // remove this nested class.
1816 delete Victim;
1817 return;
1818 }
1819
1820 // This nested class has some members that will need to be processed
1821 // after the top-level class is completely defined. Therefore, add
1822 // it to the list of nested classes within its parent.
1823 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1824 ClassStack.top()->NestedClasses.push_back(Victim);
1825 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1826}
Sean Huntbbd37c62009-11-21 08:43:09 +00001827
1828/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1829/// parses standard attributes.
1830///
1831/// [C++0x] attribute-specifier:
1832/// '[' '[' attribute-list ']' ']'
1833///
1834/// [C++0x] attribute-list:
1835/// attribute[opt]
1836/// attribute-list ',' attribute[opt]
1837///
1838/// [C++0x] attribute:
1839/// attribute-token attribute-argument-clause[opt]
1840///
1841/// [C++0x] attribute-token:
1842/// identifier
1843/// attribute-scoped-token
1844///
1845/// [C++0x] attribute-scoped-token:
1846/// attribute-namespace '::' identifier
1847///
1848/// [C++0x] attribute-namespace:
1849/// identifier
1850///
1851/// [C++0x] attribute-argument-clause:
1852/// '(' balanced-token-seq ')'
1853///
1854/// [C++0x] balanced-token-seq:
1855/// balanced-token
1856/// balanced-token-seq balanced-token
1857///
1858/// [C++0x] balanced-token:
1859/// '(' balanced-token-seq ')'
1860/// '[' balanced-token-seq ']'
1861/// '{' balanced-token-seq '}'
1862/// any token but '(', ')', '[', ']', '{', or '}'
1863CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1864 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1865 && "Not a C++0x attribute list");
1866
1867 SourceLocation StartLoc = Tok.getLocation(), Loc;
1868 AttributeList *CurrAttr = 0;
1869
1870 ConsumeBracket();
1871 ConsumeBracket();
1872
1873 if (Tok.is(tok::comma)) {
1874 Diag(Tok.getLocation(), diag::err_expected_ident);
1875 ConsumeToken();
1876 }
1877
1878 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1879 // attribute not present
1880 if (Tok.is(tok::comma)) {
1881 ConsumeToken();
1882 continue;
1883 }
1884
1885 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1886 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
1887
1888 // scoped attribute
1889 if (Tok.is(tok::coloncolon)) {
1890 ConsumeToken();
1891
1892 if (!Tok.is(tok::identifier)) {
1893 Diag(Tok.getLocation(), diag::err_expected_ident);
1894 SkipUntil(tok::r_square, tok::comma, true, true);
1895 continue;
1896 }
1897
1898 ScopeName = AttrName;
1899 ScopeLoc = AttrLoc;
1900
1901 AttrName = Tok.getIdentifierInfo();
1902 AttrLoc = ConsumeToken();
1903 }
1904
1905 bool AttrParsed = false;
1906 // No scoped names are supported; ideally we could put all non-standard
1907 // attributes into namespaces.
1908 if (!ScopeName) {
1909 switch(AttributeList::getKind(AttrName))
1910 {
1911 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00001912 case AttributeList::AT_base_check:
1913 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00001914 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00001915 case AttributeList::AT_hiding:
1916 case AttributeList::AT_noreturn:
1917 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00001918 if (Tok.is(tok::l_paren)) {
1919 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1920 << AttrName->getName();
1921 break;
1922 }
1923
1924 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1925 SourceLocation(), 0, 0, CurrAttr, false,
1926 true);
1927 AttrParsed = true;
1928 break;
1929 }
1930
1931 // One argument; must be a type-id or assignment-expression
1932 case AttributeList::AT_aligned: {
1933 if (Tok.isNot(tok::l_paren)) {
1934 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1935 << AttrName->getName();
1936 break;
1937 }
1938 SourceLocation ParamLoc = ConsumeParen();
1939
1940 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1941
1942 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1943
1944 ExprVector ArgExprs(Actions);
1945 ArgExprs.push_back(ArgExpr.release());
1946 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1947 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1948 false, true);
1949
1950 AttrParsed = true;
1951 break;
1952 }
1953
1954 // Silence warnings
1955 default: break;
1956 }
1957 }
1958
1959 // Skip the entire parameter clause, if any
1960 if (!AttrParsed && Tok.is(tok::l_paren)) {
1961 ConsumeParen();
1962 // SkipUntil maintains the balancedness of tokens.
1963 SkipUntil(tok::r_paren, false);
1964 }
1965 }
1966
1967 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1968 SkipUntil(tok::r_square, false);
1969 Loc = Tok.getLocation();
1970 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1971 SkipUntil(tok::r_square, false);
1972
1973 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1974 return Attr;
1975}
1976
1977/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1978/// attribute.
1979///
1980/// FIXME: Simply returns an alignof() expression if the argument is a
1981/// type. Ideally, the type should be propagated directly into Sema.
1982///
1983/// [C++0x] 'align' '(' type-id ')'
1984/// [C++0x] 'align' '(' assignment-expression ')'
1985Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
1986 if (isTypeIdInParens()) {
1987 EnterExpressionEvaluationContext Unevaluated(Actions,
1988 Action::Unevaluated);
1989 SourceLocation TypeLoc = Tok.getLocation();
1990 TypeTy *Ty = ParseTypeName().get();
1991 SourceRange TypeRange(Start, Tok.getLocation());
1992 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
1993 TypeRange);
1994 } else
1995 return ParseConstantExpression();
1996}