blob: 59a6e6281c2e6615524806b9e76bee0c61504df8 [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'.
288 if (Tok.is(tok::kw_typename)) {
289 ConsumeToken();
290 IsTypeName = true;
291 }
292 else
293 IsTypeName = false;
294
295 // Parse nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000296 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000297
298 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000299
300 // Check nested-name specifier.
301 if (SS.isInvalid()) {
302 SkipUntil(tok::semi);
303 return DeclPtrTy();
304 }
305 if (Tok.is(tok::annot_template_id)) {
Anders Carlsson73b39cf2009-08-28 03:35:18 +0000306 // C++0x N2914 [namespace.udecl]p5:
Mike Stump1eb44332009-09-09 15:08:12 +0000307 // A using-declaration shall not name a template-id.
Anders Carlsson73b39cf2009-08-28 03:35:18 +0000308 Diag(Tok, diag::err_using_decl_can_not_refer_to_template_spec);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000309 SkipUntil(tok::semi);
310 return DeclPtrTy();
311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000313 IdentifierInfo *TargetName = 0;
314 OverloadedOperatorKind Op = OO_None;
315 SourceLocation IdentLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000317 if (Tok.is(tok::kw_operator)) {
318 IdentLoc = Tok.getLocation();
319
320 Op = TryParseOperatorFunctionId();
321 if (!Op) {
322 // If there was an invalid operator, skip to end of decl, and eat ';'.
323 SkipUntil(tok::semi);
324 return DeclPtrTy();
325 }
326 } else if (Tok.is(tok::identifier)) {
327 // Parse identifier.
328 TargetName = Tok.getIdentifierInfo();
329 IdentLoc = ConsumeToken();
330 } else {
331 // FIXME: Use a better diagnostic here.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000332 Diag(Tok, diag::err_expected_ident_in_using);
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000333
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000334 // If there was invalid identifier, skip to end of decl, and eat ';'.
335 SkipUntil(tok::semi);
336 return DeclPtrTy();
337 }
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000339 // Parse (optional) attributes (most likely GNU strong-using extension).
340 if (Tok.is(tok::kw___attribute))
341 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000343 // Eat ';'.
344 DeclEnd = Tok.getLocation();
345 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
346 AttrList ? "attributes list" : "namespace name", tok::semi);
347
Anders Carlsson595adc12009-08-29 19:54:19 +0000348 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS,
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000349 IdentLoc, TargetName, Op,
350 AttrList, IsTypeName);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000351}
352
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000353/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
354///
355/// static_assert-declaration:
356/// static_assert ( constant-expression , string-literal ) ;
357///
Chris Lattner97144fc2009-04-02 04:16:50 +0000358Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000359 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
360 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000362 if (Tok.isNot(tok::l_paren)) {
363 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000364 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000367 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000368
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000369 OwningExprResult AssertExpr(ParseConstantExpression());
370 if (AssertExpr.isInvalid()) {
371 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000372 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Anders Carlssonad5f9602009-03-13 23:29:20 +0000375 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000376 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000377
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000378 if (Tok.isNot(tok::string_literal)) {
379 Diag(Tok, diag::err_expected_string_literal);
380 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000381 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000384 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000385 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000386 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000387
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000388 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattner97144fc2009-04-02 04:16:50 +0000390 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000391 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
392
Mike Stump1eb44332009-09-09 15:08:12 +0000393 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000394 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000395}
396
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000397/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
398///
399/// 'decltype' ( expression )
400///
401void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
402 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
403
404 SourceLocation StartLoc = ConsumeToken();
405 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000406
407 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000408 "decltype")) {
409 SkipUntil(tok::r_paren);
410 return;
411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000413 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000415 // C++0x [dcl.type.simple]p4:
416 // The operand of the decltype specifier is an unevaluated operand.
417 EnterExpressionEvaluationContext Unevaluated(Actions,
418 Action::Unevaluated);
419 OwningExprResult Result = ParseExpression();
420 if (Result.isInvalid()) {
421 SkipUntil(tok::r_paren);
422 return;
423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000425 // Match the ')'
426 SourceLocation RParenLoc;
427 if (Tok.is(tok::r_paren))
428 RParenLoc = ConsumeParen();
429 else
430 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000432 if (RParenLoc.isInvalid())
433 return;
434
435 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000436 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000437 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000438 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000439 DiagID, Result.release()))
440 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000441}
442
Douglas Gregor42a552f2008-11-05 20:51:48 +0000443/// ParseClassName - Parse a C++ class-name, which names a class. Note
444/// that we only check that the result names a type; semantic analysis
445/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000446/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000447/// found.
448///
449/// class-name: [C++ 9.1]
450/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000451/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000452///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000453Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000454 const CXXScopeSpec *SS,
455 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000456 // Check whether we have a template-id that names a type.
457 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000458 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000459 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000460 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000461 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000462
463 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
464 TypeTy *Type = Tok.getAnnotationValue();
465 EndLocation = Tok.getAnnotationEndLoc();
466 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000467
468 if (Type)
469 return Type;
470 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000471 }
472
473 // Fall through to produce an error below.
474 }
475
Douglas Gregor42a552f2008-11-05 20:51:48 +0000476 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000477 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000478 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000479 }
480
481 // We have an identifier; check whether it is actually a type.
Mike Stump1eb44332009-09-09 15:08:12 +0000482 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor42c39f32009-08-26 18:27:52 +0000483 Tok.getLocation(), CurScope, SS,
484 true);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000485 if (!Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000486 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000487 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000488 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000489 }
490
491 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000492 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000493 return Type;
494}
495
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000496/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
497/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
498/// until we reach the start of a definition or see a token that
499/// cannot start a definition.
500///
501/// class-specifier: [C++ class]
502/// class-head '{' member-specification[opt] '}'
503/// class-head '{' member-specification[opt] '}' attributes[opt]
504/// class-head:
505/// class-key identifier[opt] base-clause[opt]
506/// class-key nested-name-specifier identifier base-clause[opt]
507/// class-key nested-name-specifier[opt] simple-template-id
508/// base-clause[opt]
509/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000510/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000511/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000512/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000513/// simple-template-id base-clause[opt]
514/// class-key:
515/// 'class'
516/// 'struct'
517/// 'union'
518///
519/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000520/// class-key ::[opt] nested-name-specifier[opt] identifier
521/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
522/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000523///
524/// Note that the C++ class-specifier and elaborated-type-specifier,
525/// together, subsume the C99 struct-or-union-specifier:
526///
527/// struct-or-union-specifier: [C99 6.7.2.1]
528/// struct-or-union identifier[opt] '{' struct-contents '}'
529/// struct-or-union identifier
530/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
531/// '}' attributes[opt]
532/// [GNU] struct-or-union attributes[opt] identifier
533/// struct-or-union:
534/// 'struct'
535/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000536void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
537 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000538 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000539 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000540 DeclSpec::TST TagType;
541 if (TagTokKind == tok::kw_struct)
542 TagType = DeclSpec::TST_struct;
543 else if (TagTokKind == tok::kw_class)
544 TagType = DeclSpec::TST_class;
545 else {
546 assert(TagTokKind == tok::kw_union && "Not a class specifier");
547 TagType = DeclSpec::TST_union;
548 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000549
Douglas Gregor374929f2009-09-18 15:37:17 +0000550 if (Tok.is(tok::code_completion)) {
551 // Code completion for a struct, class, or union name.
552 Actions.CodeCompleteTag(CurScope, TagType);
553 ConsumeToken();
554 }
555
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000556 AttributeList *Attr = 0;
557 // If attributes exist after tag, parse them.
558 if (Tok.is(tok::kw___attribute))
559 Attr = ParseAttributes();
560
Steve Narofff59e17e2008-12-24 20:59:21 +0000561 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000562 if (Tok.is(tok::kw___declspec))
563 Attr = ParseMicrosoftDeclSpec(Attr);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Douglas Gregorb117a602009-09-04 05:53:02 +0000565 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
566 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
567 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000568 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000569 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
570 // properly.
571 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
572 Tok.setKind(tok::identifier);
573 }
574
575 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
576 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
577 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000578 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000579 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
580 // properly.
581 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
582 Tok.setKind(tok::identifier);
583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000585 // Parse the (optional) nested-name-specifier.
586 CXXScopeSpec SS;
Mike Stump1eb44332009-09-09 15:08:12 +0000587 if (getLang().CPlusPlus &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000588 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor39a8de12009-02-25 19:37:18 +0000589 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000590 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000591
592 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000593 IdentifierInfo *Name = 0;
594 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000595 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000596 if (Tok.is(tok::identifier)) {
597 Name = Tok.getIdentifierInfo();
598 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000599 } else if (Tok.is(tok::annot_template_id)) {
600 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
601 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000602
Douglas Gregorc45c2322009-03-31 00:43:58 +0000603 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000604 // The template-name in the simple-template-id refers to
605 // something other than a class template. Give an appropriate
606 // error message and skip to the ';'.
607 SourceRange Range(NameLoc);
608 if (SS.isNotEmpty())
609 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000610
Douglas Gregor39a8de12009-02-25 19:37:18 +0000611 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
612 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Douglas Gregor39a8de12009-02-25 19:37:18 +0000614 DS.SetTypeSpecError();
615 SkipUntil(tok::semi, false, true);
616 TemplateId->Destroy();
617 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000618 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000619 }
620
John McCall67d1a672009-08-06 02:15:43 +0000621 // There are four options here. If we have 'struct foo;', then this
622 // is either a forward declaration or a friend declaration, which
623 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000624 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000625 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000626 Action::TagUseKind TUK;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000627 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
John McCall0f434ec2009-07-31 02:45:11 +0000628 TUK = Action::TUK_Definition;
John McCall67d1a672009-08-06 02:15:43 +0000629 else if (Tok.is(tok::semi))
630 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000631 else
John McCall0f434ec2009-07-31 02:45:11 +0000632 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000633
John McCall0f434ec2009-07-31 02:45:11 +0000634 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000635 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000636 Diag(StartLoc, diag::err_anon_type_definition)
637 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000638
639 // Skip the rest of this declarator, up until the comma or semicolon.
640 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000641
642 if (TemplateId)
643 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000644 return;
645 }
646
Douglas Gregorddc29e12009-02-06 22:42:48 +0000647 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000648 Action::DeclResult TagOrTempResult = true; // invalid
649 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000650 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
651
John McCall0f434ec2009-07-31 02:45:11 +0000652 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000653 // to turn that template-id into a type.
654
Douglas Gregor402abb52009-05-28 23:31:59 +0000655 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000656 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000657 // Explicit specialization, class template partial specialization,
658 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000659 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000660 TemplateId->getTemplateArgs(),
661 TemplateId->getTemplateArgIsType(),
662 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000663 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000664 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000665 // This is an explicit instantiation of a class template.
666 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000667 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000668 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000669 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000670 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000671 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000672 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000673 TemplateTy::make(TemplateId->Template),
674 TemplateId->TemplateNameLoc,
675 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000676 TemplateArgsPtr,
677 TemplateId->getTemplateArgLocations(),
Mike Stump1eb44332009-09-09 15:08:12 +0000678 TemplateId->RAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000679 Attr);
John McCallf1bbbb42009-09-04 01:14:41 +0000680 } else if (TUK == Action::TUK_Reference || TUK == Action::TUK_Friend) {
John McCallc4e70192009-09-11 04:59:25 +0000681 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000682 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
683 TemplateId->TemplateNameLoc,
684 TemplateId->LAngleLoc,
685 TemplateArgsPtr,
686 TemplateId->getTemplateArgLocations(),
687 TemplateId->RAngleLoc);
688
John McCallc4e70192009-09-11 04:59:25 +0000689 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
690 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000691 } else {
692 // This is an explicit specialization or a class template
693 // partial specialization.
694 TemplateParameterLists FakedParamLists;
695
696 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
697 // This looks like an explicit instantiation, because we have
698 // something like
699 //
700 // template class Foo<X>
701 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000702 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000703 // meant to be an explicit specialization, but the user forgot
704 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000705 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000706
Mike Stump1eb44332009-09-09 15:08:12 +0000707 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000708 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000709 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000710 diag::err_explicit_instantiation_with_definition)
711 << SourceRange(TemplateInfo.TemplateLoc)
712 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
713
714 // Create a fake template parameter list that contains only
715 // "template<>", so that we treat this construct as a class
716 // template specialization.
717 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000718 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000719 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000720 LAngleLoc,
721 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000722 LAngleLoc));
723 TemplateParams = &FakedParamLists;
724 }
725
726 // Build the class template specialization.
727 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000728 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000729 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000730 TemplateTy::make(TemplateId->Template),
731 TemplateId->TemplateNameLoc,
732 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000733 TemplateArgsPtr,
734 TemplateId->getTemplateArgLocations(),
Mike Stump1eb44332009-09-09 15:08:12 +0000735 TemplateId->RAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000736 Attr,
Mike Stump1eb44332009-09-09 15:08:12 +0000737 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000738 TemplateParams? &(*TemplateParams)[0] : 0,
739 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000740 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000741 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000742 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000743 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000744 // Explicit instantiation of a member of a class template
745 // specialization, e.g.,
746 //
747 // template struct Outer<int>::Inner;
748 //
749 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000750 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000751 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000752 TemplateInfo.TemplateLoc,
753 TagType, StartLoc, SS, Name,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000754 NameLoc, Attr);
755 } else {
756 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000757 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000758 // FIXME: Diagnose this particular error.
759 }
760
John McCallc4e70192009-09-11 04:59:25 +0000761 bool IsDependent = false;
762
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000763 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000764 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000765 Name, NameLoc, Attr, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000766 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000767 TemplateParams? &(*TemplateParams)[0] : 0,
768 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000769 Owned, IsDependent);
770
771 // If ActOnTag said the type was dependent, try again with the
772 // less common call.
773 if (IsDependent)
774 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
775 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000776 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000777
778 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000779 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000780 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000781
782 // If there is a body, parse it and inform the actions module.
783 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000784 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000785 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000786 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000787 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000788 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000789 // FIXME: Complain that we have a base-specifier list but no
790 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000791 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000792 }
793
John McCallc4e70192009-09-11 04:59:25 +0000794 void *Result;
795 if (!TypeResult.isInvalid()) {
796 TagType = DeclSpec::TST_typename;
797 Result = TypeResult.get();
798 Owned = false;
799 } else if (!TagOrTempResult.isInvalid()) {
800 Result = TagOrTempResult.get().getAs<void>();
801 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000802 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000803 return;
804 }
Mike Stump1eb44332009-09-09 15:08:12 +0000805
John McCallfec54012009-08-03 20:12:06 +0000806 const char *PrevSpec = 0;
807 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000808
John McCallfec54012009-08-03 20:12:06 +0000809 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000810 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000811 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000812}
813
Mike Stump1eb44332009-09-09 15:08:12 +0000814/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000815///
816/// base-clause : [C++ class.derived]
817/// ':' base-specifier-list
818/// base-specifier-list:
819/// base-specifier '...'[opt]
820/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000821void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000822 assert(Tok.is(tok::colon) && "Not a base clause");
823 ConsumeToken();
824
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000825 // Build up an array of parsed base specifiers.
826 llvm::SmallVector<BaseTy *, 8> BaseInfo;
827
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000828 while (true) {
829 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000830 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000831 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000832 // Skip the rest of this base specifier, up until the comma or
833 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000834 SkipUntil(tok::comma, tok::l_brace, true, true);
835 } else {
836 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000837 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000838 }
839
840 // If the next token is a comma, consume it and keep reading
841 // base-specifiers.
842 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000844 // Consume the comma.
845 ConsumeToken();
846 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000847
848 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000849 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000850}
851
852/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
853/// one entry in the base class list of a class specifier, for example:
854/// class foo : public bar, virtual private baz {
855/// 'public bar' and 'virtual private baz' are each base-specifiers.
856///
857/// base-specifier: [C++ class.derived]
858/// ::[opt] nested-name-specifier[opt] class-name
859/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
860/// class-name
861/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
862/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000863Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000864 bool IsVirtual = false;
865 SourceLocation StartLoc = Tok.getLocation();
866
867 // Parse the 'virtual' keyword.
868 if (Tok.is(tok::kw_virtual)) {
869 ConsumeToken();
870 IsVirtual = true;
871 }
872
873 // Parse an (optional) access specifier.
874 AccessSpecifier Access = getAccessSpecifierIfPresent();
875 if (Access)
876 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000878 // Parse the 'virtual' keyword (again!), in case it came after the
879 // access specifier.
880 if (Tok.is(tok::kw_virtual)) {
881 SourceLocation VirtualLoc = ConsumeToken();
882 if (IsVirtual) {
883 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000884 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000885 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000886 }
887
888 IsVirtual = true;
889 }
890
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000891 // Parse optional '::' and optional nested-name-specifier.
892 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000893 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000894
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000895 // The location of the base class itself.
896 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000897
898 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000899 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000900 TypeResult BaseType = ParseClassName(EndLocation, &SS);
901 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000902 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000903
904 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000905 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000907 // Notify semantic analysis that we have parsed a complete
908 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000909 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000910 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000911}
912
913/// getAccessSpecifierIfPresent - Determine whether the next token is
914/// a C++ access-specifier.
915///
916/// access-specifier: [C++ class.derived]
917/// 'private'
918/// 'protected'
919/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +0000920AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000921 switch (Tok.getKind()) {
922 default: return AS_none;
923 case tok::kw_private: return AS_private;
924 case tok::kw_protected: return AS_protected;
925 case tok::kw_public: return AS_public;
926 }
927}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000928
Eli Friedmand33133c2009-07-22 21:45:50 +0000929void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
930 DeclPtrTy ThisDecl) {
931 // We just declared a member function. If this member function
932 // has any default arguments, we'll need to parse them later.
933 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000934 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +0000935 = DeclaratorInfo.getTypeObject(0).Fun;
936 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
937 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
938 if (!LateMethod) {
939 // Push this method onto the stack of late-parsed method
940 // declarations.
941 getCurrentClass().MethodDecls.push_back(
942 LateParsedMethodDeclaration(ThisDecl));
943 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +0000944 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +0000945
946 // Add all of the parameters prior to this one (they don't
947 // have default arguments).
948 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
949 for (unsigned I = 0; I < ParamIdx; ++I)
950 LateMethod->DefaultArgs.push_back(
951 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
952 }
953
954 // Add this parameter to the list of parameters (it or may
955 // not have a default argument).
956 LateMethod->DefaultArgs.push_back(
957 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
958 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
959 }
960 }
961}
962
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000963/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
964///
965/// member-declaration:
966/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
967/// function-definition ';'[opt]
968/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
969/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000970/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000971/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000972/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000973///
974/// member-declarator-list:
975/// member-declarator
976/// member-declarator-list ',' member-declarator
977///
978/// member-declarator:
979/// declarator pure-specifier[opt]
980/// declarator constant-initializer[opt]
981/// identifier[opt] ':' constant-expression
982///
Sebastian Redle2b68332009-04-12 17:16:29 +0000983/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000984/// '= 0'
985///
986/// constant-initializer:
987/// '=' constant-expression
988///
Douglas Gregor37b372b2009-08-20 22:52:58 +0000989void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
990 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000991 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000992 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000993 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +0000994 SourceLocation DeclEnd;
995 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000996 return;
997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Chris Lattner682bf922009-03-29 16:50:03 +0000999 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001000 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001001 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001002 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001003 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001004 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001005 return;
1006 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001007
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001008 // Handle: member-declaration ::= '__extension__' member-declaration
1009 if (Tok.is(tok::kw___extension__)) {
1010 // __extension__ silences extension warnings in the subexpression.
1011 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1012 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001013 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001014 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001015
1016 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001017 // FIXME: Check for template aliases
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001019 // Eat 'using'.
1020 SourceLocation UsingLoc = ConsumeToken();
1021
1022 if (Tok.is(tok::kw_namespace)) {
1023 Diag(UsingLoc, diag::err_using_namespace_in_class);
1024 SkipUntil(tok::semi, true, true);
1025 }
1026 else {
1027 SourceLocation DeclEnd;
1028 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001029 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001030 }
1031 return;
1032 }
1033
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001034 SourceLocation DSStart = Tok.getLocation();
1035 // decl-specifier-seq:
1036 // Parse the common declaration-specifiers piece.
1037 DeclSpec DS;
Douglas Gregor37b372b2009-08-20 22:52:58 +00001038 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001039
John McCalldd4a3b02009-09-16 22:47:08 +00001040 Action::MultiTemplateParamsArg TemplateParams(Actions,
1041 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1042 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1043
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001044 if (Tok.is(tok::semi)) {
1045 ConsumeToken();
John McCall67d1a672009-08-06 02:15:43 +00001046
Anders Carlsson41111812009-09-11 17:54:14 +00001047 if (DS.isFriendSpecified()) {
John McCalldd4a3b02009-09-16 22:47:08 +00001048 Actions.ActOnFriendTypeDecl(CurScope, DS, move(TemplateParams));
Anders Carlsson41111812009-09-11 17:54:14 +00001049 } else
Chris Lattner682bf922009-03-29 16:50:03 +00001050 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001051
1052 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001053 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001054
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001055 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001056
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001057 if (Tok.isNot(tok::colon)) {
1058 // Parse the first declarator.
1059 ParseDeclarator(DeclaratorInfo);
1060 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001061 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001062 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001063 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001064 if (Tok.is(tok::semi))
1065 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001066 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001067 }
1068
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001069 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001070 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001071 || (DeclaratorInfo.isFunctionDeclarator() &&
1072 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001073 if (!DeclaratorInfo.isFunctionDeclarator()) {
1074 Diag(Tok, diag::err_func_def_no_params);
1075 ConsumeBrace();
1076 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001077 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001078 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001079
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001080 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1081 Diag(Tok, diag::err_function_declared_typedef);
1082 // This recovery skips the entire function body. It would be nice
1083 // to simply call ParseCXXInlineMethodDef() below, however Sema
1084 // assumes the declarator represents a function, not a typedef.
1085 ConsumeBrace();
1086 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001087 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001088 }
1089
Douglas Gregor37b372b2009-08-20 22:52:58 +00001090 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001091 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001092 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001093 }
1094
1095 // member-declarator-list:
1096 // member-declarator
1097 // member-declarator-list ',' member-declarator
1098
Chris Lattner682bf922009-03-29 16:50:03 +00001099 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001100 OwningExprResult BitfieldSize(Actions);
1101 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001102 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001103
1104 while (1) {
1105
1106 // member-declarator:
1107 // declarator pure-specifier[opt]
1108 // declarator constant-initializer[opt]
1109 // identifier[opt] ':' constant-expression
1110
1111 if (Tok.is(tok::colon)) {
1112 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001113 BitfieldSize = ParseConstantExpression();
1114 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001115 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001118 // pure-specifier:
1119 // '= 0'
1120 //
1121 // constant-initializer:
1122 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001123 //
1124 // defaulted/deleted function-definition:
1125 // '=' 'default' [TODO]
1126 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001127
1128 if (Tok.is(tok::equal)) {
1129 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001130 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1131 ConsumeToken();
1132 Deleted = true;
1133 } else {
1134 Init = ParseInitializer();
1135 if (Init.isInvalid())
1136 SkipUntil(tok::comma, true, true);
1137 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001138 }
1139
1140 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001141 if (Tok.is(tok::kw___attribute)) {
1142 SourceLocation Loc;
1143 AttributeList *AttrList = ParseAttributes(&Loc);
1144 DeclaratorInfo.AddAttributes(AttrList, Loc);
1145 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001146
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001147 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001148 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001149 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001150
1151 DeclPtrTy ThisDecl;
1152 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001153 // TODO: handle initializers, bitfields, 'delete'
1154 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1155 /*IsDefinition*/ false,
1156 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001157 } else {
John McCall67d1a672009-08-06 02:15:43 +00001158 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1159 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001160 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001161 BitfieldSize.release(),
1162 Init.release(),
1163 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001164 }
Chris Lattner682bf922009-03-29 16:50:03 +00001165 if (ThisDecl)
1166 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001167
Douglas Gregor72b505b2008-12-16 21:30:33 +00001168 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001169 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001170 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001171 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001172 }
1173
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001174 // If we don't have a comma, it is either the end of the list (a ';')
1175 // or an error, bail out.
1176 if (Tok.isNot(tok::comma))
1177 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001179 // Consume the comma.
1180 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001182 // Parse the next declarator.
1183 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001184 BitfieldSize = 0;
1185 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001186 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001188 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001189 if (Tok.is(tok::kw___attribute)) {
1190 SourceLocation Loc;
1191 AttributeList *AttrList = ParseAttributes(&Loc);
1192 DeclaratorInfo.AddAttributes(AttrList, Loc);
1193 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001194
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001195 if (Tok.isNot(tok::colon))
1196 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001197 }
1198
1199 if (Tok.is(tok::semi)) {
1200 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001201 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001202 DeclsInGroup.size());
1203 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001204 }
1205
1206 Diag(Tok, diag::err_expected_semi_decl_list);
1207 // Skip to end of block or statement
1208 SkipUntil(tok::r_brace, true, true);
1209 if (Tok.is(tok::semi))
1210 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001211 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001212}
1213
1214/// ParseCXXMemberSpecification - Parse the class definition.
1215///
1216/// member-specification:
1217/// member-declaration member-specification[opt]
1218/// access-specifier ':' member-specification[opt]
1219///
1220void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001221 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001222 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001223 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001224 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001225
Chris Lattner49f28ca2009-03-05 08:00:35 +00001226 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1227 PP.getSourceManager(),
1228 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001230 SourceLocation LBraceLoc = ConsumeBrace();
1231
Douglas Gregor6569d682009-05-27 23:11:45 +00001232 // Determine whether this is a top-level (non-nested) class.
Mike Stump1eb44332009-09-09 15:08:12 +00001233 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregor6569d682009-05-27 23:11:45 +00001234 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001235
1236 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001237 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001238
Douglas Gregor6569d682009-05-27 23:11:45 +00001239 // Note that we are parsing a new (potentially-nested) class definition.
1240 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1241
Douglas Gregorddc29e12009-02-06 22:42:48 +00001242 if (TagDecl)
1243 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1244 else {
1245 SkipUntil(tok::r_brace, false, false);
1246 return;
1247 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001248
1249 // C++ 11p3: Members of a class defined with the keyword class are private
1250 // by default. Members of a class defined with the keywords struct or union
1251 // are public by default.
1252 AccessSpecifier CurAS;
1253 if (TagType == DeclSpec::TST_class)
1254 CurAS = AS_private;
1255 else
1256 CurAS = AS_public;
1257
1258 // While we still have something to read, read the member-declarations.
1259 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1260 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001262 // Check for extraneous top-level semicolon.
1263 if (Tok.is(tok::semi)) {
1264 Diag(Tok, diag::ext_extra_struct_semi);
1265 ConsumeToken();
1266 continue;
1267 }
1268
1269 AccessSpecifier AS = getAccessSpecifierIfPresent();
1270 if (AS != AS_none) {
1271 // Current token is a C++ access specifier.
1272 CurAS = AS;
1273 ConsumeToken();
1274 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1275 continue;
1276 }
1277
Douglas Gregor37b372b2009-08-20 22:52:58 +00001278 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001280 // Parse all the comma separated declarators.
1281 ParseCXXClassMemberDeclaration(CurAS);
1282 }
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001284 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001286 AttributeList *AttrList = 0;
1287 // If attributes exist after class contents, parse them.
1288 if (Tok.is(tok::kw___attribute))
1289 AttrList = ParseAttributes(); // FIXME: where should I put them?
1290
1291 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1292 LBraceLoc, RBraceLoc);
1293
1294 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1295 // complete within function bodies, default arguments,
1296 // exception-specifications, and constructor ctor-initializers (including
1297 // such things in nested classes).
1298 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001299 // FIXME: Only function bodies and constructor ctor-initializers are
1300 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001301 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001302 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001303 // are complete and we can parse the delayed portions of method
1304 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001305 ParseLexedMethodDeclarations(getCurrentClass());
1306 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001307 }
1308
1309 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001310 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001311 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001312
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001313 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001314}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001315
1316/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1317/// which explicitly initializes the members or base classes of a
1318/// class (C++ [class.base.init]). For example, the three initializers
1319/// after the ':' in the Derived constructor below:
1320///
1321/// @code
1322/// class Base { };
1323/// class Derived : Base {
1324/// int x;
1325/// float f;
1326/// public:
1327/// Derived(float f) : Base(), x(17), f(f) { }
1328/// };
1329/// @endcode
1330///
Mike Stump1eb44332009-09-09 15:08:12 +00001331/// [C++] ctor-initializer:
1332/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001333///
Mike Stump1eb44332009-09-09 15:08:12 +00001334/// [C++] mem-initializer-list:
1335/// mem-initializer
1336/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001337void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001338 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1339
1340 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Douglas Gregor7ad83902008-11-05 04:29:56 +00001342 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Douglas Gregor7ad83902008-11-05 04:29:56 +00001344 do {
1345 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001346 if (!MemInit.isInvalid())
1347 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001348
1349 if (Tok.is(tok::comma))
1350 ConsumeToken();
1351 else if (Tok.is(tok::l_brace))
1352 break;
1353 else {
1354 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001355 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001356 SkipUntil(tok::l_brace, true, true);
1357 break;
1358 }
1359 } while (true);
1360
Mike Stump1eb44332009-09-09 15:08:12 +00001361 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001362 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001363}
1364
1365/// ParseMemInitializer - Parse a C++ member initializer, which is
1366/// part of a constructor initializer that explicitly initializes one
1367/// member or base class (C++ [class.base.init]). See
1368/// ParseConstructorInitializer for an example.
1369///
1370/// [C++] mem-initializer:
1371/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001372///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001373/// [C++] mem-initializer-id:
1374/// '::'[opt] nested-name-specifier[opt] class-name
1375/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001376Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001377 // parse '::'[opt] nested-name-specifier[opt]
1378 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001379 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001380 TypeTy *TemplateTypeTy = 0;
1381 if (Tok.is(tok::annot_template_id)) {
1382 TemplateIdAnnotation *TemplateId
1383 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1384 if (TemplateId->Kind == TNK_Type_template) {
1385 AnnotateTemplateIdTokenAsType(&SS);
1386 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1387 TemplateTypeTy = Tok.getAnnotationValue();
1388 }
1389 // FIXME. May need to check for TNK_Dependent_template as well.
1390 }
1391 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001392 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001393 return true;
1394 }
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Douglas Gregor7ad83902008-11-05 04:29:56 +00001396 // Get the identifier. This may be a member name or a class name,
1397 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001398 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001399 SourceLocation IdLoc = ConsumeToken();
1400
1401 // Parse the '('.
1402 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001403 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001404 return true;
1405 }
1406 SourceLocation LParenLoc = ConsumeParen();
1407
1408 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001409 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001410 CommaLocsTy CommaLocs;
1411 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1412 SkipUntil(tok::r_paren);
1413 return true;
1414 }
1415
1416 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1417
Fariborz Jahanian96174332009-07-01 19:21:19 +00001418 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1419 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001420 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001421 ArgExprs.size(), CommaLocs.data(),
1422 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001423}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001424
1425/// ParseExceptionSpecification - Parse a C++ exception-specification
1426/// (C++ [except.spec]).
1427///
Douglas Gregora4745612008-12-01 18:00:20 +00001428/// exception-specification:
1429/// 'throw' '(' type-id-list [opt] ')'
1430/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001431///
Douglas Gregora4745612008-12-01 18:00:20 +00001432/// type-id-list:
1433/// type-id
1434/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001435///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001436bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001437 llvm::SmallVector<TypeTy*, 2>
1438 &Exceptions,
1439 llvm::SmallVector<SourceRange, 2>
1440 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001441 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001442 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001444 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001446 if (!Tok.is(tok::l_paren)) {
1447 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1448 }
1449 SourceLocation LParenLoc = ConsumeParen();
1450
Douglas Gregora4745612008-12-01 18:00:20 +00001451 // Parse throw(...), a Microsoft extension that means "this function
1452 // can throw anything".
1453 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001454 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001455 SourceLocation EllipsisLoc = ConsumeToken();
1456 if (!getLang().Microsoft)
1457 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001458 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001459 return false;
1460 }
1461
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001462 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001463 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001464 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001465 TypeResult Res(ParseTypeName(&Range));
1466 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001467 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001468 Ranges.push_back(Range);
1469 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001470 if (Tok.is(tok::comma))
1471 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001472 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001473 break;
1474 }
1475
Sebastian Redlab197ba2009-02-09 18:23:29 +00001476 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001477 return false;
1478}
Douglas Gregor6569d682009-05-27 23:11:45 +00001479
1480/// \brief We have just started parsing the definition of a new class,
1481/// so push that class onto our stack of classes that is currently
1482/// being parsed.
1483void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001484 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001485 "Nested class without outer class");
1486 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1487}
1488
1489/// \brief Deallocate the given parsed class and all of its nested
1490/// classes.
1491void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1492 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1493 DeallocateParsedClasses(Class->NestedClasses[I]);
1494 delete Class;
1495}
1496
1497/// \brief Pop the top class of the stack of classes that are
1498/// currently being parsed.
1499///
1500/// This routine should be called when we have finished parsing the
1501/// definition of a class, but have not yet popped the Scope
1502/// associated with the class's definition.
1503///
1504/// \returns true if the class we've popped is a top-level class,
1505/// false otherwise.
1506void Parser::PopParsingClass() {
1507 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Douglas Gregor6569d682009-05-27 23:11:45 +00001509 ParsingClass *Victim = ClassStack.top();
1510 ClassStack.pop();
1511 if (Victim->TopLevelClass) {
1512 // Deallocate all of the nested classes of this class,
1513 // recursively: we don't need to keep any of this information.
1514 DeallocateParsedClasses(Victim);
1515 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001516 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001517 assert(!ClassStack.empty() && "Missing top-level class?");
1518
1519 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1520 Victim->NestedClasses.empty()) {
1521 // The victim is a nested class, but we will not need to perform
1522 // any processing after the definition of this class since it has
1523 // no members whose handling was delayed. Therefore, we can just
1524 // remove this nested class.
1525 delete Victim;
1526 return;
1527 }
1528
1529 // This nested class has some members that will need to be processed
1530 // after the top-level class is completely defined. Therefore, add
1531 // it to the list of nested classes within its parent.
1532 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1533 ClassStack.top()->NestedClasses.push_back(Victim);
1534 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1535}