blob: f96b3c7cb22d817447e4eeb61a9a483ec2ccedf1 [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.
Chris Lattnerb28317a2009-03-28 19:18:32 +000067 Action::AttrTy *AttrList = 0;
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.
Sean Huntbbd37c62009-11-21 08:43:09 +000072 AttrList = 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 =
94 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
95
96 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
97 PP.getSourceManager(),
98 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +000099
Sean Huntbbd37c62009-11-21 08:43:09 +0000100 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
101 CXX0XAttributeList Attr;
102 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
103 Attr = ParseCXX0XAttributes();
104 ParseExternalDeclaration(Attr);
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattner51448322009-03-29 14:02:43 +0000107 // Leave the namespace scope.
108 NamespaceScope.Exit();
109
Chris Lattner97144fc2009-04-02 04:16:50 +0000110 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
111 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000112
Chris Lattner97144fc2009-04-02 04:16:50 +0000113 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000114 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000115}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000116
Anders Carlssonf67606a2009-03-28 04:07:16 +0000117/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
118/// alias definition.
119///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000120Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000121 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000122 IdentifierInfo *Alias,
123 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000124 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Anders Carlssonf67606a2009-03-28 04:07:16 +0000126 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000128 if (Tok.is(tok::code_completion)) {
129 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
130 ConsumeToken();
131 }
132
Anders Carlssonf67606a2009-03-28 04:07:16 +0000133 CXXScopeSpec SS;
134 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000135 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000136
137 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
138 Diag(Tok, diag::err_expected_namespace_name);
139 // Skip to end of the definition and eat the ';'.
140 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000141 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000142 }
143
144 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000145 IdentifierInfo *Ident = Tok.getIdentifierInfo();
146 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anders Carlssonf67606a2009-03-28 04:07:16 +0000148 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000149 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000150 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
151 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
153 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000154 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000155}
156
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000157/// ParseLinkage - We know that the current token is a string_literal
158/// and just before that, that extern was seen.
159///
160/// linkage-specification: [C++ 7.5p2: dcl.link]
161/// 'extern' string-literal '{' declaration-seq[opt] '}'
162/// 'extern' string-literal declaration
163///
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000164Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
165 unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000166 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000167 llvm::SmallVector<char, 8> LangBuffer;
168 // LangBuffer is guaranteed to be big enough.
169 LangBuffer.resize(Tok.getLength());
170 const char *LangBufPtr = &LangBuffer[0];
171 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
172
173 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000174
Douglas Gregor074149e2009-01-05 19:45:36 +0000175 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000176 DeclPtrTy LinkageSpec
177 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor074149e2009-01-05 19:45:36 +0000178 /*FIXME: */SourceLocation(),
179 Loc, LangBufPtr, StrSize,
Mike Stump1eb44332009-09-09 15:08:12 +0000180 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000181 : SourceLocation());
182
Sean Huntbbd37c62009-11-21 08:43:09 +0000183 CXX0XAttributeList Attr;
184 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
185 Attr = ParseCXX0XAttributes();
186 }
187
Douglas Gregor074149e2009-01-05 19:45:36 +0000188 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000189 ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000190 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000191 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000192 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000193
Sean Huntbbd37c62009-11-21 08:43:09 +0000194 if (Attr.HasAttr)
195 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
196 << Attr.Range;
197
Douglas Gregorf44515a2008-12-16 22:23:02 +0000198 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000199 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000200 CXX0XAttributeList Attr;
201 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
202 Attr = ParseCXX0XAttributes();
203 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000204 }
205
Douglas Gregorf44515a2008-12-16 22:23:02 +0000206 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000207 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000208}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000209
Douglas Gregorf780abc2008-12-30 03:27:21 +0000210/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
211/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000212Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000213 SourceLocation &DeclEnd,
214 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000215 assert(Tok.is(tok::kw_using) && "Not using token");
216
217 // Eat 'using'.
218 SourceLocation UsingLoc = ConsumeToken();
219
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000220 if (Tok.is(tok::code_completion)) {
221 Actions.CodeCompleteUsing(CurScope);
222 ConsumeToken();
223 }
224
Chris Lattner2f274772009-01-06 06:55:51 +0000225 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000226 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-11-21 08:43:09 +0000227 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
228
229 if (Attr.HasAttr)
230 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
231 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000232
233 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000234 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000235 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000236}
237
238/// ParseUsingDirective - Parse C++ using-directive, assumes
239/// that current token is 'namespace' and 'using' was already parsed.
240///
241/// using-directive: [C++ 7.3.p4: namespace.udir]
242/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
243/// namespace-name ;
244/// [GNU] using-directive:
245/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
246/// namespace-name attributes[opt] ;
247///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000248Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000249 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000250 SourceLocation &DeclEnd,
251 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000252 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
253
254 // Eat 'namespace'.
255 SourceLocation NamespcLoc = ConsumeToken();
256
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000257 if (Tok.is(tok::code_completion)) {
258 Actions.CodeCompleteUsingDirective(CurScope);
259 ConsumeToken();
260 }
261
Douglas Gregorf780abc2008-12-30 03:27:21 +0000262 CXXScopeSpec SS;
263 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000264 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000265
Douglas Gregorf780abc2008-12-30 03:27:21 +0000266 IdentifierInfo *NamespcName = 0;
267 SourceLocation IdentLoc = SourceLocation();
268
269 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000270 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000271 Diag(Tok, diag::err_expected_namespace_name);
272 // If there was invalid namespace name, skip to end of decl, and eat ';'.
273 SkipUntil(tok::semi);
274 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000275 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000276 }
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner823c44e2009-01-06 07:27:21 +0000278 // Parse identifier.
279 NamespcName = Tok.getIdentifierInfo();
280 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Chris Lattner823c44e2009-01-06 07:27:21 +0000282 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000283 bool GNUAttr = false;
284 if (Tok.is(tok::kw___attribute)) {
285 GNUAttr = true;
286 Attr = addAttributeLists(Attr, ParseGNUAttributes());
287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattner823c44e2009-01-06 07:27:21 +0000289 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000290 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000291 ExpectAndConsume(tok::semi,
Sean Huntbbd37c62009-11-21 08:43:09 +0000292 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner6869d8e2009-06-14 00:07:48 +0000293 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000294
295 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000296 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000297}
298
299/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
300/// 'using' was already seen.
301///
302/// using-declaration: [C++ 7.3.p3: namespace.udecl]
303/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000304/// unqualified-id
305/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000306///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000307Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000308 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000309 SourceLocation &DeclEnd,
310 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000311 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000312 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000313 bool IsTypeName;
314
315 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000316 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000317 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000318 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000319 ConsumeToken();
320 IsTypeName = true;
321 }
322 else
323 IsTypeName = false;
324
325 // Parse nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000326 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000327
328 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000329
330 // Check nested-name specifier.
331 if (SS.isInvalid()) {
332 SkipUntil(tok::semi);
333 return DeclPtrTy();
334 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000335
336 // Parse the unqualified-id. We allow parsing of both constructor and
337 // destructor names and allow the action module to diagnose any semantic
338 // errors.
339 UnqualifiedId Name;
340 if (ParseUnqualifiedId(SS,
341 /*EnteringContext=*/false,
342 /*AllowDestructorName=*/true,
343 /*AllowConstructorName=*/true,
344 /*ObjectType=*/0,
345 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000346 SkipUntil(tok::semi);
347 return DeclPtrTy();
348 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000349
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000350 // Parse (optional) attributes (most likely GNU strong-using extension).
351 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000352 AttrList = 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
Douglas Gregor12c118a2009-11-04 16:30:06 +0000360 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS, Name,
John McCall7ba107a2009-11-18 02:36:19 +0000361 AttrList, 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,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000465 const CXXScopeSpec *SS,
466 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000467 // Check whether we have a template-id that names a type.
468 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000469 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000470 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000471 if (TemplateId->Kind == TNK_Type_template) {
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
492 // We have an identifier; check whether it is actually a type.
Mike Stump1eb44332009-09-09 15:08:12 +0000493 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor42c39f32009-08-26 18:27:52 +0000494 Tok.getLocation(), CurScope, SS,
495 true);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000496 if (!Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000497 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000498 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000499 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000500 }
501
502 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000503 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000504 return Type;
505}
506
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000507/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
508/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
509/// until we reach the start of a definition or see a token that
510/// cannot start a definition.
511///
512/// class-specifier: [C++ class]
513/// class-head '{' member-specification[opt] '}'
514/// class-head '{' member-specification[opt] '}' attributes[opt]
515/// class-head:
516/// class-key identifier[opt] base-clause[opt]
517/// class-key nested-name-specifier identifier base-clause[opt]
518/// class-key nested-name-specifier[opt] simple-template-id
519/// base-clause[opt]
520/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000521/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000522/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000523/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000524/// simple-template-id base-clause[opt]
525/// class-key:
526/// 'class'
527/// 'struct'
528/// 'union'
529///
530/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000531/// class-key ::[opt] nested-name-specifier[opt] identifier
532/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
533/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000534///
535/// Note that the C++ class-specifier and elaborated-type-specifier,
536/// together, subsume the C99 struct-or-union-specifier:
537///
538/// struct-or-union-specifier: [C99 6.7.2.1]
539/// struct-or-union identifier[opt] '{' struct-contents '}'
540/// struct-or-union identifier
541/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
542/// '}' attributes[opt]
543/// [GNU] struct-or-union attributes[opt] identifier
544/// struct-or-union:
545/// 'struct'
546/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000547void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
548 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000549 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000550 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000551 DeclSpec::TST TagType;
552 if (TagTokKind == tok::kw_struct)
553 TagType = DeclSpec::TST_struct;
554 else if (TagTokKind == tok::kw_class)
555 TagType = DeclSpec::TST_class;
556 else {
557 assert(TagTokKind == tok::kw_union && "Not a class specifier");
558 TagType = DeclSpec::TST_union;
559 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000560
Douglas Gregor374929f2009-09-18 15:37:17 +0000561 if (Tok.is(tok::code_completion)) {
562 // Code completion for a struct, class, or union name.
563 Actions.CodeCompleteTag(CurScope, TagType);
564 ConsumeToken();
565 }
566
Sean Huntbbd37c62009-11-21 08:43:09 +0000567 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000568 // If attributes exist after tag, parse them.
569 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000570 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000571
Steve Narofff59e17e2008-12-24 20:59:21 +0000572 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000573 if (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000574 AttrList = ParseMicrosoftDeclSpec(AttrList);
575
576 // If C++0x attributes exist here, parse them.
577 // FIXME: Are we consistent with the ordering of parsing of different
578 // styles of attributes?
579 if (isCXX0XAttributeSpecifier())
580 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Douglas Gregorb117a602009-09-04 05:53:02 +0000582 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
583 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
584 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000585 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000586 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
587 // properly.
588 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
589 Tok.setKind(tok::identifier);
590 }
591
592 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
593 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
594 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000595 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000596 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
597 // properly.
598 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
599 Tok.setKind(tok::identifier);
600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000602 // Parse the (optional) nested-name-specifier.
603 CXXScopeSpec SS;
Mike Stump1eb44332009-09-09 15:08:12 +0000604 if (getLang().CPlusPlus &&
Chris Lattner46646492009-12-07 01:36:53 +0000605 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true, true))
Douglas Gregor39a8de12009-02-25 19:37:18 +0000606 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000607 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000608
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000609 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
610
Douglas Gregorcc636682009-02-17 23:15:12 +0000611 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000612 IdentifierInfo *Name = 0;
613 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000614 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000615 if (Tok.is(tok::identifier)) {
616 Name = Tok.getIdentifierInfo();
617 NameLoc = ConsumeToken();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000618
619 if (Tok.is(tok::less)) {
620 // The name was supposed to refer to a template, but didn't.
621 // Eat the template argument list and try to continue parsing this as
622 // a class (or template thereof).
623 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000624 SourceLocation LAngleLoc, RAngleLoc;
625 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
626 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000627 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000628 // We couldn't parse the template argument list at all, so don't
629 // try to give any location information for the list.
630 LAngleLoc = RAngleLoc = SourceLocation();
631 }
632
633 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000634 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000635 << (TagType == DeclSpec::TST_class? 0
636 : TagType == DeclSpec::TST_struct? 1
637 : 2)
638 << Name
639 << SourceRange(LAngleLoc, RAngleLoc);
640
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000641 // Strip off the last template parameter list if it was empty, since
642 // we've removed its template argument list.
643 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
644 if (TemplateParams && TemplateParams->size() > 1) {
645 TemplateParams->pop_back();
646 } else {
647 TemplateParams = 0;
648 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
649 = ParsedTemplateInfo::NonTemplate;
650 }
651 } else if (TemplateInfo.Kind
652 == ParsedTemplateInfo::ExplicitInstantiation) {
653 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000654 TemplateParams = 0;
655 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
656 = ParsedTemplateInfo::NonTemplate;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000657 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
658 = SourceLocation();
659 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
660 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000661 }
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000662
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000663
664 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000665 } else if (Tok.is(tok::annot_template_id)) {
666 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
667 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000668
Douglas Gregorc45c2322009-03-31 00:43:58 +0000669 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000670 // The template-name in the simple-template-id refers to
671 // something other than a class template. Give an appropriate
672 // error message and skip to the ';'.
673 SourceRange Range(NameLoc);
674 if (SS.isNotEmpty())
675 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000676
Douglas Gregor39a8de12009-02-25 19:37:18 +0000677 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
678 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Douglas Gregor39a8de12009-02-25 19:37:18 +0000680 DS.SetTypeSpecError();
681 SkipUntil(tok::semi, false, true);
682 TemplateId->Destroy();
683 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000684 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000685 }
686
John McCall67d1a672009-08-06 02:15:43 +0000687 // There are four options here. If we have 'struct foo;', then this
688 // is either a forward declaration or a friend declaration, which
689 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000690 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000691 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000692 Action::TagUseKind TUK;
Douglas Gregord85bea22009-09-26 06:47:28 +0000693 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
694 if (DS.isFriendSpecified()) {
695 // C++ [class.friend]p2:
696 // A class shall not be defined in a friend declaration.
697 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
698 << SourceRange(DS.getFriendSpecLoc());
699
700 // Skip everything up to the semicolon, so that this looks like a proper
701 // friend class (or template thereof) declaration.
702 SkipUntil(tok::semi, true, true);
703 TUK = Action::TUK_Friend;
704 } else {
705 // Okay, this is a class definition.
706 TUK = Action::TUK_Definition;
707 }
708 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000709 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000710 else
John McCall0f434ec2009-07-31 02:45:11 +0000711 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000712
John McCall0f434ec2009-07-31 02:45:11 +0000713 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000714 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000715 Diag(StartLoc, diag::err_anon_type_definition)
716 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000717
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000718 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000719
720 if (TemplateId)
721 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000722 return;
723 }
724
Douglas Gregorddc29e12009-02-06 22:42:48 +0000725 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000726 Action::DeclResult TagOrTempResult = true; // invalid
727 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000728
John McCall0f434ec2009-07-31 02:45:11 +0000729 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000730 // to turn that template-id into a type.
731
Douglas Gregor402abb52009-05-28 23:31:59 +0000732 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000733 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000734 // Explicit specialization, class template partial specialization,
735 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000736 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000737 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000738 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000739 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000740 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000741 // This is an explicit instantiation of a class template.
742 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000743 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000744 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000745 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000746 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000747 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000748 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000749 TemplateTy::make(TemplateId->Template),
750 TemplateId->TemplateNameLoc,
751 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000752 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000753 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000754 AttrList);
Douglas Gregorfc9cd612009-09-26 20:57:03 +0000755 } else if (TUK == Action::TUK_Reference) {
John McCallc4e70192009-09-11 04:59:25 +0000756 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000757 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
758 TemplateId->TemplateNameLoc,
759 TemplateId->LAngleLoc,
760 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000761 TemplateId->RAngleLoc);
762
John McCallc4e70192009-09-11 04:59:25 +0000763 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
764 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000765 } else {
766 // This is an explicit specialization or a class template
767 // partial specialization.
768 TemplateParameterLists FakedParamLists;
769
770 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
771 // This looks like an explicit instantiation, because we have
772 // something like
773 //
774 // template class Foo<X>
775 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000776 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000777 // meant to be an explicit specialization, but the user forgot
778 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000779 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000780
Mike Stump1eb44332009-09-09 15:08:12 +0000781 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000782 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000783 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000784 diag::err_explicit_instantiation_with_definition)
785 << SourceRange(TemplateInfo.TemplateLoc)
786 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
787
788 // Create a fake template parameter list that contains only
789 // "template<>", so that we treat this construct as a class
790 // template specialization.
791 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000792 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000793 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000794 LAngleLoc,
795 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000796 LAngleLoc));
797 TemplateParams = &FakedParamLists;
798 }
799
800 // Build the class template specialization.
801 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000802 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000803 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000804 TemplateTy::make(TemplateId->Template),
805 TemplateId->TemplateNameLoc,
806 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000807 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000808 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000809 AttrList,
Mike Stump1eb44332009-09-09 15:08:12 +0000810 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000811 TemplateParams? &(*TemplateParams)[0] : 0,
812 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000813 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000814 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000815 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000816 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000817 // Explicit instantiation of a member of a class template
818 // specialization, e.g.,
819 //
820 // template struct Outer<int>::Inner;
821 //
822 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000823 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000824 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000825 TemplateInfo.TemplateLoc,
826 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000827 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000828 } else {
829 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000830 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000831 // FIXME: Diagnose this particular error.
832 }
833
John McCallc4e70192009-09-11 04:59:25 +0000834 bool IsDependent = false;
835
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000836 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000837 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000838 Name, NameLoc, AttrList, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000839 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000840 TemplateParams? &(*TemplateParams)[0] : 0,
841 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000842 Owned, IsDependent);
843
844 // If ActOnTag said the type was dependent, try again with the
845 // less common call.
846 if (IsDependent)
847 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
848 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000849 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000850
851 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000852 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000853 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000854
855 // If there is a body, parse it and inform the actions module.
856 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000857 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000858 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000859 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000860 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000861 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000862 // FIXME: Complain that we have a base-specifier list but no
863 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000864 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000865 }
866
John McCallc4e70192009-09-11 04:59:25 +0000867 void *Result;
868 if (!TypeResult.isInvalid()) {
869 TagType = DeclSpec::TST_typename;
870 Result = TypeResult.get();
871 Owned = false;
872 } else if (!TagOrTempResult.isInvalid()) {
873 Result = TagOrTempResult.get().getAs<void>();
874 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000875 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000876 return;
877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
John McCallfec54012009-08-03 20:12:06 +0000879 const char *PrevSpec = 0;
880 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000881
John McCallfec54012009-08-03 20:12:06 +0000882 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000883 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000884 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000885}
886
Mike Stump1eb44332009-09-09 15:08:12 +0000887/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000888///
889/// base-clause : [C++ class.derived]
890/// ':' base-specifier-list
891/// base-specifier-list:
892/// base-specifier '...'[opt]
893/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000894void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000895 assert(Tok.is(tok::colon) && "Not a base clause");
896 ConsumeToken();
897
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000898 // Build up an array of parsed base specifiers.
899 llvm::SmallVector<BaseTy *, 8> BaseInfo;
900
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000901 while (true) {
902 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000903 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000904 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000905 // Skip the rest of this base specifier, up until the comma or
906 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000907 SkipUntil(tok::comma, tok::l_brace, true, true);
908 } else {
909 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000910 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000911 }
912
913 // If the next token is a comma, consume it and keep reading
914 // base-specifiers.
915 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000917 // Consume the comma.
918 ConsumeToken();
919 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000920
921 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000922 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000923}
924
925/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
926/// one entry in the base class list of a class specifier, for example:
927/// class foo : public bar, virtual private baz {
928/// 'public bar' and 'virtual private baz' are each base-specifiers.
929///
930/// base-specifier: [C++ class.derived]
931/// ::[opt] nested-name-specifier[opt] class-name
932/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
933/// class-name
934/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
935/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000936Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000937 bool IsVirtual = false;
938 SourceLocation StartLoc = Tok.getLocation();
939
940 // Parse the 'virtual' keyword.
941 if (Tok.is(tok::kw_virtual)) {
942 ConsumeToken();
943 IsVirtual = true;
944 }
945
946 // Parse an (optional) access specifier.
947 AccessSpecifier Access = getAccessSpecifierIfPresent();
948 if (Access)
949 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000951 // Parse the 'virtual' keyword (again!), in case it came after the
952 // access specifier.
953 if (Tok.is(tok::kw_virtual)) {
954 SourceLocation VirtualLoc = ConsumeToken();
955 if (IsVirtual) {
956 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000957 Diag(VirtualLoc, diag::err_dup_virtual)
Chris Lattner29d9c1a2009-12-06 17:36:05 +0000958 << CodeModificationHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000959 }
960
961 IsVirtual = true;
962 }
963
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000964 // Parse optional '::' and optional nested-name-specifier.
965 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000966 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000967
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000968 // The location of the base class itself.
969 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000970
971 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000972 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000973 TypeResult BaseType = ParseClassName(EndLocation, &SS);
974 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000975 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
977 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000978 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000980 // Notify semantic analysis that we have parsed a complete
981 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000982 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000983 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000984}
985
986/// getAccessSpecifierIfPresent - Determine whether the next token is
987/// a C++ access-specifier.
988///
989/// access-specifier: [C++ class.derived]
990/// 'private'
991/// 'protected'
992/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +0000993AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000994 switch (Tok.getKind()) {
995 default: return AS_none;
996 case tok::kw_private: return AS_private;
997 case tok::kw_protected: return AS_protected;
998 case tok::kw_public: return AS_public;
999 }
1000}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001001
Eli Friedmand33133c2009-07-22 21:45:50 +00001002void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1003 DeclPtrTy ThisDecl) {
1004 // We just declared a member function. If this member function
1005 // has any default arguments, we'll need to parse them later.
1006 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001007 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001008 = DeclaratorInfo.getTypeObject(0).Fun;
1009 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1010 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1011 if (!LateMethod) {
1012 // Push this method onto the stack of late-parsed method
1013 // declarations.
1014 getCurrentClass().MethodDecls.push_back(
1015 LateParsedMethodDeclaration(ThisDecl));
1016 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +00001017 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001018
1019 // Add all of the parameters prior to this one (they don't
1020 // have default arguments).
1021 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1022 for (unsigned I = 0; I < ParamIdx; ++I)
1023 LateMethod->DefaultArgs.push_back(
1024 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1025 }
1026
1027 // Add this parameter to the list of parameters (it or may
1028 // not have a default argument).
1029 LateMethod->DefaultArgs.push_back(
1030 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1031 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1032 }
1033 }
1034}
1035
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001036/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1037///
1038/// member-declaration:
1039/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1040/// function-definition ';'[opt]
1041/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1042/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001043/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001044/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001045/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001046///
1047/// member-declarator-list:
1048/// member-declarator
1049/// member-declarator-list ',' member-declarator
1050///
1051/// member-declarator:
1052/// declarator pure-specifier[opt]
1053/// declarator constant-initializer[opt]
1054/// identifier[opt] ':' constant-expression
1055///
Sebastian Redle2b68332009-04-12 17:16:29 +00001056/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001057/// '= 0'
1058///
1059/// constant-initializer:
1060/// '=' constant-expression
1061///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001062void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1063 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001064 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001065 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001066 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001067 SourceLocation DeclEnd;
1068 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001069 return;
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattner682bf922009-03-29 16:50:03 +00001072 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001073 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001074 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001075 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001076 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001077 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001078 return;
1079 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001080
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001081 // Handle: member-declaration ::= '__extension__' member-declaration
1082 if (Tok.is(tok::kw___extension__)) {
1083 // __extension__ silences extension warnings in the subexpression.
1084 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1085 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001086 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001087 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001088
Sean Huntbbd37c62009-11-21 08:43:09 +00001089 CXX0XAttributeList AttrList;
1090 // Optional C++0x attribute-specifier
1091 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1092 AttrList = ParseCXX0XAttributes();
1093 }
1094
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001095 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001096 // FIXME: Check for template aliases
Sean Huntbbd37c62009-11-21 08:43:09 +00001097
1098 if (AttrList.HasAttr)
1099 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1100 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001102 // Eat 'using'.
1103 SourceLocation UsingLoc = ConsumeToken();
1104
1105 if (Tok.is(tok::kw_namespace)) {
1106 Diag(UsingLoc, diag::err_using_namespace_in_class);
1107 SkipUntil(tok::semi, true, true);
1108 }
1109 else {
1110 SourceLocation DeclEnd;
1111 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001112 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001113 }
1114 return;
1115 }
1116
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001117 SourceLocation DSStart = Tok.getLocation();
1118 // decl-specifier-seq:
1119 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001120 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +00001121 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001122 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001123
John McCalldd4a3b02009-09-16 22:47:08 +00001124 Action::MultiTemplateParamsArg TemplateParams(Actions,
1125 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1126 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1127
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001128 if (Tok.is(tok::semi)) {
1129 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001130 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001131 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001132 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001133
John McCall54abf7d2009-11-04 02:18:39 +00001134 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001135
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001136 if (Tok.isNot(tok::colon)) {
1137 // Parse the first declarator.
1138 ParseDeclarator(DeclaratorInfo);
1139 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001140 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001141 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001142 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001143 if (Tok.is(tok::semi))
1144 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001145 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001146 }
1147
John Thompson1b2fc0f2009-11-25 22:58:06 +00001148 // If attributes exist after the declarator, but before an '{', parse them.
1149 if (Tok.is(tok::kw___attribute)) {
1150 SourceLocation Loc;
1151 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1152 DeclaratorInfo.AddAttributes(AttrList, Loc);
1153 }
1154
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001155 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001156 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001157 || (DeclaratorInfo.isFunctionDeclarator() &&
1158 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001159 if (!DeclaratorInfo.isFunctionDeclarator()) {
1160 Diag(Tok, diag::err_func_def_no_params);
1161 ConsumeBrace();
1162 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001163 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001164 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001165
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001166 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1167 Diag(Tok, diag::err_function_declared_typedef);
1168 // This recovery skips the entire function body. It would be nice
1169 // to simply call ParseCXXInlineMethodDef() below, however Sema
1170 // assumes the declarator represents a function, not a typedef.
1171 ConsumeBrace();
1172 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001173 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001174 }
1175
Douglas Gregor37b372b2009-08-20 22:52:58 +00001176 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001177 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001178 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001179 }
1180
1181 // member-declarator-list:
1182 // member-declarator
1183 // member-declarator-list ',' member-declarator
1184
Chris Lattner682bf922009-03-29 16:50:03 +00001185 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001186 OwningExprResult BitfieldSize(Actions);
1187 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001188 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001189
1190 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001191 // member-declarator:
1192 // declarator pure-specifier[opt]
1193 // declarator constant-initializer[opt]
1194 // identifier[opt] ':' constant-expression
1195
1196 if (Tok.is(tok::colon)) {
1197 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001198 BitfieldSize = ParseConstantExpression();
1199 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001200 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001203 // pure-specifier:
1204 // '= 0'
1205 //
1206 // constant-initializer:
1207 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001208 //
1209 // defaulted/deleted function-definition:
1210 // '=' 'default' [TODO]
1211 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001212
1213 if (Tok.is(tok::equal)) {
1214 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001215 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1216 ConsumeToken();
1217 Deleted = true;
1218 } else {
1219 Init = ParseInitializer();
1220 if (Init.isInvalid())
1221 SkipUntil(tok::comma, true, true);
1222 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001223 }
1224
1225 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001226 if (Tok.is(tok::kw___attribute)) {
1227 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001228 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001229 DeclaratorInfo.AddAttributes(AttrList, Loc);
1230 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001231
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001232 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001233 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001234 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001235
1236 DeclPtrTy ThisDecl;
1237 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001238 // TODO: handle initializers, bitfields, 'delete'
1239 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1240 /*IsDefinition*/ false,
1241 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001242 } else {
John McCall67d1a672009-08-06 02:15:43 +00001243 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1244 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001245 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001246 BitfieldSize.release(),
1247 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001248 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001249 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001250 }
Chris Lattner682bf922009-03-29 16:50:03 +00001251 if (ThisDecl)
1252 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001253
Douglas Gregor72b505b2008-12-16 21:30:33 +00001254 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001255 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001256 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001257 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001258 }
1259
John McCall54abf7d2009-11-04 02:18:39 +00001260 DeclaratorInfo.complete(ThisDecl);
1261
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001262 // If we don't have a comma, it is either the end of the list (a ';')
1263 // or an error, bail out.
1264 if (Tok.isNot(tok::comma))
1265 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001267 // Consume the comma.
1268 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001270 // Parse the next declarator.
1271 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001272 BitfieldSize = 0;
1273 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001274 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001276 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001277 if (Tok.is(tok::kw___attribute)) {
1278 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001279 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001280 DeclaratorInfo.AddAttributes(AttrList, Loc);
1281 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001282
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001283 if (Tok.isNot(tok::colon))
1284 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001285 }
1286
1287 if (Tok.is(tok::semi)) {
1288 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001289 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001290 DeclsInGroup.size());
1291 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001292 }
1293
1294 Diag(Tok, diag::err_expected_semi_decl_list);
1295 // Skip to end of block or statement
1296 SkipUntil(tok::r_brace, true, true);
1297 if (Tok.is(tok::semi))
1298 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001299 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001300}
1301
1302/// ParseCXXMemberSpecification - Parse the class definition.
1303///
1304/// member-specification:
1305/// member-declaration member-specification[opt]
1306/// access-specifier ':' member-specification[opt]
1307///
1308void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001309 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001310 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001311 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001312 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001313
Chris Lattner49f28ca2009-03-05 08:00:35 +00001314 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1315 PP.getSourceManager(),
1316 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001318 SourceLocation LBraceLoc = ConsumeBrace();
1319
Douglas Gregor6569d682009-05-27 23:11:45 +00001320 // Determine whether this is a top-level (non-nested) class.
Mike Stump1eb44332009-09-09 15:08:12 +00001321 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregor6569d682009-05-27 23:11:45 +00001322 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001323
1324 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001325 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001326
Douglas Gregor6569d682009-05-27 23:11:45 +00001327 // Note that we are parsing a new (potentially-nested) class definition.
1328 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1329
Douglas Gregorddc29e12009-02-06 22:42:48 +00001330 if (TagDecl)
1331 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1332 else {
1333 SkipUntil(tok::r_brace, false, false);
1334 return;
1335 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001336
1337 // C++ 11p3: Members of a class defined with the keyword class are private
1338 // by default. Members of a class defined with the keywords struct or union
1339 // are public by default.
1340 AccessSpecifier CurAS;
1341 if (TagType == DeclSpec::TST_class)
1342 CurAS = AS_private;
1343 else
1344 CurAS = AS_public;
1345
1346 // While we still have something to read, read the member-declarations.
1347 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1348 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001350 // Check for extraneous top-level semicolon.
1351 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001352 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001353 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001354 ConsumeToken();
1355 continue;
1356 }
1357
1358 AccessSpecifier AS = getAccessSpecifierIfPresent();
1359 if (AS != AS_none) {
1360 // Current token is a C++ access specifier.
1361 CurAS = AS;
1362 ConsumeToken();
1363 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1364 continue;
1365 }
1366
Douglas Gregor37b372b2009-08-20 22:52:58 +00001367 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001369 // Parse all the comma separated declarators.
1370 ParseCXXClassMemberDeclaration(CurAS);
1371 }
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001373 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001375 AttributeList *AttrList = 0;
1376 // If attributes exist after class contents, parse them.
1377 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001378 AttrList = ParseGNUAttributes(); // FIXME: where should I put them?
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001379
1380 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1381 LBraceLoc, RBraceLoc);
1382
1383 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1384 // complete within function bodies, default arguments,
1385 // exception-specifications, and constructor ctor-initializers (including
1386 // such things in nested classes).
1387 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001388 // FIXME: Only function bodies and constructor ctor-initializers are
1389 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001390 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001391 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001392 // are complete and we can parse the delayed portions of method
1393 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001394 ParseLexedMethodDeclarations(getCurrentClass());
1395 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001396 }
1397
1398 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001399 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001400 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001401
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001402 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001403}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001404
1405/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1406/// which explicitly initializes the members or base classes of a
1407/// class (C++ [class.base.init]). For example, the three initializers
1408/// after the ':' in the Derived constructor below:
1409///
1410/// @code
1411/// class Base { };
1412/// class Derived : Base {
1413/// int x;
1414/// float f;
1415/// public:
1416/// Derived(float f) : Base(), x(17), f(f) { }
1417/// };
1418/// @endcode
1419///
Mike Stump1eb44332009-09-09 15:08:12 +00001420/// [C++] ctor-initializer:
1421/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001422///
Mike Stump1eb44332009-09-09 15:08:12 +00001423/// [C++] mem-initializer-list:
1424/// mem-initializer
1425/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001426void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001427 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1428
1429 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Douglas Gregor7ad83902008-11-05 04:29:56 +00001431 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Douglas Gregor7ad83902008-11-05 04:29:56 +00001433 do {
1434 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001435 if (!MemInit.isInvalid())
1436 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001437
1438 if (Tok.is(tok::comma))
1439 ConsumeToken();
1440 else if (Tok.is(tok::l_brace))
1441 break;
1442 else {
1443 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001444 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001445 SkipUntil(tok::l_brace, true, true);
1446 break;
1447 }
1448 } while (true);
1449
Mike Stump1eb44332009-09-09 15:08:12 +00001450 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001451 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001452}
1453
1454/// ParseMemInitializer - Parse a C++ member initializer, which is
1455/// part of a constructor initializer that explicitly initializes one
1456/// member or base class (C++ [class.base.init]). See
1457/// ParseConstructorInitializer for an example.
1458///
1459/// [C++] mem-initializer:
1460/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001461///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001462/// [C++] mem-initializer-id:
1463/// '::'[opt] nested-name-specifier[opt] class-name
1464/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001465Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001466 // parse '::'[opt] nested-name-specifier[opt]
1467 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001468 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001469 TypeTy *TemplateTypeTy = 0;
1470 if (Tok.is(tok::annot_template_id)) {
1471 TemplateIdAnnotation *TemplateId
1472 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1473 if (TemplateId->Kind == TNK_Type_template) {
1474 AnnotateTemplateIdTokenAsType(&SS);
1475 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1476 TemplateTypeTy = Tok.getAnnotationValue();
1477 }
1478 // FIXME. May need to check for TNK_Dependent_template as well.
1479 }
1480 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001481 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001482 return true;
1483 }
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Douglas Gregor7ad83902008-11-05 04:29:56 +00001485 // Get the identifier. This may be a member name or a class name,
1486 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001487 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001488 SourceLocation IdLoc = ConsumeToken();
1489
1490 // Parse the '('.
1491 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001492 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001493 return true;
1494 }
1495 SourceLocation LParenLoc = ConsumeParen();
1496
1497 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001498 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001499 CommaLocsTy CommaLocs;
1500 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1501 SkipUntil(tok::r_paren);
1502 return true;
1503 }
1504
1505 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1506
Fariborz Jahanian96174332009-07-01 19:21:19 +00001507 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1508 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001509 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001510 ArgExprs.size(), CommaLocs.data(),
1511 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001512}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001513
1514/// ParseExceptionSpecification - Parse a C++ exception-specification
1515/// (C++ [except.spec]).
1516///
Douglas Gregora4745612008-12-01 18:00:20 +00001517/// exception-specification:
1518/// 'throw' '(' type-id-list [opt] ')'
1519/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001520///
Douglas Gregora4745612008-12-01 18:00:20 +00001521/// type-id-list:
1522/// type-id
1523/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001524///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001525bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001526 llvm::SmallVector<TypeTy*, 2>
1527 &Exceptions,
1528 llvm::SmallVector<SourceRange, 2>
1529 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001530 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001531 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001533 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001535 if (!Tok.is(tok::l_paren)) {
1536 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1537 }
1538 SourceLocation LParenLoc = ConsumeParen();
1539
Douglas Gregora4745612008-12-01 18:00:20 +00001540 // Parse throw(...), a Microsoft extension that means "this function
1541 // can throw anything".
1542 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001543 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001544 SourceLocation EllipsisLoc = ConsumeToken();
1545 if (!getLang().Microsoft)
1546 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001547 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001548 return false;
1549 }
1550
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001551 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001552 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001553 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001554 TypeResult Res(ParseTypeName(&Range));
1555 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001556 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001557 Ranges.push_back(Range);
1558 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001559 if (Tok.is(tok::comma))
1560 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001561 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001562 break;
1563 }
1564
Sebastian Redlab197ba2009-02-09 18:23:29 +00001565 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001566 return false;
1567}
Douglas Gregor6569d682009-05-27 23:11:45 +00001568
1569/// \brief We have just started parsing the definition of a new class,
1570/// so push that class onto our stack of classes that is currently
1571/// being parsed.
1572void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001573 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001574 "Nested class without outer class");
1575 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1576}
1577
1578/// \brief Deallocate the given parsed class and all of its nested
1579/// classes.
1580void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1581 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1582 DeallocateParsedClasses(Class->NestedClasses[I]);
1583 delete Class;
1584}
1585
1586/// \brief Pop the top class of the stack of classes that are
1587/// currently being parsed.
1588///
1589/// This routine should be called when we have finished parsing the
1590/// definition of a class, but have not yet popped the Scope
1591/// associated with the class's definition.
1592///
1593/// \returns true if the class we've popped is a top-level class,
1594/// false otherwise.
1595void Parser::PopParsingClass() {
1596 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Douglas Gregor6569d682009-05-27 23:11:45 +00001598 ParsingClass *Victim = ClassStack.top();
1599 ClassStack.pop();
1600 if (Victim->TopLevelClass) {
1601 // Deallocate all of the nested classes of this class,
1602 // recursively: we don't need to keep any of this information.
1603 DeallocateParsedClasses(Victim);
1604 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001605 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001606 assert(!ClassStack.empty() && "Missing top-level class?");
1607
1608 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1609 Victim->NestedClasses.empty()) {
1610 // The victim is a nested class, but we will not need to perform
1611 // any processing after the definition of this class since it has
1612 // no members whose handling was delayed. Therefore, we can just
1613 // remove this nested class.
1614 delete Victim;
1615 return;
1616 }
1617
1618 // This nested class has some members that will need to be processed
1619 // after the top-level class is completely defined. Therefore, add
1620 // it to the list of nested classes within its parent.
1621 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1622 ClassStack.top()->NestedClasses.push_back(Victim);
1623 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1624}
Sean Huntbbd37c62009-11-21 08:43:09 +00001625
1626/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1627/// parses standard attributes.
1628///
1629/// [C++0x] attribute-specifier:
1630/// '[' '[' attribute-list ']' ']'
1631///
1632/// [C++0x] attribute-list:
1633/// attribute[opt]
1634/// attribute-list ',' attribute[opt]
1635///
1636/// [C++0x] attribute:
1637/// attribute-token attribute-argument-clause[opt]
1638///
1639/// [C++0x] attribute-token:
1640/// identifier
1641/// attribute-scoped-token
1642///
1643/// [C++0x] attribute-scoped-token:
1644/// attribute-namespace '::' identifier
1645///
1646/// [C++0x] attribute-namespace:
1647/// identifier
1648///
1649/// [C++0x] attribute-argument-clause:
1650/// '(' balanced-token-seq ')'
1651///
1652/// [C++0x] balanced-token-seq:
1653/// balanced-token
1654/// balanced-token-seq balanced-token
1655///
1656/// [C++0x] balanced-token:
1657/// '(' balanced-token-seq ')'
1658/// '[' balanced-token-seq ']'
1659/// '{' balanced-token-seq '}'
1660/// any token but '(', ')', '[', ']', '{', or '}'
1661CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1662 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1663 && "Not a C++0x attribute list");
1664
1665 SourceLocation StartLoc = Tok.getLocation(), Loc;
1666 AttributeList *CurrAttr = 0;
1667
1668 ConsumeBracket();
1669 ConsumeBracket();
1670
1671 if (Tok.is(tok::comma)) {
1672 Diag(Tok.getLocation(), diag::err_expected_ident);
1673 ConsumeToken();
1674 }
1675
1676 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1677 // attribute not present
1678 if (Tok.is(tok::comma)) {
1679 ConsumeToken();
1680 continue;
1681 }
1682
1683 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1684 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
1685
1686 // scoped attribute
1687 if (Tok.is(tok::coloncolon)) {
1688 ConsumeToken();
1689
1690 if (!Tok.is(tok::identifier)) {
1691 Diag(Tok.getLocation(), diag::err_expected_ident);
1692 SkipUntil(tok::r_square, tok::comma, true, true);
1693 continue;
1694 }
1695
1696 ScopeName = AttrName;
1697 ScopeLoc = AttrLoc;
1698
1699 AttrName = Tok.getIdentifierInfo();
1700 AttrLoc = ConsumeToken();
1701 }
1702
1703 bool AttrParsed = false;
1704 // No scoped names are supported; ideally we could put all non-standard
1705 // attributes into namespaces.
1706 if (!ScopeName) {
1707 switch(AttributeList::getKind(AttrName))
1708 {
1709 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00001710 case AttributeList::AT_base_check:
1711 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00001712 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00001713 case AttributeList::AT_hiding:
1714 case AttributeList::AT_noreturn:
1715 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00001716 if (Tok.is(tok::l_paren)) {
1717 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1718 << AttrName->getName();
1719 break;
1720 }
1721
1722 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1723 SourceLocation(), 0, 0, CurrAttr, false,
1724 true);
1725 AttrParsed = true;
1726 break;
1727 }
1728
1729 // One argument; must be a type-id or assignment-expression
1730 case AttributeList::AT_aligned: {
1731 if (Tok.isNot(tok::l_paren)) {
1732 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1733 << AttrName->getName();
1734 break;
1735 }
1736 SourceLocation ParamLoc = ConsumeParen();
1737
1738 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1739
1740 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1741
1742 ExprVector ArgExprs(Actions);
1743 ArgExprs.push_back(ArgExpr.release());
1744 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1745 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1746 false, true);
1747
1748 AttrParsed = true;
1749 break;
1750 }
1751
1752 // Silence warnings
1753 default: break;
1754 }
1755 }
1756
1757 // Skip the entire parameter clause, if any
1758 if (!AttrParsed && Tok.is(tok::l_paren)) {
1759 ConsumeParen();
1760 // SkipUntil maintains the balancedness of tokens.
1761 SkipUntil(tok::r_paren, false);
1762 }
1763 }
1764
1765 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1766 SkipUntil(tok::r_square, false);
1767 Loc = Tok.getLocation();
1768 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1769 SkipUntil(tok::r_square, false);
1770
1771 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1772 return Attr;
1773}
1774
1775/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1776/// attribute.
1777///
1778/// FIXME: Simply returns an alignof() expression if the argument is a
1779/// type. Ideally, the type should be propagated directly into Sema.
1780///
1781/// [C++0x] 'align' '(' type-id ')'
1782/// [C++0x] 'align' '(' assignment-expression ')'
1783Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
1784 if (isTypeIdInParens()) {
1785 EnterExpressionEvaluationContext Unevaluated(Actions,
1786 Action::Unevaluated);
1787 SourceLocation TypeLoc = Tok.getLocation();
1788 TypeTy *Ty = ParseTypeName().get();
1789 SourceRange TypeRange(Start, Tok.getLocation());
1790 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
1791 TypeRange);
1792 } else
1793 return ParseConstantExpression();
1794}