blob: bcf332bf3094ba1ab19e12c51e59d4e52eb677ff [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 Gregord85bea22009-09-26 06:47:28 +0000627 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
628 if (DS.isFriendSpecified()) {
629 // C++ [class.friend]p2:
630 // A class shall not be defined in a friend declaration.
631 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
632 << SourceRange(DS.getFriendSpecLoc());
633
634 // Skip everything up to the semicolon, so that this looks like a proper
635 // friend class (or template thereof) declaration.
636 SkipUntil(tok::semi, true, true);
637 TUK = Action::TUK_Friend;
638 } else {
639 // Okay, this is a class definition.
640 TUK = Action::TUK_Definition;
641 }
642 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000643 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000644 else
John McCall0f434ec2009-07-31 02:45:11 +0000645 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000646
John McCall0f434ec2009-07-31 02:45:11 +0000647 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000648 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000649 Diag(StartLoc, diag::err_anon_type_definition)
650 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000651
652 // Skip the rest of this declarator, up until the comma or semicolon.
653 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000654
655 if (TemplateId)
656 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000657 return;
658 }
659
Douglas Gregorddc29e12009-02-06 22:42:48 +0000660 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000661 Action::DeclResult TagOrTempResult = true; // invalid
662 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000663 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
664
John McCall0f434ec2009-07-31 02:45:11 +0000665 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000666 // to turn that template-id into a type.
667
Douglas Gregor402abb52009-05-28 23:31:59 +0000668 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000669 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000670 // Explicit specialization, class template partial specialization,
671 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000672 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000673 TemplateId->getTemplateArgs(),
674 TemplateId->getTemplateArgIsType(),
675 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000676 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000677 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000678 // This is an explicit instantiation of a class template.
679 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000680 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000681 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000682 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000683 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000684 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000685 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000686 TemplateTy::make(TemplateId->Template),
687 TemplateId->TemplateNameLoc,
688 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000689 TemplateArgsPtr,
690 TemplateId->getTemplateArgLocations(),
Mike Stump1eb44332009-09-09 15:08:12 +0000691 TemplateId->RAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000692 Attr);
John McCallf1bbbb42009-09-04 01:14:41 +0000693 } else if (TUK == Action::TUK_Reference || TUK == Action::TUK_Friend) {
John McCallc4e70192009-09-11 04:59:25 +0000694 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000695 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
696 TemplateId->TemplateNameLoc,
697 TemplateId->LAngleLoc,
698 TemplateArgsPtr,
699 TemplateId->getTemplateArgLocations(),
700 TemplateId->RAngleLoc);
701
John McCallc4e70192009-09-11 04:59:25 +0000702 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
703 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000704 } else {
705 // This is an explicit specialization or a class template
706 // partial specialization.
707 TemplateParameterLists FakedParamLists;
708
709 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
710 // This looks like an explicit instantiation, because we have
711 // something like
712 //
713 // template class Foo<X>
714 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000715 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000716 // meant to be an explicit specialization, but the user forgot
717 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000718 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000719
Mike Stump1eb44332009-09-09 15:08:12 +0000720 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000721 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000722 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000723 diag::err_explicit_instantiation_with_definition)
724 << SourceRange(TemplateInfo.TemplateLoc)
725 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
726
727 // Create a fake template parameter list that contains only
728 // "template<>", so that we treat this construct as a class
729 // template specialization.
730 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000731 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000732 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000733 LAngleLoc,
734 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000735 LAngleLoc));
736 TemplateParams = &FakedParamLists;
737 }
738
739 // Build the class template specialization.
740 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000741 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000742 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000743 TemplateTy::make(TemplateId->Template),
744 TemplateId->TemplateNameLoc,
745 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000746 TemplateArgsPtr,
747 TemplateId->getTemplateArgLocations(),
Mike Stump1eb44332009-09-09 15:08:12 +0000748 TemplateId->RAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000749 Attr,
Mike Stump1eb44332009-09-09 15:08:12 +0000750 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000751 TemplateParams? &(*TemplateParams)[0] : 0,
752 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000753 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000754 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000755 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000756 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000757 // Explicit instantiation of a member of a class template
758 // specialization, e.g.,
759 //
760 // template struct Outer<int>::Inner;
761 //
762 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000763 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000764 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000765 TemplateInfo.TemplateLoc,
766 TagType, StartLoc, SS, Name,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000767 NameLoc, Attr);
768 } else {
769 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000770 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000771 // FIXME: Diagnose this particular error.
772 }
773
John McCallc4e70192009-09-11 04:59:25 +0000774 bool IsDependent = false;
775
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000776 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000777 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000778 Name, NameLoc, Attr, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000779 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000780 TemplateParams? &(*TemplateParams)[0] : 0,
781 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000782 Owned, IsDependent);
783
784 // If ActOnTag said the type was dependent, try again with the
785 // less common call.
786 if (IsDependent)
787 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
788 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000789 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000790
791 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000792 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000793 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000794
795 // If there is a body, parse it and inform the actions module.
796 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000797 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000798 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000799 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000800 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000801 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000802 // FIXME: Complain that we have a base-specifier list but no
803 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000804 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000805 }
806
John McCallc4e70192009-09-11 04:59:25 +0000807 void *Result;
808 if (!TypeResult.isInvalid()) {
809 TagType = DeclSpec::TST_typename;
810 Result = TypeResult.get();
811 Owned = false;
812 } else if (!TagOrTempResult.isInvalid()) {
813 Result = TagOrTempResult.get().getAs<void>();
814 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000815 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000816 return;
817 }
Mike Stump1eb44332009-09-09 15:08:12 +0000818
John McCallfec54012009-08-03 20:12:06 +0000819 const char *PrevSpec = 0;
820 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000821
John McCallfec54012009-08-03 20:12:06 +0000822 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000823 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000824 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000825}
826
Mike Stump1eb44332009-09-09 15:08:12 +0000827/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000828///
829/// base-clause : [C++ class.derived]
830/// ':' base-specifier-list
831/// base-specifier-list:
832/// base-specifier '...'[opt]
833/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000834void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000835 assert(Tok.is(tok::colon) && "Not a base clause");
836 ConsumeToken();
837
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000838 // Build up an array of parsed base specifiers.
839 llvm::SmallVector<BaseTy *, 8> BaseInfo;
840
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000841 while (true) {
842 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000843 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000844 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000845 // Skip the rest of this base specifier, up until the comma or
846 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000847 SkipUntil(tok::comma, tok::l_brace, true, true);
848 } else {
849 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000850 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000851 }
852
853 // If the next token is a comma, consume it and keep reading
854 // base-specifiers.
855 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000857 // Consume the comma.
858 ConsumeToken();
859 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000860
861 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000862 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000863}
864
865/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
866/// one entry in the base class list of a class specifier, for example:
867/// class foo : public bar, virtual private baz {
868/// 'public bar' and 'virtual private baz' are each base-specifiers.
869///
870/// base-specifier: [C++ class.derived]
871/// ::[opt] nested-name-specifier[opt] class-name
872/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
873/// class-name
874/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
875/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000876Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000877 bool IsVirtual = false;
878 SourceLocation StartLoc = Tok.getLocation();
879
880 // Parse the 'virtual' keyword.
881 if (Tok.is(tok::kw_virtual)) {
882 ConsumeToken();
883 IsVirtual = true;
884 }
885
886 // Parse an (optional) access specifier.
887 AccessSpecifier Access = getAccessSpecifierIfPresent();
888 if (Access)
889 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000891 // Parse the 'virtual' keyword (again!), in case it came after the
892 // access specifier.
893 if (Tok.is(tok::kw_virtual)) {
894 SourceLocation VirtualLoc = ConsumeToken();
895 if (IsVirtual) {
896 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000897 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000898 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000899 }
900
901 IsVirtual = true;
902 }
903
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000904 // Parse optional '::' and optional nested-name-specifier.
905 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000906 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000907
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000908 // The location of the base class itself.
909 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000910
911 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000912 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000913 TypeResult BaseType = ParseClassName(EndLocation, &SS);
914 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000915 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000916
917 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000918 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000920 // Notify semantic analysis that we have parsed a complete
921 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000922 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000923 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000924}
925
926/// getAccessSpecifierIfPresent - Determine whether the next token is
927/// a C++ access-specifier.
928///
929/// access-specifier: [C++ class.derived]
930/// 'private'
931/// 'protected'
932/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +0000933AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000934 switch (Tok.getKind()) {
935 default: return AS_none;
936 case tok::kw_private: return AS_private;
937 case tok::kw_protected: return AS_protected;
938 case tok::kw_public: return AS_public;
939 }
940}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000941
Eli Friedmand33133c2009-07-22 21:45:50 +0000942void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
943 DeclPtrTy ThisDecl) {
944 // We just declared a member function. If this member function
945 // has any default arguments, we'll need to parse them later.
946 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000947 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +0000948 = DeclaratorInfo.getTypeObject(0).Fun;
949 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
950 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
951 if (!LateMethod) {
952 // Push this method onto the stack of late-parsed method
953 // declarations.
954 getCurrentClass().MethodDecls.push_back(
955 LateParsedMethodDeclaration(ThisDecl));
956 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +0000957 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +0000958
959 // Add all of the parameters prior to this one (they don't
960 // have default arguments).
961 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
962 for (unsigned I = 0; I < ParamIdx; ++I)
963 LateMethod->DefaultArgs.push_back(
964 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
965 }
966
967 // Add this parameter to the list of parameters (it or may
968 // not have a default argument).
969 LateMethod->DefaultArgs.push_back(
970 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
971 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
972 }
973 }
974}
975
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000976/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
977///
978/// member-declaration:
979/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
980/// function-definition ';'[opt]
981/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
982/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000983/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000984/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000985/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000986///
987/// member-declarator-list:
988/// member-declarator
989/// member-declarator-list ',' member-declarator
990///
991/// member-declarator:
992/// declarator pure-specifier[opt]
993/// declarator constant-initializer[opt]
994/// identifier[opt] ':' constant-expression
995///
Sebastian Redle2b68332009-04-12 17:16:29 +0000996/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000997/// '= 0'
998///
999/// constant-initializer:
1000/// '=' constant-expression
1001///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001002void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1003 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001004 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001005 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001006 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001007 SourceLocation DeclEnd;
1008 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001009 return;
1010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattner682bf922009-03-29 16:50:03 +00001012 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001013 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001014 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001015 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001016 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001017 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001018 return;
1019 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001020
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001021 // Handle: member-declaration ::= '__extension__' member-declaration
1022 if (Tok.is(tok::kw___extension__)) {
1023 // __extension__ silences extension warnings in the subexpression.
1024 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1025 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001026 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001027 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001028
1029 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001030 // FIXME: Check for template aliases
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001032 // Eat 'using'.
1033 SourceLocation UsingLoc = ConsumeToken();
1034
1035 if (Tok.is(tok::kw_namespace)) {
1036 Diag(UsingLoc, diag::err_using_namespace_in_class);
1037 SkipUntil(tok::semi, true, true);
1038 }
1039 else {
1040 SourceLocation DeclEnd;
1041 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001042 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001043 }
1044 return;
1045 }
1046
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001047 SourceLocation DSStart = Tok.getLocation();
1048 // decl-specifier-seq:
1049 // Parse the common declaration-specifiers piece.
1050 DeclSpec DS;
Douglas Gregor37b372b2009-08-20 22:52:58 +00001051 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001052
John McCalldd4a3b02009-09-16 22:47:08 +00001053 Action::MultiTemplateParamsArg TemplateParams(Actions,
1054 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1055 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1056
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001057 if (Tok.is(tok::semi)) {
1058 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001059 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001060 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001061 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001062
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001063 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001064
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001065 if (Tok.isNot(tok::colon)) {
1066 // Parse the first declarator.
1067 ParseDeclarator(DeclaratorInfo);
1068 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001069 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001070 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001071 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001072 if (Tok.is(tok::semi))
1073 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001074 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001075 }
1076
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001077 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001078 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001079 || (DeclaratorInfo.isFunctionDeclarator() &&
1080 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001081 if (!DeclaratorInfo.isFunctionDeclarator()) {
1082 Diag(Tok, diag::err_func_def_no_params);
1083 ConsumeBrace();
1084 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001085 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001086 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001087
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001088 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1089 Diag(Tok, diag::err_function_declared_typedef);
1090 // This recovery skips the entire function body. It would be nice
1091 // to simply call ParseCXXInlineMethodDef() below, however Sema
1092 // assumes the declarator represents a function, not a typedef.
1093 ConsumeBrace();
1094 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001095 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001096 }
1097
Douglas Gregor37b372b2009-08-20 22:52:58 +00001098 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001099 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001100 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001101 }
1102
1103 // member-declarator-list:
1104 // member-declarator
1105 // member-declarator-list ',' member-declarator
1106
Chris Lattner682bf922009-03-29 16:50:03 +00001107 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001108 OwningExprResult BitfieldSize(Actions);
1109 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001110 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001111
1112 while (1) {
1113
1114 // member-declarator:
1115 // declarator pure-specifier[opt]
1116 // declarator constant-initializer[opt]
1117 // identifier[opt] ':' constant-expression
1118
1119 if (Tok.is(tok::colon)) {
1120 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001121 BitfieldSize = ParseConstantExpression();
1122 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001123 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001124 }
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001126 // pure-specifier:
1127 // '= 0'
1128 //
1129 // constant-initializer:
1130 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001131 //
1132 // defaulted/deleted function-definition:
1133 // '=' 'default' [TODO]
1134 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001135
1136 if (Tok.is(tok::equal)) {
1137 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001138 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1139 ConsumeToken();
1140 Deleted = true;
1141 } else {
1142 Init = ParseInitializer();
1143 if (Init.isInvalid())
1144 SkipUntil(tok::comma, true, true);
1145 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001146 }
1147
1148 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001149 if (Tok.is(tok::kw___attribute)) {
1150 SourceLocation Loc;
1151 AttributeList *AttrList = ParseAttributes(&Loc);
1152 DeclaratorInfo.AddAttributes(AttrList, Loc);
1153 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001154
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001155 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001156 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001157 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001158
1159 DeclPtrTy ThisDecl;
1160 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001161 // TODO: handle initializers, bitfields, 'delete'
1162 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1163 /*IsDefinition*/ false,
1164 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001165 } else {
John McCall67d1a672009-08-06 02:15:43 +00001166 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1167 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001168 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001169 BitfieldSize.release(),
1170 Init.release(),
1171 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001172 }
Chris Lattner682bf922009-03-29 16:50:03 +00001173 if (ThisDecl)
1174 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001175
Douglas Gregor72b505b2008-12-16 21:30:33 +00001176 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001177 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001178 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001179 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001180 }
1181
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001182 // If we don't have a comma, it is either the end of the list (a ';')
1183 // or an error, bail out.
1184 if (Tok.isNot(tok::comma))
1185 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001187 // Consume the comma.
1188 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001190 // Parse the next declarator.
1191 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001192 BitfieldSize = 0;
1193 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001194 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001196 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001197 if (Tok.is(tok::kw___attribute)) {
1198 SourceLocation Loc;
1199 AttributeList *AttrList = ParseAttributes(&Loc);
1200 DeclaratorInfo.AddAttributes(AttrList, Loc);
1201 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001202
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001203 if (Tok.isNot(tok::colon))
1204 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001205 }
1206
1207 if (Tok.is(tok::semi)) {
1208 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001209 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001210 DeclsInGroup.size());
1211 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001212 }
1213
1214 Diag(Tok, diag::err_expected_semi_decl_list);
1215 // Skip to end of block or statement
1216 SkipUntil(tok::r_brace, true, true);
1217 if (Tok.is(tok::semi))
1218 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001219 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001220}
1221
1222/// ParseCXXMemberSpecification - Parse the class definition.
1223///
1224/// member-specification:
1225/// member-declaration member-specification[opt]
1226/// access-specifier ':' member-specification[opt]
1227///
1228void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001229 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001230 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001231 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001232 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001233
Chris Lattner49f28ca2009-03-05 08:00:35 +00001234 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1235 PP.getSourceManager(),
1236 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001238 SourceLocation LBraceLoc = ConsumeBrace();
1239
Douglas Gregor6569d682009-05-27 23:11:45 +00001240 // Determine whether this is a top-level (non-nested) class.
Mike Stump1eb44332009-09-09 15:08:12 +00001241 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregor6569d682009-05-27 23:11:45 +00001242 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001243
1244 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001245 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001246
Douglas Gregor6569d682009-05-27 23:11:45 +00001247 // Note that we are parsing a new (potentially-nested) class definition.
1248 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1249
Douglas Gregorddc29e12009-02-06 22:42:48 +00001250 if (TagDecl)
1251 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1252 else {
1253 SkipUntil(tok::r_brace, false, false);
1254 return;
1255 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001256
1257 // C++ 11p3: Members of a class defined with the keyword class are private
1258 // by default. Members of a class defined with the keywords struct or union
1259 // are public by default.
1260 AccessSpecifier CurAS;
1261 if (TagType == DeclSpec::TST_class)
1262 CurAS = AS_private;
1263 else
1264 CurAS = AS_public;
1265
1266 // While we still have something to read, read the member-declarations.
1267 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1268 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001270 // Check for extraneous top-level semicolon.
1271 if (Tok.is(tok::semi)) {
1272 Diag(Tok, diag::ext_extra_struct_semi);
1273 ConsumeToken();
1274 continue;
1275 }
1276
1277 AccessSpecifier AS = getAccessSpecifierIfPresent();
1278 if (AS != AS_none) {
1279 // Current token is a C++ access specifier.
1280 CurAS = AS;
1281 ConsumeToken();
1282 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1283 continue;
1284 }
1285
Douglas Gregor37b372b2009-08-20 22:52:58 +00001286 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001288 // Parse all the comma separated declarators.
1289 ParseCXXClassMemberDeclaration(CurAS);
1290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001292 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001294 AttributeList *AttrList = 0;
1295 // If attributes exist after class contents, parse them.
1296 if (Tok.is(tok::kw___attribute))
1297 AttrList = ParseAttributes(); // FIXME: where should I put them?
1298
1299 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1300 LBraceLoc, RBraceLoc);
1301
1302 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1303 // complete within function bodies, default arguments,
1304 // exception-specifications, and constructor ctor-initializers (including
1305 // such things in nested classes).
1306 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001307 // FIXME: Only function bodies and constructor ctor-initializers are
1308 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001309 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001310 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001311 // are complete and we can parse the delayed portions of method
1312 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001313 ParseLexedMethodDeclarations(getCurrentClass());
1314 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001315 }
1316
1317 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001318 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001319 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001320
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001321 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001322}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001323
1324/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1325/// which explicitly initializes the members or base classes of a
1326/// class (C++ [class.base.init]). For example, the three initializers
1327/// after the ':' in the Derived constructor below:
1328///
1329/// @code
1330/// class Base { };
1331/// class Derived : Base {
1332/// int x;
1333/// float f;
1334/// public:
1335/// Derived(float f) : Base(), x(17), f(f) { }
1336/// };
1337/// @endcode
1338///
Mike Stump1eb44332009-09-09 15:08:12 +00001339/// [C++] ctor-initializer:
1340/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001341///
Mike Stump1eb44332009-09-09 15:08:12 +00001342/// [C++] mem-initializer-list:
1343/// mem-initializer
1344/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001345void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001346 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1347
1348 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Douglas Gregor7ad83902008-11-05 04:29:56 +00001350 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Douglas Gregor7ad83902008-11-05 04:29:56 +00001352 do {
1353 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001354 if (!MemInit.isInvalid())
1355 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001356
1357 if (Tok.is(tok::comma))
1358 ConsumeToken();
1359 else if (Tok.is(tok::l_brace))
1360 break;
1361 else {
1362 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001363 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001364 SkipUntil(tok::l_brace, true, true);
1365 break;
1366 }
1367 } while (true);
1368
Mike Stump1eb44332009-09-09 15:08:12 +00001369 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001370 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001371}
1372
1373/// ParseMemInitializer - Parse a C++ member initializer, which is
1374/// part of a constructor initializer that explicitly initializes one
1375/// member or base class (C++ [class.base.init]). See
1376/// ParseConstructorInitializer for an example.
1377///
1378/// [C++] mem-initializer:
1379/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001380///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001381/// [C++] mem-initializer-id:
1382/// '::'[opt] nested-name-specifier[opt] class-name
1383/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001384Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001385 // parse '::'[opt] nested-name-specifier[opt]
1386 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001387 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001388 TypeTy *TemplateTypeTy = 0;
1389 if (Tok.is(tok::annot_template_id)) {
1390 TemplateIdAnnotation *TemplateId
1391 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1392 if (TemplateId->Kind == TNK_Type_template) {
1393 AnnotateTemplateIdTokenAsType(&SS);
1394 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1395 TemplateTypeTy = Tok.getAnnotationValue();
1396 }
1397 // FIXME. May need to check for TNK_Dependent_template as well.
1398 }
1399 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001400 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001401 return true;
1402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregor7ad83902008-11-05 04:29:56 +00001404 // Get the identifier. This may be a member name or a class name,
1405 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001406 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001407 SourceLocation IdLoc = ConsumeToken();
1408
1409 // Parse the '('.
1410 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001411 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001412 return true;
1413 }
1414 SourceLocation LParenLoc = ConsumeParen();
1415
1416 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001417 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001418 CommaLocsTy CommaLocs;
1419 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1420 SkipUntil(tok::r_paren);
1421 return true;
1422 }
1423
1424 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1425
Fariborz Jahanian96174332009-07-01 19:21:19 +00001426 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1427 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001428 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001429 ArgExprs.size(), CommaLocs.data(),
1430 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001431}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001432
1433/// ParseExceptionSpecification - Parse a C++ exception-specification
1434/// (C++ [except.spec]).
1435///
Douglas Gregora4745612008-12-01 18:00:20 +00001436/// exception-specification:
1437/// 'throw' '(' type-id-list [opt] ')'
1438/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001439///
Douglas Gregora4745612008-12-01 18:00:20 +00001440/// type-id-list:
1441/// type-id
1442/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001443///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001444bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001445 llvm::SmallVector<TypeTy*, 2>
1446 &Exceptions,
1447 llvm::SmallVector<SourceRange, 2>
1448 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001449 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001450 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001452 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001454 if (!Tok.is(tok::l_paren)) {
1455 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1456 }
1457 SourceLocation LParenLoc = ConsumeParen();
1458
Douglas Gregora4745612008-12-01 18:00:20 +00001459 // Parse throw(...), a Microsoft extension that means "this function
1460 // can throw anything".
1461 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001462 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001463 SourceLocation EllipsisLoc = ConsumeToken();
1464 if (!getLang().Microsoft)
1465 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001466 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001467 return false;
1468 }
1469
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001470 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001471 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001472 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001473 TypeResult Res(ParseTypeName(&Range));
1474 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001475 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001476 Ranges.push_back(Range);
1477 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001478 if (Tok.is(tok::comma))
1479 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001480 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001481 break;
1482 }
1483
Sebastian Redlab197ba2009-02-09 18:23:29 +00001484 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001485 return false;
1486}
Douglas Gregor6569d682009-05-27 23:11:45 +00001487
1488/// \brief We have just started parsing the definition of a new class,
1489/// so push that class onto our stack of classes that is currently
1490/// being parsed.
1491void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001492 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001493 "Nested class without outer class");
1494 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1495}
1496
1497/// \brief Deallocate the given parsed class and all of its nested
1498/// classes.
1499void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1500 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1501 DeallocateParsedClasses(Class->NestedClasses[I]);
1502 delete Class;
1503}
1504
1505/// \brief Pop the top class of the stack of classes that are
1506/// currently being parsed.
1507///
1508/// This routine should be called when we have finished parsing the
1509/// definition of a class, but have not yet popped the Scope
1510/// associated with the class's definition.
1511///
1512/// \returns true if the class we've popped is a top-level class,
1513/// false otherwise.
1514void Parser::PopParsingClass() {
1515 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Douglas Gregor6569d682009-05-27 23:11:45 +00001517 ParsingClass *Victim = ClassStack.top();
1518 ClassStack.pop();
1519 if (Victim->TopLevelClass) {
1520 // Deallocate all of the nested classes of this class,
1521 // recursively: we don't need to keep any of this information.
1522 DeallocateParsedClasses(Victim);
1523 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001524 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001525 assert(!ClassStack.empty() && "Missing top-level class?");
1526
1527 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1528 Victim->NestedClasses.empty()) {
1529 // The victim is a nested class, but we will not need to perform
1530 // any processing after the definition of this class since it has
1531 // no members whose handling was delayed. Therefore, we can just
1532 // remove this nested class.
1533 delete Victim;
1534 return;
1535 }
1536
1537 // This nested class has some members that will need to be processed
1538 // after the top-level class is completely defined. Therefore, add
1539 // it to the list of nested classes within its parent.
1540 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1541 ClassStack.top()->NestedClasses.push_back(Victim);
1542 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1543}