blob: 154c2923486e14a4f629a996a8056dda70e97382 [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"
Chris Lattnerbc8d5642008-12-18 01:12:00 +000019#include "ExtensionRAIIObject.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000020using namespace clang;
21
22/// ParseNamespace - We know that the current token is a namespace keyword. This
23/// may either be a top level namespace or a block-level namespace alias.
24///
25/// namespace-definition: [C++ 7.3: basic.namespace]
26/// named-namespace-definition
27/// unnamed-namespace-definition
28///
29/// unnamed-namespace-definition:
30/// 'namespace' attributes[opt] '{' namespace-body '}'
31///
32/// named-namespace-definition:
33/// original-namespace-definition
34/// extension-namespace-definition
35///
36/// original-namespace-definition:
37/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
38///
39/// extension-namespace-definition:
40/// 'namespace' original-namespace-name '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000041///
Chris Lattner8f08cb72007-08-25 06:57:03 +000042/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
43/// 'namespace' identifier '=' qualified-namespace-specifier ';'
44///
Chris Lattner97144fc2009-04-02 04:16:50 +000045Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
46 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000047 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000048 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000049
Douglas Gregor49f40bd2009-09-18 19:03:04 +000050 if (Tok.is(tok::code_completion)) {
51 Actions.CodeCompleteNamespaceDecl(CurScope);
52 ConsumeToken();
53 }
54
Chris Lattner8f08cb72007-08-25 06:57:03 +000055 SourceLocation IdentLoc;
56 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000057
58 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000059
Chris Lattner04d66662007-10-09 17:33:22 +000060 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000061 Ident = Tok.getIdentifierInfo();
62 IdentLoc = ConsumeToken(); // eat the identifier.
63 }
Mike Stump1eb44332009-09-09 15:08:12 +000064
Chris Lattner8f08cb72007-08-25 06:57:03 +000065 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +000066 Action::AttrTy *AttrList = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000067 if (Tok.is(tok::kw___attribute)) {
68 attrTok = Tok;
69
Chris Lattner8f08cb72007-08-25 06:57:03 +000070 // FIXME: save these somewhere.
71 AttrList = ParseAttributes();
Douglas Gregor6a588dd2009-06-17 19:49:00 +000072 }
Mike Stump1eb44332009-09-09 15:08:12 +000073
Douglas Gregor6a588dd2009-06-17 19:49:00 +000074 if (Tok.is(tok::equal)) {
75 if (AttrList)
76 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
77
Chris Lattner97144fc2009-04-02 04:16:50 +000078 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000079 }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattner51448322009-03-29 14:02:43 +000081 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000082 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000083 diag::err_expected_ident_lbrace);
84 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000085 }
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattner51448322009-03-29 14:02:43 +000087 SourceLocation LBrace = ConsumeBrace();
88
89 // Enter a scope for the namespace.
90 ParseScope NamespaceScope(this, Scope::DeclScope);
91
92 DeclPtrTy NamespcDecl =
93 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
94
95 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
96 PP.getSourceManager(),
97 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +000098
Chris Lattner51448322009-03-29 14:02:43 +000099 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
100 ParseExternalDeclaration();
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Chris Lattner51448322009-03-29 14:02:43 +0000102 // Leave the namespace scope.
103 NamespaceScope.Exit();
104
Chris Lattner97144fc2009-04-02 04:16:50 +0000105 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
106 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000107
Chris Lattner97144fc2009-04-02 04:16:50 +0000108 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000109 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000110}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000111
Anders Carlssonf67606a2009-03-28 04:07:16 +0000112/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
113/// alias definition.
114///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000115Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000116 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000117 IdentifierInfo *Alias,
118 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000119 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Anders Carlssonf67606a2009-03-28 04:07:16 +0000121 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000123 if (Tok.is(tok::code_completion)) {
124 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
125 ConsumeToken();
126 }
127
Anders Carlssonf67606a2009-03-28 04:07:16 +0000128 CXXScopeSpec SS;
129 // Parse (optional) nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000130 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000131
132 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
133 Diag(Tok, diag::err_expected_namespace_name);
134 // Skip to end of the definition and eat the ';'.
135 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000136 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000137 }
138
139 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000140 IdentifierInfo *Ident = Tok.getIdentifierInfo();
141 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Anders Carlssonf67606a2009-03-28 04:07:16 +0000143 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000144 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000145 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
146 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
148 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000149 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000150}
151
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000152/// ParseLinkage - We know that the current token is a string_literal
153/// and just before that, that extern was seen.
154///
155/// linkage-specification: [C++ 7.5p2: dcl.link]
156/// 'extern' string-literal '{' declaration-seq[opt] '}'
157/// 'extern' string-literal declaration
158///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000159Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000160 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000161 llvm::SmallVector<char, 8> LangBuffer;
162 // LangBuffer is guaranteed to be big enough.
163 LangBuffer.resize(Tok.getLength());
164 const char *LangBufPtr = &LangBuffer[0];
165 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
166
167 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000168
Douglas Gregor074149e2009-01-05 19:45:36 +0000169 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000170 DeclPtrTy LinkageSpec
171 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor074149e2009-01-05 19:45:36 +0000172 /*FIXME: */SourceLocation(),
173 Loc, LangBufPtr, StrSize,
Mike Stump1eb44332009-09-09 15:08:12 +0000174 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000175 : SourceLocation());
176
177 if (Tok.isNot(tok::l_brace)) {
178 ParseDeclarationOrFunctionDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +0000179 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000180 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000181 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000182
183 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000184 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000185 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000186 }
187
Douglas Gregorf44515a2008-12-16 22:23:02 +0000188 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000189 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000190}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000191
Douglas Gregorf780abc2008-12-30 03:27:21 +0000192/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
193/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000194Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
195 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000196 assert(Tok.is(tok::kw_using) && "Not using token");
197
198 // Eat 'using'.
199 SourceLocation UsingLoc = ConsumeToken();
200
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000201 if (Tok.is(tok::code_completion)) {
202 Actions.CodeCompleteUsing(CurScope);
203 ConsumeToken();
204 }
205
Chris Lattner2f274772009-01-06 06:55:51 +0000206 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000207 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner97144fc2009-04-02 04:16:50 +0000208 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner2f274772009-01-06 06:55:51 +0000209
210 // Otherwise, it must be using-declaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000211 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000212}
213
214/// ParseUsingDirective - Parse C++ using-directive, assumes
215/// that current token is 'namespace' and 'using' was already parsed.
216///
217/// using-directive: [C++ 7.3.p4: namespace.udir]
218/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
219/// namespace-name ;
220/// [GNU] using-directive:
221/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
222/// namespace-name attributes[opt] ;
223///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000224Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000225 SourceLocation UsingLoc,
226 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000227 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
228
229 // Eat 'namespace'.
230 SourceLocation NamespcLoc = ConsumeToken();
231
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000232 if (Tok.is(tok::code_completion)) {
233 Actions.CodeCompleteUsingDirective(CurScope);
234 ConsumeToken();
235 }
236
Douglas Gregorf780abc2008-12-30 03:27:21 +0000237 CXXScopeSpec SS;
238 // Parse (optional) nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000239 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000240
241 AttributeList *AttrList = 0;
242 IdentifierInfo *NamespcName = 0;
243 SourceLocation IdentLoc = SourceLocation();
244
245 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000246 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000247 Diag(Tok, diag::err_expected_namespace_name);
248 // If there was invalid namespace name, skip to end of decl, and eat ';'.
249 SkipUntil(tok::semi);
250 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000251 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000252 }
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Chris Lattner823c44e2009-01-06 07:27:21 +0000254 // Parse identifier.
255 NamespcName = Tok.getIdentifierInfo();
256 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner823c44e2009-01-06 07:27:21 +0000258 // Parse (optional) attributes (most likely GNU strong-using extension).
259 if (Tok.is(tok::kw___attribute))
260 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Chris Lattner823c44e2009-01-06 07:27:21 +0000262 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000263 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000264 ExpectAndConsume(tok::semi,
265 AttrList ? diag::err_expected_semi_after_attribute_list :
266 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000267
268 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000269 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000270}
271
272/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
273/// 'using' was already seen.
274///
275/// using-declaration: [C++ 7.3.p3: namespace.udecl]
276/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000277/// unqualified-id
278/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000279///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000280Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000281 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000282 SourceLocation &DeclEnd,
283 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000284 CXXScopeSpec SS;
285 bool IsTypeName;
286
287 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000288 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000289 if (Tok.is(tok::kw_typename)) {
290 ConsumeToken();
291 IsTypeName = true;
292 }
293 else
294 IsTypeName = false;
295
296 // Parse nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000297 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000298
299 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000300
301 // Check nested-name specifier.
302 if (SS.isInvalid()) {
303 SkipUntil(tok::semi);
304 return DeclPtrTy();
305 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000306
307 // Parse the unqualified-id. We allow parsing of both constructor and
308 // destructor names and allow the action module to diagnose any semantic
309 // errors.
310 UnqualifiedId Name;
311 if (ParseUnqualifiedId(SS,
312 /*EnteringContext=*/false,
313 /*AllowDestructorName=*/true,
314 /*AllowConstructorName=*/true,
315 /*ObjectType=*/0,
316 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000317 SkipUntil(tok::semi);
318 return DeclPtrTy();
319 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000320
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000321 // Parse (optional) attributes (most likely GNU strong-using extension).
322 if (Tok.is(tok::kw___attribute))
323 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000325 // Eat ';'.
326 DeclEnd = Tok.getLocation();
327 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000328 AttrList ? "attributes list" : "using declaration",
329 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000330
Douglas Gregor12c118a2009-11-04 16:30:06 +0000331 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS, Name,
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000332 AttrList, IsTypeName);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000333}
334
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000335/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
336///
337/// static_assert-declaration:
338/// static_assert ( constant-expression , string-literal ) ;
339///
Chris Lattner97144fc2009-04-02 04:16:50 +0000340Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000341 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
342 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000344 if (Tok.isNot(tok::l_paren)) {
345 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000346 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000349 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000350
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000351 OwningExprResult AssertExpr(ParseConstantExpression());
352 if (AssertExpr.isInvalid()) {
353 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000354 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Anders Carlssonad5f9602009-03-13 23:29:20 +0000357 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000358 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000359
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000360 if (Tok.isNot(tok::string_literal)) {
361 Diag(Tok, diag::err_expected_string_literal);
362 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000363 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000366 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000367 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000368 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000369
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000370 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner97144fc2009-04-02 04:16:50 +0000372 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000373 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
374
Mike Stump1eb44332009-09-09 15:08:12 +0000375 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000376 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000377}
378
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000379/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
380///
381/// 'decltype' ( expression )
382///
383void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
384 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
385
386 SourceLocation StartLoc = ConsumeToken();
387 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000388
389 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000390 "decltype")) {
391 SkipUntil(tok::r_paren);
392 return;
393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000395 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000397 // C++0x [dcl.type.simple]p4:
398 // The operand of the decltype specifier is an unevaluated operand.
399 EnterExpressionEvaluationContext Unevaluated(Actions,
400 Action::Unevaluated);
401 OwningExprResult Result = ParseExpression();
402 if (Result.isInvalid()) {
403 SkipUntil(tok::r_paren);
404 return;
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000407 // Match the ')'
408 SourceLocation RParenLoc;
409 if (Tok.is(tok::r_paren))
410 RParenLoc = ConsumeParen();
411 else
412 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000414 if (RParenLoc.isInvalid())
415 return;
416
417 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000418 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000419 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000420 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000421 DiagID, Result.release()))
422 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000423}
424
Douglas Gregor42a552f2008-11-05 20:51:48 +0000425/// ParseClassName - Parse a C++ class-name, which names a class. Note
426/// that we only check that the result names a type; semantic analysis
427/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000428/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000429/// found.
430///
431/// class-name: [C++ 9.1]
432/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000433/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000434///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000435Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000436 const CXXScopeSpec *SS,
437 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000438 // Check whether we have a template-id that names a type.
439 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000440 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000441 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000442 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000443 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000444
445 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
446 TypeTy *Type = Tok.getAnnotationValue();
447 EndLocation = Tok.getAnnotationEndLoc();
448 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000449
450 if (Type)
451 return Type;
452 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000453 }
454
455 // Fall through to produce an error below.
456 }
457
Douglas Gregor42a552f2008-11-05 20:51:48 +0000458 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000459 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000460 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000461 }
462
463 // We have an identifier; check whether it is actually a type.
Mike Stump1eb44332009-09-09 15:08:12 +0000464 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor42c39f32009-08-26 18:27:52 +0000465 Tok.getLocation(), CurScope, SS,
466 true);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000467 if (!Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000468 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000469 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000470 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000471 }
472
473 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000474 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000475 return Type;
476}
477
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000478/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
479/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
480/// until we reach the start of a definition or see a token that
481/// cannot start a definition.
482///
483/// class-specifier: [C++ class]
484/// class-head '{' member-specification[opt] '}'
485/// class-head '{' member-specification[opt] '}' attributes[opt]
486/// class-head:
487/// class-key identifier[opt] base-clause[opt]
488/// class-key nested-name-specifier identifier base-clause[opt]
489/// class-key nested-name-specifier[opt] simple-template-id
490/// base-clause[opt]
491/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000492/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000493/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000494/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000495/// simple-template-id base-clause[opt]
496/// class-key:
497/// 'class'
498/// 'struct'
499/// 'union'
500///
501/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000502/// class-key ::[opt] nested-name-specifier[opt] identifier
503/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
504/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000505///
506/// Note that the C++ class-specifier and elaborated-type-specifier,
507/// together, subsume the C99 struct-or-union-specifier:
508///
509/// struct-or-union-specifier: [C99 6.7.2.1]
510/// struct-or-union identifier[opt] '{' struct-contents '}'
511/// struct-or-union identifier
512/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
513/// '}' attributes[opt]
514/// [GNU] struct-or-union attributes[opt] identifier
515/// struct-or-union:
516/// 'struct'
517/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000518void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
519 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000520 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000521 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000522 DeclSpec::TST TagType;
523 if (TagTokKind == tok::kw_struct)
524 TagType = DeclSpec::TST_struct;
525 else if (TagTokKind == tok::kw_class)
526 TagType = DeclSpec::TST_class;
527 else {
528 assert(TagTokKind == tok::kw_union && "Not a class specifier");
529 TagType = DeclSpec::TST_union;
530 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000531
Douglas Gregor374929f2009-09-18 15:37:17 +0000532 if (Tok.is(tok::code_completion)) {
533 // Code completion for a struct, class, or union name.
534 Actions.CodeCompleteTag(CurScope, TagType);
535 ConsumeToken();
536 }
537
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000538 AttributeList *Attr = 0;
539 // If attributes exist after tag, parse them.
540 if (Tok.is(tok::kw___attribute))
541 Attr = ParseAttributes();
542
Steve Narofff59e17e2008-12-24 20:59:21 +0000543 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000544 if (Tok.is(tok::kw___declspec))
545 Attr = ParseMicrosoftDeclSpec(Attr);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregorb117a602009-09-04 05:53:02 +0000547 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
548 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
549 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000550 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000551 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
552 // properly.
553 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
554 Tok.setKind(tok::identifier);
555 }
556
557 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
558 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
559 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000560 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000561 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
562 // properly.
563 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
564 Tok.setKind(tok::identifier);
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000567 // Parse the (optional) nested-name-specifier.
568 CXXScopeSpec SS;
Mike Stump1eb44332009-09-09 15:08:12 +0000569 if (getLang().CPlusPlus &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000570 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor39a8de12009-02-25 19:37:18 +0000571 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000572 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000573
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000574 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
575
Douglas Gregorcc636682009-02-17 23:15:12 +0000576 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000577 IdentifierInfo *Name = 0;
578 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000579 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000580 if (Tok.is(tok::identifier)) {
581 Name = Tok.getIdentifierInfo();
582 NameLoc = ConsumeToken();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000583
584 if (Tok.is(tok::less)) {
585 // The name was supposed to refer to a template, but didn't.
586 // Eat the template argument list and try to continue parsing this as
587 // a class (or template thereof).
588 TemplateArgList TemplateArgs;
589 TemplateArgIsTypeList TemplateArgIsType;
590 TemplateArgLocationList TemplateArgLocations;
591 SourceLocation LAngleLoc, RAngleLoc;
592 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
593 true, LAngleLoc,
594 TemplateArgs, TemplateArgIsType,
595 TemplateArgLocations, RAngleLoc)) {
596 // We couldn't parse the template argument list at all, so don't
597 // try to give any location information for the list.
598 LAngleLoc = RAngleLoc = SourceLocation();
599 }
600
601 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000602 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000603 << (TagType == DeclSpec::TST_class? 0
604 : TagType == DeclSpec::TST_struct? 1
605 : 2)
606 << Name
607 << SourceRange(LAngleLoc, RAngleLoc);
608
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000609 // Strip off the last template parameter list if it was empty, since
610 // we've removed its template argument list.
611 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
612 if (TemplateParams && TemplateParams->size() > 1) {
613 TemplateParams->pop_back();
614 } else {
615 TemplateParams = 0;
616 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
617 = ParsedTemplateInfo::NonTemplate;
618 }
619 } else if (TemplateInfo.Kind
620 == ParsedTemplateInfo::ExplicitInstantiation) {
621 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000622 TemplateParams = 0;
623 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
624 = ParsedTemplateInfo::NonTemplate;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000625 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
626 = SourceLocation();
627 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
628 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000629 }
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000630
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000631
632 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000633 } else if (Tok.is(tok::annot_template_id)) {
634 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
635 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000636
Douglas Gregorc45c2322009-03-31 00:43:58 +0000637 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000638 // The template-name in the simple-template-id refers to
639 // something other than a class template. Give an appropriate
640 // error message and skip to the ';'.
641 SourceRange Range(NameLoc);
642 if (SS.isNotEmpty())
643 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000644
Douglas Gregor39a8de12009-02-25 19:37:18 +0000645 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
646 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Douglas Gregor39a8de12009-02-25 19:37:18 +0000648 DS.SetTypeSpecError();
649 SkipUntil(tok::semi, false, true);
650 TemplateId->Destroy();
651 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000652 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000653 }
654
John McCall67d1a672009-08-06 02:15:43 +0000655 // There are four options here. If we have 'struct foo;', then this
656 // is either a forward declaration or a friend declaration, which
657 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000658 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000659 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000660 Action::TagUseKind TUK;
Douglas Gregord85bea22009-09-26 06:47:28 +0000661 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
662 if (DS.isFriendSpecified()) {
663 // C++ [class.friend]p2:
664 // A class shall not be defined in a friend declaration.
665 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
666 << SourceRange(DS.getFriendSpecLoc());
667
668 // Skip everything up to the semicolon, so that this looks like a proper
669 // friend class (or template thereof) declaration.
670 SkipUntil(tok::semi, true, true);
671 TUK = Action::TUK_Friend;
672 } else {
673 // Okay, this is a class definition.
674 TUK = Action::TUK_Definition;
675 }
676 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000677 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000678 else
John McCall0f434ec2009-07-31 02:45:11 +0000679 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000680
John McCall0f434ec2009-07-31 02:45:11 +0000681 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000682 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000683 Diag(StartLoc, diag::err_anon_type_definition)
684 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000685
686 // Skip the rest of this declarator, up until the comma or semicolon.
687 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000688
689 if (TemplateId)
690 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000691 return;
692 }
693
Douglas Gregorddc29e12009-02-06 22:42:48 +0000694 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000695 Action::DeclResult TagOrTempResult = true; // invalid
696 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000697
John McCall0f434ec2009-07-31 02:45:11 +0000698 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000699 // to turn that template-id into a type.
700
Douglas Gregor402abb52009-05-28 23:31:59 +0000701 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000702 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000703 // Explicit specialization, class template partial specialization,
704 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000705 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000706 TemplateId->getTemplateArgs(),
707 TemplateId->getTemplateArgIsType(),
708 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000709 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000710 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000711 // This is an explicit instantiation of a class template.
712 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000713 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000714 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000715 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000716 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000717 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000718 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000719 TemplateTy::make(TemplateId->Template),
720 TemplateId->TemplateNameLoc,
721 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000722 TemplateArgsPtr,
723 TemplateId->getTemplateArgLocations(),
Mike Stump1eb44332009-09-09 15:08:12 +0000724 TemplateId->RAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000725 Attr);
Douglas Gregorfc9cd612009-09-26 20:57:03 +0000726 } else if (TUK == Action::TUK_Reference) {
John McCallc4e70192009-09-11 04:59:25 +0000727 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000728 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
729 TemplateId->TemplateNameLoc,
730 TemplateId->LAngleLoc,
731 TemplateArgsPtr,
732 TemplateId->getTemplateArgLocations(),
733 TemplateId->RAngleLoc);
734
John McCallc4e70192009-09-11 04:59:25 +0000735 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
736 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000737 } else {
738 // This is an explicit specialization or a class template
739 // partial specialization.
740 TemplateParameterLists FakedParamLists;
741
742 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
743 // This looks like an explicit instantiation, because we have
744 // something like
745 //
746 // template class Foo<X>
747 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000748 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000749 // meant to be an explicit specialization, but the user forgot
750 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000751 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000752
Mike Stump1eb44332009-09-09 15:08:12 +0000753 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000754 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000755 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000756 diag::err_explicit_instantiation_with_definition)
757 << SourceRange(TemplateInfo.TemplateLoc)
758 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
759
760 // Create a fake template parameter list that contains only
761 // "template<>", so that we treat this construct as a class
762 // template specialization.
763 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000764 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000765 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000766 LAngleLoc,
767 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000768 LAngleLoc));
769 TemplateParams = &FakedParamLists;
770 }
771
772 // Build the class template specialization.
773 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000774 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000775 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000776 TemplateTy::make(TemplateId->Template),
777 TemplateId->TemplateNameLoc,
778 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000779 TemplateArgsPtr,
780 TemplateId->getTemplateArgLocations(),
Mike Stump1eb44332009-09-09 15:08:12 +0000781 TemplateId->RAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000782 Attr,
Mike Stump1eb44332009-09-09 15:08:12 +0000783 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000784 TemplateParams? &(*TemplateParams)[0] : 0,
785 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000786 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000787 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000788 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000789 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000790 // Explicit instantiation of a member of a class template
791 // specialization, e.g.,
792 //
793 // template struct Outer<int>::Inner;
794 //
795 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000796 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000797 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000798 TemplateInfo.TemplateLoc,
799 TagType, StartLoc, SS, Name,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000800 NameLoc, Attr);
801 } else {
802 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000803 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000804 // FIXME: Diagnose this particular error.
805 }
806
John McCallc4e70192009-09-11 04:59:25 +0000807 bool IsDependent = false;
808
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000809 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000810 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000811 Name, NameLoc, Attr, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000812 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000813 TemplateParams? &(*TemplateParams)[0] : 0,
814 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000815 Owned, IsDependent);
816
817 // If ActOnTag said the type was dependent, try again with the
818 // less common call.
819 if (IsDependent)
820 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
821 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000822 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000823
824 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000825 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000826 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000827
828 // If there is a body, parse it and inform the actions module.
829 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000830 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000831 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000832 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000833 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000834 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000835 // FIXME: Complain that we have a base-specifier list but no
836 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000837 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000838 }
839
John McCallc4e70192009-09-11 04:59:25 +0000840 void *Result;
841 if (!TypeResult.isInvalid()) {
842 TagType = DeclSpec::TST_typename;
843 Result = TypeResult.get();
844 Owned = false;
845 } else if (!TagOrTempResult.isInvalid()) {
846 Result = TagOrTempResult.get().getAs<void>();
847 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000848 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000849 return;
850 }
Mike Stump1eb44332009-09-09 15:08:12 +0000851
John McCallfec54012009-08-03 20:12:06 +0000852 const char *PrevSpec = 0;
853 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000854
John McCallfec54012009-08-03 20:12:06 +0000855 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000856 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000857 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000858}
859
Mike Stump1eb44332009-09-09 15:08:12 +0000860/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000861///
862/// base-clause : [C++ class.derived]
863/// ':' base-specifier-list
864/// base-specifier-list:
865/// base-specifier '...'[opt]
866/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000867void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000868 assert(Tok.is(tok::colon) && "Not a base clause");
869 ConsumeToken();
870
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000871 // Build up an array of parsed base specifiers.
872 llvm::SmallVector<BaseTy *, 8> BaseInfo;
873
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000874 while (true) {
875 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000876 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000877 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000878 // Skip the rest of this base specifier, up until the comma or
879 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000880 SkipUntil(tok::comma, tok::l_brace, true, true);
881 } else {
882 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000883 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000884 }
885
886 // If the next token is a comma, consume it and keep reading
887 // base-specifiers.
888 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000890 // Consume the comma.
891 ConsumeToken();
892 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000893
894 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000895 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000896}
897
898/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
899/// one entry in the base class list of a class specifier, for example:
900/// class foo : public bar, virtual private baz {
901/// 'public bar' and 'virtual private baz' are each base-specifiers.
902///
903/// base-specifier: [C++ class.derived]
904/// ::[opt] nested-name-specifier[opt] class-name
905/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
906/// class-name
907/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
908/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000909Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000910 bool IsVirtual = false;
911 SourceLocation StartLoc = Tok.getLocation();
912
913 // Parse the 'virtual' keyword.
914 if (Tok.is(tok::kw_virtual)) {
915 ConsumeToken();
916 IsVirtual = true;
917 }
918
919 // Parse an (optional) access specifier.
920 AccessSpecifier Access = getAccessSpecifierIfPresent();
921 if (Access)
922 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000924 // Parse the 'virtual' keyword (again!), in case it came after the
925 // access specifier.
926 if (Tok.is(tok::kw_virtual)) {
927 SourceLocation VirtualLoc = ConsumeToken();
928 if (IsVirtual) {
929 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000930 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000931 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000932 }
933
934 IsVirtual = true;
935 }
936
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000937 // Parse optional '::' and optional nested-name-specifier.
938 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000939 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000940
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000941 // The location of the base class itself.
942 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000943
944 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000945 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000946 TypeResult BaseType = ParseClassName(EndLocation, &SS);
947 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000948 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000949
950 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000951 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000953 // Notify semantic analysis that we have parsed a complete
954 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000955 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000956 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000957}
958
959/// getAccessSpecifierIfPresent - Determine whether the next token is
960/// a C++ access-specifier.
961///
962/// access-specifier: [C++ class.derived]
963/// 'private'
964/// 'protected'
965/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +0000966AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000967 switch (Tok.getKind()) {
968 default: return AS_none;
969 case tok::kw_private: return AS_private;
970 case tok::kw_protected: return AS_protected;
971 case tok::kw_public: return AS_public;
972 }
973}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000974
Eli Friedmand33133c2009-07-22 21:45:50 +0000975void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
976 DeclPtrTy ThisDecl) {
977 // We just declared a member function. If this member function
978 // has any default arguments, we'll need to parse them later.
979 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000980 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +0000981 = DeclaratorInfo.getTypeObject(0).Fun;
982 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
983 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
984 if (!LateMethod) {
985 // Push this method onto the stack of late-parsed method
986 // declarations.
987 getCurrentClass().MethodDecls.push_back(
988 LateParsedMethodDeclaration(ThisDecl));
989 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +0000990 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +0000991
992 // Add all of the parameters prior to this one (they don't
993 // have default arguments).
994 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
995 for (unsigned I = 0; I < ParamIdx; ++I)
996 LateMethod->DefaultArgs.push_back(
997 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
998 }
999
1000 // Add this parameter to the list of parameters (it or may
1001 // not have a default argument).
1002 LateMethod->DefaultArgs.push_back(
1003 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1004 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1005 }
1006 }
1007}
1008
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001009/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1010///
1011/// member-declaration:
1012/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1013/// function-definition ';'[opt]
1014/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1015/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001016/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001017/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001018/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001019///
1020/// member-declarator-list:
1021/// member-declarator
1022/// member-declarator-list ',' member-declarator
1023///
1024/// member-declarator:
1025/// declarator pure-specifier[opt]
1026/// declarator constant-initializer[opt]
1027/// identifier[opt] ':' constant-expression
1028///
Sebastian Redle2b68332009-04-12 17:16:29 +00001029/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001030/// '= 0'
1031///
1032/// constant-initializer:
1033/// '=' constant-expression
1034///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001035void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1036 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001037 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001038 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001039 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001040 SourceLocation DeclEnd;
1041 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001042 return;
1043 }
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Chris Lattner682bf922009-03-29 16:50:03 +00001045 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001046 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001047 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001048 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001049 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001050 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001051 return;
1052 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001053
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001054 // Handle: member-declaration ::= '__extension__' member-declaration
1055 if (Tok.is(tok::kw___extension__)) {
1056 // __extension__ silences extension warnings in the subexpression.
1057 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1058 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001059 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001060 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001061
1062 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001063 // FIXME: Check for template aliases
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001065 // Eat 'using'.
1066 SourceLocation UsingLoc = ConsumeToken();
1067
1068 if (Tok.is(tok::kw_namespace)) {
1069 Diag(UsingLoc, diag::err_using_namespace_in_class);
1070 SkipUntil(tok::semi, true, true);
1071 }
1072 else {
1073 SourceLocation DeclEnd;
1074 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001075 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001076 }
1077 return;
1078 }
1079
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001080 SourceLocation DSStart = Tok.getLocation();
1081 // decl-specifier-seq:
1082 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001083 ParsingDeclSpec DS(*this);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001084 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001085
John McCalldd4a3b02009-09-16 22:47:08 +00001086 Action::MultiTemplateParamsArg TemplateParams(Actions,
1087 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1088 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1089
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001090 if (Tok.is(tok::semi)) {
1091 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001092 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001093 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001094 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001095
John McCall54abf7d2009-11-04 02:18:39 +00001096 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001097
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001098 if (Tok.isNot(tok::colon)) {
1099 // Parse the first declarator.
1100 ParseDeclarator(DeclaratorInfo);
1101 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001102 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001103 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001104 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001105 if (Tok.is(tok::semi))
1106 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001107 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001108 }
1109
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001110 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001111 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001112 || (DeclaratorInfo.isFunctionDeclarator() &&
1113 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001114 if (!DeclaratorInfo.isFunctionDeclarator()) {
1115 Diag(Tok, diag::err_func_def_no_params);
1116 ConsumeBrace();
1117 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001118 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001119 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001120
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001121 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1122 Diag(Tok, diag::err_function_declared_typedef);
1123 // This recovery skips the entire function body. It would be nice
1124 // to simply call ParseCXXInlineMethodDef() below, however Sema
1125 // assumes the declarator represents a function, not a typedef.
1126 ConsumeBrace();
1127 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001128 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001129 }
1130
Douglas Gregor37b372b2009-08-20 22:52:58 +00001131 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001132 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001133 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001134 }
1135
1136 // member-declarator-list:
1137 // member-declarator
1138 // member-declarator-list ',' member-declarator
1139
Chris Lattner682bf922009-03-29 16:50:03 +00001140 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001141 OwningExprResult BitfieldSize(Actions);
1142 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001143 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001144
1145 while (1) {
1146
1147 // member-declarator:
1148 // declarator pure-specifier[opt]
1149 // declarator constant-initializer[opt]
1150 // identifier[opt] ':' constant-expression
1151
1152 if (Tok.is(tok::colon)) {
1153 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001154 BitfieldSize = ParseConstantExpression();
1155 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001156 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001159 // pure-specifier:
1160 // '= 0'
1161 //
1162 // constant-initializer:
1163 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001164 //
1165 // defaulted/deleted function-definition:
1166 // '=' 'default' [TODO]
1167 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001168
1169 if (Tok.is(tok::equal)) {
1170 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001171 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1172 ConsumeToken();
1173 Deleted = true;
1174 } else {
1175 Init = ParseInitializer();
1176 if (Init.isInvalid())
1177 SkipUntil(tok::comma, true, true);
1178 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001179 }
1180
1181 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001182 if (Tok.is(tok::kw___attribute)) {
1183 SourceLocation Loc;
1184 AttributeList *AttrList = ParseAttributes(&Loc);
1185 DeclaratorInfo.AddAttributes(AttrList, Loc);
1186 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001187
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001188 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001189 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001190 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001191
1192 DeclPtrTy ThisDecl;
1193 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001194 // TODO: handle initializers, bitfields, 'delete'
1195 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1196 /*IsDefinition*/ false,
1197 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001198 } else {
John McCall67d1a672009-08-06 02:15:43 +00001199 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1200 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001201 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001202 BitfieldSize.release(),
1203 Init.release(),
1204 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001205 }
Chris Lattner682bf922009-03-29 16:50:03 +00001206 if (ThisDecl)
1207 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001208
Douglas Gregor72b505b2008-12-16 21:30:33 +00001209 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001210 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001211 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001212 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001213 }
1214
John McCall54abf7d2009-11-04 02:18:39 +00001215 DeclaratorInfo.complete(ThisDecl);
1216
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001217 // If we don't have a comma, it is either the end of the list (a ';')
1218 // or an error, bail out.
1219 if (Tok.isNot(tok::comma))
1220 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001222 // Consume the comma.
1223 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001225 // Parse the next declarator.
1226 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001227 BitfieldSize = 0;
1228 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001229 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001231 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001232 if (Tok.is(tok::kw___attribute)) {
1233 SourceLocation Loc;
1234 AttributeList *AttrList = ParseAttributes(&Loc);
1235 DeclaratorInfo.AddAttributes(AttrList, Loc);
1236 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001237
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001238 if (Tok.isNot(tok::colon))
1239 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001240 }
1241
1242 if (Tok.is(tok::semi)) {
1243 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001244 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001245 DeclsInGroup.size());
1246 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001247 }
1248
1249 Diag(Tok, diag::err_expected_semi_decl_list);
1250 // Skip to end of block or statement
1251 SkipUntil(tok::r_brace, true, true);
1252 if (Tok.is(tok::semi))
1253 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001254 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001255}
1256
1257/// ParseCXXMemberSpecification - Parse the class definition.
1258///
1259/// member-specification:
1260/// member-declaration member-specification[opt]
1261/// access-specifier ':' member-specification[opt]
1262///
1263void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001264 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001265 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001266 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001267 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001268
Chris Lattner49f28ca2009-03-05 08:00:35 +00001269 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1270 PP.getSourceManager(),
1271 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001273 SourceLocation LBraceLoc = ConsumeBrace();
1274
Douglas Gregor6569d682009-05-27 23:11:45 +00001275 // Determine whether this is a top-level (non-nested) class.
Mike Stump1eb44332009-09-09 15:08:12 +00001276 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregor6569d682009-05-27 23:11:45 +00001277 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001278
1279 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001280 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001281
Douglas Gregor6569d682009-05-27 23:11:45 +00001282 // Note that we are parsing a new (potentially-nested) class definition.
1283 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1284
Douglas Gregorddc29e12009-02-06 22:42:48 +00001285 if (TagDecl)
1286 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1287 else {
1288 SkipUntil(tok::r_brace, false, false);
1289 return;
1290 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001291
1292 // C++ 11p3: Members of a class defined with the keyword class are private
1293 // by default. Members of a class defined with the keywords struct or union
1294 // are public by default.
1295 AccessSpecifier CurAS;
1296 if (TagType == DeclSpec::TST_class)
1297 CurAS = AS_private;
1298 else
1299 CurAS = AS_public;
1300
1301 // While we still have something to read, read the member-declarations.
1302 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1303 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001305 // Check for extraneous top-level semicolon.
1306 if (Tok.is(tok::semi)) {
1307 Diag(Tok, diag::ext_extra_struct_semi);
1308 ConsumeToken();
1309 continue;
1310 }
1311
1312 AccessSpecifier AS = getAccessSpecifierIfPresent();
1313 if (AS != AS_none) {
1314 // Current token is a C++ access specifier.
1315 CurAS = AS;
1316 ConsumeToken();
1317 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1318 continue;
1319 }
1320
Douglas Gregor37b372b2009-08-20 22:52:58 +00001321 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001323 // Parse all the comma separated declarators.
1324 ParseCXXClassMemberDeclaration(CurAS);
1325 }
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001327 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001329 AttributeList *AttrList = 0;
1330 // If attributes exist after class contents, parse them.
1331 if (Tok.is(tok::kw___attribute))
1332 AttrList = ParseAttributes(); // FIXME: where should I put them?
1333
1334 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1335 LBraceLoc, RBraceLoc);
1336
1337 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1338 // complete within function bodies, default arguments,
1339 // exception-specifications, and constructor ctor-initializers (including
1340 // such things in nested classes).
1341 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001342 // FIXME: Only function bodies and constructor ctor-initializers are
1343 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001344 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001345 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001346 // are complete and we can parse the delayed portions of method
1347 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001348 ParseLexedMethodDeclarations(getCurrentClass());
1349 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001350 }
1351
1352 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001353 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001354 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001355
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001356 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001357}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001358
1359/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1360/// which explicitly initializes the members or base classes of a
1361/// class (C++ [class.base.init]). For example, the three initializers
1362/// after the ':' in the Derived constructor below:
1363///
1364/// @code
1365/// class Base { };
1366/// class Derived : Base {
1367/// int x;
1368/// float f;
1369/// public:
1370/// Derived(float f) : Base(), x(17), f(f) { }
1371/// };
1372/// @endcode
1373///
Mike Stump1eb44332009-09-09 15:08:12 +00001374/// [C++] ctor-initializer:
1375/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001376///
Mike Stump1eb44332009-09-09 15:08:12 +00001377/// [C++] mem-initializer-list:
1378/// mem-initializer
1379/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001380void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001381 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1382
1383 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Douglas Gregor7ad83902008-11-05 04:29:56 +00001385 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Douglas Gregor7ad83902008-11-05 04:29:56 +00001387 do {
1388 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001389 if (!MemInit.isInvalid())
1390 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001391
1392 if (Tok.is(tok::comma))
1393 ConsumeToken();
1394 else if (Tok.is(tok::l_brace))
1395 break;
1396 else {
1397 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001398 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001399 SkipUntil(tok::l_brace, true, true);
1400 break;
1401 }
1402 } while (true);
1403
Mike Stump1eb44332009-09-09 15:08:12 +00001404 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001405 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001406}
1407
1408/// ParseMemInitializer - Parse a C++ member initializer, which is
1409/// part of a constructor initializer that explicitly initializes one
1410/// member or base class (C++ [class.base.init]). See
1411/// ParseConstructorInitializer for an example.
1412///
1413/// [C++] mem-initializer:
1414/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001415///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001416/// [C++] mem-initializer-id:
1417/// '::'[opt] nested-name-specifier[opt] class-name
1418/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001419Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001420 // parse '::'[opt] nested-name-specifier[opt]
1421 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001422 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001423 TypeTy *TemplateTypeTy = 0;
1424 if (Tok.is(tok::annot_template_id)) {
1425 TemplateIdAnnotation *TemplateId
1426 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1427 if (TemplateId->Kind == TNK_Type_template) {
1428 AnnotateTemplateIdTokenAsType(&SS);
1429 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1430 TemplateTypeTy = Tok.getAnnotationValue();
1431 }
1432 // FIXME. May need to check for TNK_Dependent_template as well.
1433 }
1434 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001435 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001436 return true;
1437 }
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Douglas Gregor7ad83902008-11-05 04:29:56 +00001439 // Get the identifier. This may be a member name or a class name,
1440 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001441 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001442 SourceLocation IdLoc = ConsumeToken();
1443
1444 // Parse the '('.
1445 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001446 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001447 return true;
1448 }
1449 SourceLocation LParenLoc = ConsumeParen();
1450
1451 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001452 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001453 CommaLocsTy CommaLocs;
1454 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1455 SkipUntil(tok::r_paren);
1456 return true;
1457 }
1458
1459 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1460
Fariborz Jahanian96174332009-07-01 19:21:19 +00001461 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1462 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001463 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001464 ArgExprs.size(), CommaLocs.data(),
1465 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001466}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001467
1468/// ParseExceptionSpecification - Parse a C++ exception-specification
1469/// (C++ [except.spec]).
1470///
Douglas Gregora4745612008-12-01 18:00:20 +00001471/// exception-specification:
1472/// 'throw' '(' type-id-list [opt] ')'
1473/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001474///
Douglas Gregora4745612008-12-01 18:00:20 +00001475/// type-id-list:
1476/// type-id
1477/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001478///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001479bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001480 llvm::SmallVector<TypeTy*, 2>
1481 &Exceptions,
1482 llvm::SmallVector<SourceRange, 2>
1483 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001484 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001485 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001487 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001489 if (!Tok.is(tok::l_paren)) {
1490 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1491 }
1492 SourceLocation LParenLoc = ConsumeParen();
1493
Douglas Gregora4745612008-12-01 18:00:20 +00001494 // Parse throw(...), a Microsoft extension that means "this function
1495 // can throw anything".
1496 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001497 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001498 SourceLocation EllipsisLoc = ConsumeToken();
1499 if (!getLang().Microsoft)
1500 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001501 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001502 return false;
1503 }
1504
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001505 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001506 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001507 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001508 TypeResult Res(ParseTypeName(&Range));
1509 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001510 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001511 Ranges.push_back(Range);
1512 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001513 if (Tok.is(tok::comma))
1514 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001515 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001516 break;
1517 }
1518
Sebastian Redlab197ba2009-02-09 18:23:29 +00001519 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001520 return false;
1521}
Douglas Gregor6569d682009-05-27 23:11:45 +00001522
1523/// \brief We have just started parsing the definition of a new class,
1524/// so push that class onto our stack of classes that is currently
1525/// being parsed.
1526void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001527 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001528 "Nested class without outer class");
1529 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1530}
1531
1532/// \brief Deallocate the given parsed class and all of its nested
1533/// classes.
1534void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1535 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1536 DeallocateParsedClasses(Class->NestedClasses[I]);
1537 delete Class;
1538}
1539
1540/// \brief Pop the top class of the stack of classes that are
1541/// currently being parsed.
1542///
1543/// This routine should be called when we have finished parsing the
1544/// definition of a class, but have not yet popped the Scope
1545/// associated with the class's definition.
1546///
1547/// \returns true if the class we've popped is a top-level class,
1548/// false otherwise.
1549void Parser::PopParsingClass() {
1550 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Douglas Gregor6569d682009-05-27 23:11:45 +00001552 ParsingClass *Victim = ClassStack.top();
1553 ClassStack.pop();
1554 if (Victim->TopLevelClass) {
1555 // Deallocate all of the nested classes of this class,
1556 // recursively: we don't need to keep any of this information.
1557 DeallocateParsedClasses(Victim);
1558 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001559 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001560 assert(!ClassStack.empty() && "Missing top-level class?");
1561
1562 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1563 Victim->NestedClasses.empty()) {
1564 // The victim is a nested class, but we will not need to perform
1565 // any processing after the definition of this class since it has
1566 // no members whose handling was delayed. Therefore, we can just
1567 // remove this nested class.
1568 delete Victim;
1569 return;
1570 }
1571
1572 // This nested class has some members that will need to be processed
1573 // after the top-level class is completely defined. Therefore, add
1574 // it to the list of nested classes within its parent.
1575 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1576 ClassStack.top()->NestedClasses.push_back(Victim);
1577 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1578}