blob: 498eaf19cd66c4aceec42f0eb017a1a5e8ff39fa [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
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000017#include "clang/Parse/Scope.h"
Chris Lattnerbc8d5642008-12-18 01:12:00 +000018#include "ExtensionRAIIObject.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000019using namespace clang;
20
21/// ParseNamespace - We know that the current token is a namespace keyword. This
22/// may either be a top level namespace or a block-level namespace alias.
23///
24/// namespace-definition: [C++ 7.3: basic.namespace]
25/// named-namespace-definition
26/// unnamed-namespace-definition
27///
28/// unnamed-namespace-definition:
29/// 'namespace' attributes[opt] '{' namespace-body '}'
30///
31/// named-namespace-definition:
32/// original-namespace-definition
33/// extension-namespace-definition
34///
35/// original-namespace-definition:
36/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
37///
38/// extension-namespace-definition:
39/// 'namespace' original-namespace-name '{' namespace-body '}'
40///
41/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
42/// 'namespace' identifier '=' qualified-namespace-specifier ';'
43///
Chris Lattner97144fc2009-04-02 04:16:50 +000044Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
45 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000046 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000047 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
48
49 SourceLocation IdentLoc;
50 IdentifierInfo *Ident = 0;
51
Chris Lattner04d66662007-10-09 17:33:22 +000052 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000053 Ident = Tok.getIdentifierInfo();
54 IdentLoc = ConsumeToken(); // eat the identifier.
55 }
56
57 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +000058 Action::AttrTy *AttrList = 0;
Chris Lattner04d66662007-10-09 17:33:22 +000059 if (Tok.is(tok::kw___attribute))
Chris Lattner8f08cb72007-08-25 06:57:03 +000060 // FIXME: save these somewhere.
61 AttrList = ParseAttributes();
62
Anders Carlssonf67606a2009-03-28 04:07:16 +000063 if (Tok.is(tok::equal))
Chris Lattner8f08cb72007-08-25 06:57:03 +000064 // FIXME: Verify no attributes were present.
Chris Lattner97144fc2009-04-02 04:16:50 +000065 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Anders Carlssonf67606a2009-03-28 04:07:16 +000066
Chris Lattner51448322009-03-29 14:02:43 +000067 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +000068 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000069 diag::err_expected_ident_lbrace);
70 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 }
72
Chris Lattner51448322009-03-29 14:02:43 +000073 SourceLocation LBrace = ConsumeBrace();
74
75 // Enter a scope for the namespace.
76 ParseScope NamespaceScope(this, Scope::DeclScope);
77
78 DeclPtrTy NamespcDecl =
79 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
80
81 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
82 PP.getSourceManager(),
83 "parsing namespace");
84
85 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
86 ParseExternalDeclaration();
87
88 // Leave the namespace scope.
89 NamespaceScope.Exit();
90
Chris Lattner97144fc2009-04-02 04:16:50 +000091 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
92 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +000093
Chris Lattner97144fc2009-04-02 04:16:50 +000094 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +000095 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +000096}
Chris Lattnerc6fdc342008-01-12 07:05:38 +000097
Anders Carlssonf67606a2009-03-28 04:07:16 +000098/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
99/// alias definition.
100///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000101Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
102 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000103 IdentifierInfo *Alias,
104 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000105 assert(Tok.is(tok::equal) && "Not equal token");
106
107 ConsumeToken(); // eat the '='.
108
109 CXXScopeSpec SS;
110 // Parse (optional) nested-name-specifier.
111 ParseOptionalCXXScopeSpecifier(SS);
112
113 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
114 Diag(Tok, diag::err_expected_namespace_name);
115 // Skip to end of the definition and eat the ';'.
116 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000117 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000118 }
119
120 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000121 IdentifierInfo *Ident = Tok.getIdentifierInfo();
122 SourceLocation IdentLoc = ConsumeToken();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000123
124 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000125 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000126 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
127 "", tok::semi);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000128
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000129 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
130 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000131}
132
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000133/// ParseLinkage - We know that the current token is a string_literal
134/// and just before that, that extern was seen.
135///
136/// linkage-specification: [C++ 7.5p2: dcl.link]
137/// 'extern' string-literal '{' declaration-seq[opt] '}'
138/// 'extern' string-literal declaration
139///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000140Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000141 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000142 llvm::SmallVector<char, 8> LangBuffer;
143 // LangBuffer is guaranteed to be big enough.
144 LangBuffer.resize(Tok.getLength());
145 const char *LangBufPtr = &LangBuffer[0];
146 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
147
148 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000149
Douglas Gregor074149e2009-01-05 19:45:36 +0000150 ParseScope LinkageScope(this, Scope::DeclScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000151 DeclPtrTy LinkageSpec
Douglas Gregor074149e2009-01-05 19:45:36 +0000152 = Actions.ActOnStartLinkageSpecification(CurScope,
153 /*FIXME: */SourceLocation(),
154 Loc, LangBufPtr, StrSize,
155 Tok.is(tok::l_brace)? Tok.getLocation()
156 : SourceLocation());
157
158 if (Tok.isNot(tok::l_brace)) {
159 ParseDeclarationOrFunctionDefinition();
160 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
161 SourceLocation());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000162 }
163
164 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000165 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000166 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000167 }
168
Douglas Gregorf44515a2008-12-16 22:23:02 +0000169 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000170 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000171}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000172
Douglas Gregorf780abc2008-12-30 03:27:21 +0000173/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
174/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000175Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
176 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000177 assert(Tok.is(tok::kw_using) && "Not using token");
178
179 // Eat 'using'.
180 SourceLocation UsingLoc = ConsumeToken();
181
Chris Lattner2f274772009-01-06 06:55:51 +0000182 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000183 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner97144fc2009-04-02 04:16:50 +0000184 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner2f274772009-01-06 06:55:51 +0000185
186 // Otherwise, it must be using-declaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000187 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000188}
189
190/// ParseUsingDirective - Parse C++ using-directive, assumes
191/// that current token is 'namespace' and 'using' was already parsed.
192///
193/// using-directive: [C++ 7.3.p4: namespace.udir]
194/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
195/// namespace-name ;
196/// [GNU] using-directive:
197/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
198/// namespace-name attributes[opt] ;
199///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000200Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000201 SourceLocation UsingLoc,
202 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000203 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
204
205 // Eat 'namespace'.
206 SourceLocation NamespcLoc = ConsumeToken();
207
208 CXXScopeSpec SS;
209 // Parse (optional) nested-name-specifier.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000210 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000211
212 AttributeList *AttrList = 0;
213 IdentifierInfo *NamespcName = 0;
214 SourceLocation IdentLoc = SourceLocation();
215
216 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000217 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000218 Diag(Tok, diag::err_expected_namespace_name);
219 // If there was invalid namespace name, skip to end of decl, and eat ';'.
220 SkipUntil(tok::semi);
221 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000222 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000223 }
Chris Lattner823c44e2009-01-06 07:27:21 +0000224
225 // Parse identifier.
226 NamespcName = Tok.getIdentifierInfo();
227 IdentLoc = ConsumeToken();
228
229 // Parse (optional) attributes (most likely GNU strong-using extension).
230 if (Tok.is(tok::kw___attribute))
231 AttrList = ParseAttributes();
232
233 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000234 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000235 ExpectAndConsume(tok::semi,
236 AttrList ? diag::err_expected_semi_after_attribute_list :
237 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000238
239 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000240 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000241}
242
243/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
244/// 'using' was already seen.
245///
246/// using-declaration: [C++ 7.3.p3: namespace.udecl]
247/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
248/// unqualified-id [TODO]
249/// 'using' :: unqualified-id [TODO]
250///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000251Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000252 SourceLocation UsingLoc,
253 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000254 assert(false && "Not implemented");
255 // FIXME: Implement parsing.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000256 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000257}
258
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000259/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
260///
261/// static_assert-declaration:
262/// static_assert ( constant-expression , string-literal ) ;
263///
Chris Lattner97144fc2009-04-02 04:16:50 +0000264Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000265 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
266 SourceLocation StaticAssertLoc = ConsumeToken();
267
268 if (Tok.isNot(tok::l_paren)) {
269 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000270 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000271 }
272
273 SourceLocation LParenLoc = ConsumeParen();
274
275 OwningExprResult AssertExpr(ParseConstantExpression());
276 if (AssertExpr.isInvalid()) {
277 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000278 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000279 }
280
Anders Carlssonad5f9602009-03-13 23:29:20 +0000281 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000282 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000283
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000284 if (Tok.isNot(tok::string_literal)) {
285 Diag(Tok, diag::err_expected_string_literal);
286 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000287 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000288 }
289
290 OwningExprResult AssertMessage(ParseStringLiteralExpression());
291 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000292 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000293
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000294 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000295
Chris Lattner97144fc2009-04-02 04:16:50 +0000296 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000297 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
298
Anders Carlssonad5f9602009-03-13 23:29:20 +0000299 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000300 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000301}
302
Douglas Gregor42a552f2008-11-05 20:51:48 +0000303/// ParseClassName - Parse a C++ class-name, which names a class. Note
304/// that we only check that the result names a type; semantic analysis
305/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000306/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000307/// found.
308///
309/// class-name: [C++ 9.1]
310/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000311/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000312///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000313Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
314 const CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000315 // Check whether we have a template-id that names a type.
316 if (Tok.is(tok::annot_template_id)) {
317 TemplateIdAnnotation *TemplateId
318 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000319 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000320 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000321
322 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
323 TypeTy *Type = Tok.getAnnotationValue();
324 EndLocation = Tok.getAnnotationEndLoc();
325 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000326
327 if (Type)
328 return Type;
329 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000330 }
331
332 // Fall through to produce an error below.
333 }
334
Douglas Gregor42a552f2008-11-05 20:51:48 +0000335 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000336 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000337 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000338 }
339
340 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000341 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
342 Tok.getLocation(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000343 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000344 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000345 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000346 }
347
348 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000349 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000350 return Type;
351}
352
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000353/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
354/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
355/// until we reach the start of a definition or see a token that
356/// cannot start a definition.
357///
358/// class-specifier: [C++ class]
359/// class-head '{' member-specification[opt] '}'
360/// class-head '{' member-specification[opt] '}' attributes[opt]
361/// class-head:
362/// class-key identifier[opt] base-clause[opt]
363/// class-key nested-name-specifier identifier base-clause[opt]
364/// class-key nested-name-specifier[opt] simple-template-id
365/// base-clause[opt]
366/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
367/// [GNU] class-key attributes[opt] nested-name-specifier
368/// identifier base-clause[opt]
369/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
370/// simple-template-id base-clause[opt]
371/// class-key:
372/// 'class'
373/// 'struct'
374/// 'union'
375///
376/// elaborated-type-specifier: [C++ dcl.type.elab]
377/// class-key ::[opt] nested-name-specifier[opt] identifier
378/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
379/// simple-template-id
380///
381/// Note that the C++ class-specifier and elaborated-type-specifier,
382/// together, subsume the C99 struct-or-union-specifier:
383///
384/// struct-or-union-specifier: [C99 6.7.2.1]
385/// struct-or-union identifier[opt] '{' struct-contents '}'
386/// struct-or-union identifier
387/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
388/// '}' attributes[opt]
389/// [GNU] struct-or-union attributes[opt] identifier
390/// struct-or-union:
391/// 'struct'
392/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000393void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
394 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000395 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000396 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000397 DeclSpec::TST TagType;
398 if (TagTokKind == tok::kw_struct)
399 TagType = DeclSpec::TST_struct;
400 else if (TagTokKind == tok::kw_class)
401 TagType = DeclSpec::TST_class;
402 else {
403 assert(TagTokKind == tok::kw_union && "Not a class specifier");
404 TagType = DeclSpec::TST_union;
405 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000406
407 AttributeList *Attr = 0;
408 // If attributes exist after tag, parse them.
409 if (Tok.is(tok::kw___attribute))
410 Attr = ParseAttributes();
411
Steve Narofff59e17e2008-12-24 20:59:21 +0000412 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000413 if (Tok.is(tok::kw___declspec))
414 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Narofff59e17e2008-12-24 20:59:21 +0000415
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000416 // Parse the (optional) nested-name-specifier.
417 CXXScopeSpec SS;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000418 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
419 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000420 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000421
422 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000423 IdentifierInfo *Name = 0;
424 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000425 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000426 if (Tok.is(tok::identifier)) {
427 Name = Tok.getIdentifierInfo();
428 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000429 } else if (Tok.is(tok::annot_template_id)) {
430 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
431 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000432
Douglas Gregorc45c2322009-03-31 00:43:58 +0000433 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000434 // The template-name in the simple-template-id refers to
435 // something other than a class template. Give an appropriate
436 // error message and skip to the ';'.
437 SourceRange Range(NameLoc);
438 if (SS.isNotEmpty())
439 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000440
Douglas Gregor39a8de12009-02-25 19:37:18 +0000441 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
442 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000443
Douglas Gregor39a8de12009-02-25 19:37:18 +0000444 DS.SetTypeSpecError();
445 SkipUntil(tok::semi, false, true);
446 TemplateId->Destroy();
447 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000448 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000449 }
450
451 // There are three options here. If we have 'struct foo;', then
452 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000453 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000454 // something like 'struct foo xyz', a reference.
455 Action::TagKind TK;
456 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
457 TK = Action::TK_Definition;
Anders Carlsson5dc2af12009-05-11 22:25:03 +0000458 else if (Tok.is(tok::semi) && !DS.isFriendSpecified())
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000459 TK = Action::TK_Declaration;
460 else
461 TK = Action::TK_Reference;
462
Douglas Gregor39a8de12009-02-25 19:37:18 +0000463 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000464 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000465 Diag(StartLoc, diag::err_anon_type_definition)
466 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000467
468 // Skip the rest of this declarator, up until the comma or semicolon.
469 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000470
471 if (TemplateId)
472 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000473 return;
474 }
475
Douglas Gregorddc29e12009-02-06 22:42:48 +0000476 // Create the tag portion of the class or class template.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000477 Action::DeclResult TagOrTempResult;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000478 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
479
480 // FIXME: When TK == TK_Reference and we have a template-id, we need
481 // to turn that template-id into a type.
482
Douglas Gregor402abb52009-05-28 23:31:59 +0000483 bool Owned = false;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000484 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000485 // Explicit specialization, class template partial specialization,
486 // or explicit instantiation.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000487 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
488 TemplateId->getTemplateArgs(),
489 TemplateId->getTemplateArgIsType(),
490 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000491 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
492 TK == Action::TK_Declaration) {
493 // This is an explicit instantiation of a class template.
494 TagOrTempResult
495 = Actions.ActOnExplicitInstantiation(CurScope,
496 TemplateInfo.TemplateLoc,
497 TagType,
498 StartLoc,
499 SS,
500 TemplateTy::make(TemplateId->Template),
501 TemplateId->TemplateNameLoc,
502 TemplateId->LAngleLoc,
503 TemplateArgsPtr,
504 TemplateId->getTemplateArgLocations(),
505 TemplateId->RAngleLoc,
506 Attr);
507 } else {
508 // This is an explicit specialization or a class template
509 // partial specialization.
510 TemplateParameterLists FakedParamLists;
511
512 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
513 // This looks like an explicit instantiation, because we have
514 // something like
515 //
516 // template class Foo<X>
517 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000518 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000519 // meant to be an explicit specialization, but the user forgot
520 // the '<>' after 'template'.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000521 assert(TK == Action::TK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000522
523 SourceLocation LAngleLoc
524 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
525 Diag(TemplateId->TemplateNameLoc,
526 diag::err_explicit_instantiation_with_definition)
527 << SourceRange(TemplateInfo.TemplateLoc)
528 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
529
530 // Create a fake template parameter list that contains only
531 // "template<>", so that we treat this construct as a class
532 // template specialization.
533 FakedParamLists.push_back(
534 Actions.ActOnTemplateParameterList(0, SourceLocation(),
535 TemplateInfo.TemplateLoc,
536 LAngleLoc,
537 0, 0,
538 LAngleLoc));
539 TemplateParams = &FakedParamLists;
540 }
541
542 // Build the class template specialization.
543 TagOrTempResult
544 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000545 StartLoc, SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000546 TemplateTy::make(TemplateId->Template),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000547 TemplateId->TemplateNameLoc,
548 TemplateId->LAngleLoc,
549 TemplateArgsPtr,
550 TemplateId->getTemplateArgLocations(),
551 TemplateId->RAngleLoc,
552 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000553 Action::MultiTemplateParamsArg(Actions,
554 TemplateParams? &(*TemplateParams)[0] : 0,
555 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000556 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000557 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000558 } else if (TemplateParams && TK != Action::TK_Reference) {
559 // Class template declaration or definition.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000560 TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
561 StartLoc, SS, Name, NameLoc,
562 Attr,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000563 Action::MultiTemplateParamsArg(Actions,
564 &(*TemplateParams)[0],
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000565 TemplateParams->size()),
566 AS);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000567 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
568 TK == Action::TK_Declaration) {
569 // Explicit instantiation of a member of a class template
570 // specialization, e.g.,
571 //
572 // template struct Outer<int>::Inner;
573 //
574 TagOrTempResult
575 = Actions.ActOnExplicitInstantiation(CurScope,
576 TemplateInfo.TemplateLoc,
577 TagType, StartLoc, SS, Name,
578 NameLoc, Attr);
579 } else {
580 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
581 TK == Action::TK_Definition) {
582 // FIXME: Diagnose this particular error.
583 }
584
585 // Declaration or definition of a class type
586 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS,
Douglas Gregor402abb52009-05-28 23:31:59 +0000587 Name, NameLoc, Attr, AS, Owned);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000588 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000589
590 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000591 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000592 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000593
594 // If there is a body, parse it and inform the actions module.
595 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000596 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000597 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000598 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000599 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000600 else if (TK == Action::TK_Definition) {
601 // FIXME: Complain that we have a base-specifier list but no
602 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000603 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000604 }
605
606 const char *PrevSpec = 0;
Anders Carlsson66e99772009-05-11 22:27:47 +0000607 if (TagOrTempResult.isInvalid()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000608 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000609 return;
610 }
611
Anders Carlsson66e99772009-05-11 22:27:47 +0000612 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
Douglas Gregor402abb52009-05-28 23:31:59 +0000613 TagOrTempResult.get().getAs<void>(), Owned))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000614 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Anders Carlssond4f551b2009-05-11 22:42:30 +0000615
616 if (DS.isFriendSpecified())
617 Actions.ActOnFriendDecl(CurScope, DS.getFriendSpecLoc(),
618 TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000619}
620
621/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
622///
623/// base-clause : [C++ class.derived]
624/// ':' base-specifier-list
625/// base-specifier-list:
626/// base-specifier '...'[opt]
627/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000628void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000629 assert(Tok.is(tok::colon) && "Not a base clause");
630 ConsumeToken();
631
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000632 // Build up an array of parsed base specifiers.
633 llvm::SmallVector<BaseTy *, 8> BaseInfo;
634
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000635 while (true) {
636 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000637 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000638 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000639 // Skip the rest of this base specifier, up until the comma or
640 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000641 SkipUntil(tok::comma, tok::l_brace, true, true);
642 } else {
643 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000644 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000645 }
646
647 // If the next token is a comma, consume it and keep reading
648 // base-specifiers.
649 if (Tok.isNot(tok::comma)) break;
650
651 // Consume the comma.
652 ConsumeToken();
653 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000654
655 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000656 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000657}
658
659/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
660/// one entry in the base class list of a class specifier, for example:
661/// class foo : public bar, virtual private baz {
662/// 'public bar' and 'virtual private baz' are each base-specifiers.
663///
664/// base-specifier: [C++ class.derived]
665/// ::[opt] nested-name-specifier[opt] class-name
666/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
667/// class-name
668/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
669/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000670Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000671 bool IsVirtual = false;
672 SourceLocation StartLoc = Tok.getLocation();
673
674 // Parse the 'virtual' keyword.
675 if (Tok.is(tok::kw_virtual)) {
676 ConsumeToken();
677 IsVirtual = true;
678 }
679
680 // Parse an (optional) access specifier.
681 AccessSpecifier Access = getAccessSpecifierIfPresent();
682 if (Access)
683 ConsumeToken();
684
685 // Parse the 'virtual' keyword (again!), in case it came after the
686 // access specifier.
687 if (Tok.is(tok::kw_virtual)) {
688 SourceLocation VirtualLoc = ConsumeToken();
689 if (IsVirtual) {
690 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000691 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000692 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000693 }
694
695 IsVirtual = true;
696 }
697
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000698 // Parse optional '::' and optional nested-name-specifier.
699 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000700 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000701
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000702 // The location of the base class itself.
703 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000704
705 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000706 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000707 TypeResult BaseType = ParseClassName(EndLocation, &SS);
708 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000709 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000710
711 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000712 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000713
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000714 // Notify semantic analysis that we have parsed a complete
715 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000716 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000717 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000718}
719
720/// getAccessSpecifierIfPresent - Determine whether the next token is
721/// a C++ access-specifier.
722///
723/// access-specifier: [C++ class.derived]
724/// 'private'
725/// 'protected'
726/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000727AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000728{
729 switch (Tok.getKind()) {
730 default: return AS_none;
731 case tok::kw_private: return AS_private;
732 case tok::kw_protected: return AS_protected;
733 case tok::kw_public: return AS_public;
734 }
735}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000736
737/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
738///
739/// member-declaration:
740/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
741/// function-definition ';'[opt]
742/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
743/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000744/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000745/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000746/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000747///
748/// member-declarator-list:
749/// member-declarator
750/// member-declarator-list ',' member-declarator
751///
752/// member-declarator:
753/// declarator pure-specifier[opt]
754/// declarator constant-initializer[opt]
755/// identifier[opt] ':' constant-expression
756///
Sebastian Redle2b68332009-04-12 17:16:29 +0000757/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000758/// '= 0'
759///
760/// constant-initializer:
761/// '=' constant-expression
762///
Chris Lattner682bf922009-03-29 16:50:03 +0000763void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000764 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000765 if (Tok.is(tok::kw_static_assert)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000766 SourceLocation DeclEnd;
767 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000768 return;
769 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000770
Chris Lattner682bf922009-03-29 16:50:03 +0000771 if (Tok.is(tok::kw_template)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000772 SourceLocation DeclEnd;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000773 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
774 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000775 return;
776 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000777
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000778 // Handle: member-declaration ::= '__extension__' member-declaration
779 if (Tok.is(tok::kw___extension__)) {
780 // __extension__ silences extension warnings in the subexpression.
781 ExtensionRAIIObject O(Diags); // Use RAII to do this.
782 ConsumeToken();
783 return ParseCXXClassMemberDeclaration(AS);
784 }
785
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000786 SourceLocation DSStart = Tok.getLocation();
787 // decl-specifier-seq:
788 // Parse the common declaration-specifiers piece.
789 DeclSpec DS;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000790 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000791
792 if (Tok.is(tok::semi)) {
793 ConsumeToken();
794 // C++ 9.2p7: The member-declarator-list can be omitted only after a
795 // class-specifier or an enum-specifier or in a friend declaration.
796 // FIXME: Friend declarations.
797 switch (DS.getTypeSpecType()) {
Chris Lattner682bf922009-03-29 16:50:03 +0000798 case DeclSpec::TST_struct:
799 case DeclSpec::TST_union:
800 case DeclSpec::TST_class:
801 case DeclSpec::TST_enum:
802 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
803 return;
804 default:
805 Diag(DSStart, diag::err_no_declarators);
806 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000807 }
808 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000809
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000810 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000811
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000812 if (Tok.isNot(tok::colon)) {
813 // Parse the first declarator.
814 ParseDeclarator(DeclaratorInfo);
815 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000816 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000817 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000818 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000819 if (Tok.is(tok::semi))
820 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000821 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000822 }
823
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000824 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000825 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +0000826 || (DeclaratorInfo.isFunctionDeclarator() &&
827 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000828 if (!DeclaratorInfo.isFunctionDeclarator()) {
829 Diag(Tok, diag::err_func_def_no_params);
830 ConsumeBrace();
831 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000832 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000833 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000834
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000835 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
836 Diag(Tok, diag::err_function_declared_typedef);
837 // This recovery skips the entire function body. It would be nice
838 // to simply call ParseCXXInlineMethodDef() below, however Sema
839 // assumes the declarator represents a function, not a typedef.
840 ConsumeBrace();
841 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000842 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000843 }
844
Chris Lattner682bf922009-03-29 16:50:03 +0000845 ParseCXXInlineMethodDef(AS, DeclaratorInfo);
846 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000847 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000848 }
849
850 // member-declarator-list:
851 // member-declarator
852 // member-declarator-list ',' member-declarator
853
Chris Lattner682bf922009-03-29 16:50:03 +0000854 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000855 OwningExprResult BitfieldSize(Actions);
856 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +0000857 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000858
859 while (1) {
860
861 // member-declarator:
862 // declarator pure-specifier[opt]
863 // declarator constant-initializer[opt]
864 // identifier[opt] ':' constant-expression
865
866 if (Tok.is(tok::colon)) {
867 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000868 BitfieldSize = ParseConstantExpression();
869 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000870 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000871 }
872
873 // pure-specifier:
874 // '= 0'
875 //
876 // constant-initializer:
877 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +0000878 //
879 // defaulted/deleted function-definition:
880 // '=' 'default' [TODO]
881 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000882
883 if (Tok.is(tok::equal)) {
884 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +0000885 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
886 ConsumeToken();
887 Deleted = true;
888 } else {
889 Init = ParseInitializer();
890 if (Init.isInvalid())
891 SkipUntil(tok::comma, true, true);
892 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000893 }
894
895 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000896 if (Tok.is(tok::kw___attribute)) {
897 SourceLocation Loc;
898 AttributeList *AttrList = ParseAttributes(&Loc);
899 DeclaratorInfo.AddAttributes(AttrList, Loc);
900 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000901
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000902 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +0000903 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000904 // See Sema::ActOnCXXMemberDeclarator for details.
Chris Lattner682bf922009-03-29 16:50:03 +0000905 DeclPtrTy ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
906 DeclaratorInfo,
907 BitfieldSize.release(),
Sebastian Redle2b68332009-04-12 17:16:29 +0000908 Init.release(),
909 Deleted);
Chris Lattner682bf922009-03-29 16:50:03 +0000910 if (ThisDecl)
911 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000912
Douglas Gregor72b505b2008-12-16 21:30:33 +0000913 if (DeclaratorInfo.isFunctionDeclarator() &&
914 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
915 != DeclSpec::SCS_typedef) {
916 // We just declared a member function. If this member function
917 // has any default arguments, we'll need to parse them later.
918 LateParsedMethodDeclaration *LateMethod = 0;
919 DeclaratorChunk::FunctionTypeInfo &FTI
920 = DeclaratorInfo.getTypeObject(0).Fun;
921 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
922 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
923 if (!LateMethod) {
924 // Push this method onto the stack of late-parsed method
925 // declarations.
Douglas Gregor6569d682009-05-27 23:11:45 +0000926 getCurrentClass().MethodDecls.push_back(
Chris Lattner682bf922009-03-29 16:50:03 +0000927 LateParsedMethodDeclaration(ThisDecl));
Douglas Gregor6569d682009-05-27 23:11:45 +0000928 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000929
930 // Add all of the parameters prior to this one (they don't
931 // have default arguments).
932 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
933 for (unsigned I = 0; I < ParamIdx; ++I)
934 LateMethod->DefaultArgs.push_back(
935 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
936 }
937
938 // Add this parameter to the list of parameters (it or may
939 // not have a default argument).
940 LateMethod->DefaultArgs.push_back(
941 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
942 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
943 }
944 }
945 }
946
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000947 // If we don't have a comma, it is either the end of the list (a ';')
948 // or an error, bail out.
949 if (Tok.isNot(tok::comma))
950 break;
951
952 // Consume the comma.
953 ConsumeToken();
954
955 // Parse the next declarator.
956 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000957 BitfieldSize = 0;
958 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +0000959 Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000960
961 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000962 if (Tok.is(tok::kw___attribute)) {
963 SourceLocation Loc;
964 AttributeList *AttrList = ParseAttributes(&Loc);
965 DeclaratorInfo.AddAttributes(AttrList, Loc);
966 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000967
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000968 if (Tok.isNot(tok::colon))
969 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000970 }
971
972 if (Tok.is(tok::semi)) {
973 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +0000974 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +0000975 DeclsInGroup.size());
976 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000977 }
978
979 Diag(Tok, diag::err_expected_semi_decl_list);
980 // Skip to end of block or statement
981 SkipUntil(tok::r_brace, true, true);
982 if (Tok.is(tok::semi))
983 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000984 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000985}
986
987/// ParseCXXMemberSpecification - Parse the class definition.
988///
989/// member-specification:
990/// member-declaration member-specification[opt]
991/// access-specifier ':' member-specification[opt]
992///
993void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000994 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000995 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000996 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000997 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000998
Chris Lattner49f28ca2009-03-05 08:00:35 +0000999 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1000 PP.getSourceManager(),
1001 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001002
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001003 SourceLocation LBraceLoc = ConsumeBrace();
1004
Douglas Gregor6569d682009-05-27 23:11:45 +00001005 // Determine whether this is a top-level (non-nested) class.
1006 bool TopLevelClass = ClassStack.empty() ||
1007 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001008
1009 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001010 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001011
Douglas Gregor6569d682009-05-27 23:11:45 +00001012 // Note that we are parsing a new (potentially-nested) class definition.
1013 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1014
Douglas Gregorddc29e12009-02-06 22:42:48 +00001015 if (TagDecl)
1016 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1017 else {
1018 SkipUntil(tok::r_brace, false, false);
1019 return;
1020 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001021
1022 // C++ 11p3: Members of a class defined with the keyword class are private
1023 // by default. Members of a class defined with the keywords struct or union
1024 // are public by default.
1025 AccessSpecifier CurAS;
1026 if (TagType == DeclSpec::TST_class)
1027 CurAS = AS_private;
1028 else
1029 CurAS = AS_public;
1030
1031 // While we still have something to read, read the member-declarations.
1032 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1033 // Each iteration of this loop reads one member-declaration.
1034
1035 // Check for extraneous top-level semicolon.
1036 if (Tok.is(tok::semi)) {
1037 Diag(Tok, diag::ext_extra_struct_semi);
1038 ConsumeToken();
1039 continue;
1040 }
1041
1042 AccessSpecifier AS = getAccessSpecifierIfPresent();
1043 if (AS != AS_none) {
1044 // Current token is a C++ access specifier.
1045 CurAS = AS;
1046 ConsumeToken();
1047 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1048 continue;
1049 }
1050
1051 // Parse all the comma separated declarators.
1052 ParseCXXClassMemberDeclaration(CurAS);
1053 }
1054
1055 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1056
1057 AttributeList *AttrList = 0;
1058 // If attributes exist after class contents, parse them.
1059 if (Tok.is(tok::kw___attribute))
1060 AttrList = ParseAttributes(); // FIXME: where should I put them?
1061
1062 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1063 LBraceLoc, RBraceLoc);
1064
1065 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1066 // complete within function bodies, default arguments,
1067 // exception-specifications, and constructor ctor-initializers (including
1068 // such things in nested classes).
1069 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001070 // FIXME: Only function bodies and constructor ctor-initializers are
1071 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001072 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001073 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001074 // are complete and we can parse the delayed portions of method
1075 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001076 ParseLexedMethodDeclarations(getCurrentClass());
1077 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001078 }
1079
1080 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001081 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001082 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001083
Douglas Gregor72de6672009-01-08 20:45:30 +00001084 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001085}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001086
1087/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1088/// which explicitly initializes the members or base classes of a
1089/// class (C++ [class.base.init]). For example, the three initializers
1090/// after the ':' in the Derived constructor below:
1091///
1092/// @code
1093/// class Base { };
1094/// class Derived : Base {
1095/// int x;
1096/// float f;
1097/// public:
1098/// Derived(float f) : Base(), x(17), f(f) { }
1099/// };
1100/// @endcode
1101///
1102/// [C++] ctor-initializer:
1103/// ':' mem-initializer-list
1104///
1105/// [C++] mem-initializer-list:
1106/// mem-initializer
1107/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001108void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001109 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1110
1111 SourceLocation ColonLoc = ConsumeToken();
1112
1113 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1114
1115 do {
1116 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001117 if (!MemInit.isInvalid())
1118 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001119
1120 if (Tok.is(tok::comma))
1121 ConsumeToken();
1122 else if (Tok.is(tok::l_brace))
1123 break;
1124 else {
1125 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001126 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001127 SkipUntil(tok::l_brace, true, true);
1128 break;
1129 }
1130 } while (true);
1131
1132 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001133 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001134}
1135
1136/// ParseMemInitializer - Parse a C++ member initializer, which is
1137/// part of a constructor initializer that explicitly initializes one
1138/// member or base class (C++ [class.base.init]). See
1139/// ParseConstructorInitializer for an example.
1140///
1141/// [C++] mem-initializer:
1142/// mem-initializer-id '(' expression-list[opt] ')'
1143///
1144/// [C++] mem-initializer-id:
1145/// '::'[opt] nested-name-specifier[opt] class-name
1146/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001147Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001148 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1149
1150 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001151 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001152 return true;
1153 }
1154
1155 // Get the identifier. This may be a member name or a class name,
1156 // but we'll let the semantic analysis determine which it is.
1157 IdentifierInfo *II = Tok.getIdentifierInfo();
1158 SourceLocation IdLoc = ConsumeToken();
1159
1160 // Parse the '('.
1161 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001162 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001163 return true;
1164 }
1165 SourceLocation LParenLoc = ConsumeParen();
1166
1167 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001168 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001169 CommaLocsTy CommaLocs;
1170 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1171 SkipUntil(tok::r_paren);
1172 return true;
1173 }
1174
1175 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1176
Sebastian Redla55e52c2008-11-25 22:21:31 +00001177 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1178 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001179 ArgExprs.size(), CommaLocs.data(),
1180 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001181}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001182
1183/// ParseExceptionSpecification - Parse a C++ exception-specification
1184/// (C++ [except.spec]).
1185///
Douglas Gregora4745612008-12-01 18:00:20 +00001186/// exception-specification:
1187/// 'throw' '(' type-id-list [opt] ')'
1188/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001189///
Douglas Gregora4745612008-12-01 18:00:20 +00001190/// type-id-list:
1191/// type-id
1192/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001193///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001194bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001195 llvm::SmallVector<TypeTy*, 2>
1196 &Exceptions,
1197 llvm::SmallVector<SourceRange, 2>
1198 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001199 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001200 assert(Tok.is(tok::kw_throw) && "expected throw");
1201
1202 SourceLocation ThrowLoc = ConsumeToken();
1203
1204 if (!Tok.is(tok::l_paren)) {
1205 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1206 }
1207 SourceLocation LParenLoc = ConsumeParen();
1208
Douglas Gregora4745612008-12-01 18:00:20 +00001209 // Parse throw(...), a Microsoft extension that means "this function
1210 // can throw anything".
1211 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001212 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001213 SourceLocation EllipsisLoc = ConsumeToken();
1214 if (!getLang().Microsoft)
1215 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001216 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001217 return false;
1218 }
1219
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001220 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001221 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001222 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001223 TypeResult Res(ParseTypeName(&Range));
1224 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001225 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001226 Ranges.push_back(Range);
1227 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001228 if (Tok.is(tok::comma))
1229 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001230 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001231 break;
1232 }
1233
Sebastian Redlab197ba2009-02-09 18:23:29 +00001234 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001235 return false;
1236}
Douglas Gregor6569d682009-05-27 23:11:45 +00001237
1238/// \brief We have just started parsing the definition of a new class,
1239/// so push that class onto our stack of classes that is currently
1240/// being parsed.
1241void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1242 assert((TopLevelClass || !ClassStack.empty()) &&
1243 "Nested class without outer class");
1244 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1245}
1246
1247/// \brief Deallocate the given parsed class and all of its nested
1248/// classes.
1249void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1250 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1251 DeallocateParsedClasses(Class->NestedClasses[I]);
1252 delete Class;
1253}
1254
1255/// \brief Pop the top class of the stack of classes that are
1256/// currently being parsed.
1257///
1258/// This routine should be called when we have finished parsing the
1259/// definition of a class, but have not yet popped the Scope
1260/// associated with the class's definition.
1261///
1262/// \returns true if the class we've popped is a top-level class,
1263/// false otherwise.
1264void Parser::PopParsingClass() {
1265 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1266
1267 ParsingClass *Victim = ClassStack.top();
1268 ClassStack.pop();
1269 if (Victim->TopLevelClass) {
1270 // Deallocate all of the nested classes of this class,
1271 // recursively: we don't need to keep any of this information.
1272 DeallocateParsedClasses(Victim);
1273 return;
1274 }
1275 assert(!ClassStack.empty() && "Missing top-level class?");
1276
1277 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1278 Victim->NestedClasses.empty()) {
1279 // The victim is a nested class, but we will not need to perform
1280 // any processing after the definition of this class since it has
1281 // no members whose handling was delayed. Therefore, we can just
1282 // remove this nested class.
1283 delete Victim;
1284 return;
1285 }
1286
1287 // This nested class has some members that will need to be processed
1288 // after the top-level class is completely defined. Therefore, add
1289 // it to the list of nested classes within its parent.
1290 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1291 ClassStack.top()->NestedClasses.push_back(Victim);
1292 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1293}