blob: 1427814c9342561f12c9344427dcc83524c62272 [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;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000051
52 Token attrTok;
Chris Lattner8f08cb72007-08-25 06:57:03 +000053
Chris Lattner04d66662007-10-09 17:33:22 +000054 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000055 Ident = Tok.getIdentifierInfo();
56 IdentLoc = ConsumeToken(); // eat the identifier.
57 }
58
59 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +000060 Action::AttrTy *AttrList = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000061 if (Tok.is(tok::kw___attribute)) {
62 attrTok = Tok;
63
Chris Lattner8f08cb72007-08-25 06:57:03 +000064 // FIXME: save these somewhere.
65 AttrList = ParseAttributes();
Douglas Gregor6a588dd2009-06-17 19:49:00 +000066 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000067
Douglas Gregor6a588dd2009-06-17 19:49:00 +000068 if (Tok.is(tok::equal)) {
69 if (AttrList)
70 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
71
Chris Lattner97144fc2009-04-02 04:16:50 +000072 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 }
Anders Carlssonf67606a2009-03-28 04:07:16 +000074
Chris Lattner51448322009-03-29 14:02:43 +000075 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +000076 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000077 diag::err_expected_ident_lbrace);
78 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000079 }
80
Chris Lattner51448322009-03-29 14:02:43 +000081 SourceLocation LBrace = ConsumeBrace();
82
83 // Enter a scope for the namespace.
84 ParseScope NamespaceScope(this, Scope::DeclScope);
85
86 DeclPtrTy NamespcDecl =
87 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
88
89 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
90 PP.getSourceManager(),
91 "parsing namespace");
92
93 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
94 ParseExternalDeclaration();
95
96 // Leave the namespace scope.
97 NamespaceScope.Exit();
98
Chris Lattner97144fc2009-04-02 04:16:50 +000099 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
100 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000101
Chris Lattner97144fc2009-04-02 04:16:50 +0000102 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000103 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000104}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000105
Anders Carlssonf67606a2009-03-28 04:07:16 +0000106/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
107/// alias definition.
108///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000109Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
110 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000111 IdentifierInfo *Alias,
112 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000113 assert(Tok.is(tok::equal) && "Not equal token");
114
115 ConsumeToken(); // eat the '='.
116
117 CXXScopeSpec SS;
118 // Parse (optional) nested-name-specifier.
119 ParseOptionalCXXScopeSpecifier(SS);
120
121 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
122 Diag(Tok, diag::err_expected_namespace_name);
123 // Skip to end of the definition and eat the ';'.
124 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000125 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000126 }
127
128 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000129 IdentifierInfo *Ident = Tok.getIdentifierInfo();
130 SourceLocation IdentLoc = ConsumeToken();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000131
132 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000133 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000134 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
135 "", tok::semi);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000136
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000137 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
138 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000139}
140
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000141/// ParseLinkage - We know that the current token is a string_literal
142/// and just before that, that extern was seen.
143///
144/// linkage-specification: [C++ 7.5p2: dcl.link]
145/// 'extern' string-literal '{' declaration-seq[opt] '}'
146/// 'extern' string-literal declaration
147///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000148Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000149 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000150 llvm::SmallVector<char, 8> LangBuffer;
151 // LangBuffer is guaranteed to be big enough.
152 LangBuffer.resize(Tok.getLength());
153 const char *LangBufPtr = &LangBuffer[0];
154 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
155
156 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000157
Douglas Gregor074149e2009-01-05 19:45:36 +0000158 ParseScope LinkageScope(this, Scope::DeclScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000159 DeclPtrTy LinkageSpec
Douglas Gregor074149e2009-01-05 19:45:36 +0000160 = Actions.ActOnStartLinkageSpecification(CurScope,
161 /*FIXME: */SourceLocation(),
162 Loc, LangBufPtr, StrSize,
163 Tok.is(tok::l_brace)? Tok.getLocation()
164 : SourceLocation());
165
166 if (Tok.isNot(tok::l_brace)) {
167 ParseDeclarationOrFunctionDefinition();
168 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
169 SourceLocation());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000170 }
171
172 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000173 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000174 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000175 }
176
Douglas Gregorf44515a2008-12-16 22:23:02 +0000177 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000178 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000179}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000180
Douglas Gregorf780abc2008-12-30 03:27:21 +0000181/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
182/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000183Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
184 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000185 assert(Tok.is(tok::kw_using) && "Not using token");
186
187 // Eat 'using'.
188 SourceLocation UsingLoc = ConsumeToken();
189
Chris Lattner2f274772009-01-06 06:55:51 +0000190 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000191 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner97144fc2009-04-02 04:16:50 +0000192 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner2f274772009-01-06 06:55:51 +0000193
194 // Otherwise, it must be using-declaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000195 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000196}
197
198/// ParseUsingDirective - Parse C++ using-directive, assumes
199/// that current token is 'namespace' and 'using' was already parsed.
200///
201/// using-directive: [C++ 7.3.p4: namespace.udir]
202/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
203/// namespace-name ;
204/// [GNU] using-directive:
205/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
206/// namespace-name attributes[opt] ;
207///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000208Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000209 SourceLocation UsingLoc,
210 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000211 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
212
213 // Eat 'namespace'.
214 SourceLocation NamespcLoc = ConsumeToken();
215
216 CXXScopeSpec SS;
217 // Parse (optional) nested-name-specifier.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000218 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000219
220 AttributeList *AttrList = 0;
221 IdentifierInfo *NamespcName = 0;
222 SourceLocation IdentLoc = SourceLocation();
223
224 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000225 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000226 Diag(Tok, diag::err_expected_namespace_name);
227 // If there was invalid namespace name, skip to end of decl, and eat ';'.
228 SkipUntil(tok::semi);
229 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000230 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000231 }
Chris Lattner823c44e2009-01-06 07:27:21 +0000232
233 // Parse identifier.
234 NamespcName = Tok.getIdentifierInfo();
235 IdentLoc = ConsumeToken();
236
237 // Parse (optional) attributes (most likely GNU strong-using extension).
238 if (Tok.is(tok::kw___attribute))
239 AttrList = ParseAttributes();
240
241 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000242 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000243 ExpectAndConsume(tok::semi,
244 AttrList ? diag::err_expected_semi_after_attribute_list :
245 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000246
247 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000248 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000249}
250
251/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
252/// 'using' was already seen.
253///
254/// using-declaration: [C++ 7.3.p3: namespace.udecl]
255/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
256/// unqualified-id [TODO]
257/// 'using' :: unqualified-id [TODO]
258///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000259Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000260 SourceLocation UsingLoc,
261 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000262 assert(false && "Not implemented");
263 // FIXME: Implement parsing.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000264 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000265}
266
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000267/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
268///
269/// static_assert-declaration:
270/// static_assert ( constant-expression , string-literal ) ;
271///
Chris Lattner97144fc2009-04-02 04:16:50 +0000272Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000273 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
274 SourceLocation StaticAssertLoc = ConsumeToken();
275
276 if (Tok.isNot(tok::l_paren)) {
277 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000278 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000279 }
280
281 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000282
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000283 OwningExprResult AssertExpr(ParseConstantExpression());
284 if (AssertExpr.isInvalid()) {
285 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000286 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000287 }
288
Anders Carlssonad5f9602009-03-13 23:29:20 +0000289 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000290 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000291
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000292 if (Tok.isNot(tok::string_literal)) {
293 Diag(Tok, diag::err_expected_string_literal);
294 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000295 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000296 }
297
298 OwningExprResult AssertMessage(ParseStringLiteralExpression());
299 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000300 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000301
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000302 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000303
Chris Lattner97144fc2009-04-02 04:16:50 +0000304 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000305 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
306
Anders Carlssonad5f9602009-03-13 23:29:20 +0000307 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000308 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000309}
310
Douglas Gregor42a552f2008-11-05 20:51:48 +0000311/// ParseClassName - Parse a C++ class-name, which names a class. Note
312/// that we only check that the result names a type; semantic analysis
313/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000314/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000315/// found.
316///
317/// class-name: [C++ 9.1]
318/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000319/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000320///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000321Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
322 const CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000323 // Check whether we have a template-id that names a type.
324 if (Tok.is(tok::annot_template_id)) {
325 TemplateIdAnnotation *TemplateId
326 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000327 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000328 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000329
330 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
331 TypeTy *Type = Tok.getAnnotationValue();
332 EndLocation = Tok.getAnnotationEndLoc();
333 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000334
335 if (Type)
336 return Type;
337 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000338 }
339
340 // Fall through to produce an error below.
341 }
342
Douglas Gregor42a552f2008-11-05 20:51:48 +0000343 if (Tok.isNot(tok::identifier)) {
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 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000349 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
350 Tok.getLocation(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000351 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000352 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000353 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000354 }
355
356 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000357 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000358 return Type;
359}
360
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000361/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
362/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
363/// until we reach the start of a definition or see a token that
364/// cannot start a definition.
365///
366/// class-specifier: [C++ class]
367/// class-head '{' member-specification[opt] '}'
368/// class-head '{' member-specification[opt] '}' attributes[opt]
369/// class-head:
370/// class-key identifier[opt] base-clause[opt]
371/// class-key nested-name-specifier identifier base-clause[opt]
372/// class-key nested-name-specifier[opt] simple-template-id
373/// base-clause[opt]
374/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
375/// [GNU] class-key attributes[opt] nested-name-specifier
376/// identifier base-clause[opt]
377/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
378/// simple-template-id base-clause[opt]
379/// class-key:
380/// 'class'
381/// 'struct'
382/// 'union'
383///
384/// elaborated-type-specifier: [C++ dcl.type.elab]
385/// class-key ::[opt] nested-name-specifier[opt] identifier
386/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
387/// simple-template-id
388///
389/// Note that the C++ class-specifier and elaborated-type-specifier,
390/// together, subsume the C99 struct-or-union-specifier:
391///
392/// struct-or-union-specifier: [C99 6.7.2.1]
393/// struct-or-union identifier[opt] '{' struct-contents '}'
394/// struct-or-union identifier
395/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
396/// '}' attributes[opt]
397/// [GNU] struct-or-union attributes[opt] identifier
398/// struct-or-union:
399/// 'struct'
400/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000401void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
402 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000403 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000404 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000405 DeclSpec::TST TagType;
406 if (TagTokKind == tok::kw_struct)
407 TagType = DeclSpec::TST_struct;
408 else if (TagTokKind == tok::kw_class)
409 TagType = DeclSpec::TST_class;
410 else {
411 assert(TagTokKind == tok::kw_union && "Not a class specifier");
412 TagType = DeclSpec::TST_union;
413 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000414
415 AttributeList *Attr = 0;
416 // If attributes exist after tag, parse them.
417 if (Tok.is(tok::kw___attribute))
418 Attr = ParseAttributes();
419
Steve Narofff59e17e2008-12-24 20:59:21 +0000420 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000421 if (Tok.is(tok::kw___declspec))
422 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Narofff59e17e2008-12-24 20:59:21 +0000423
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000424 // Parse the (optional) nested-name-specifier.
425 CXXScopeSpec SS;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000426 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
427 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000428 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000429
430 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000431 IdentifierInfo *Name = 0;
432 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000433 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000434 if (Tok.is(tok::identifier)) {
435 Name = Tok.getIdentifierInfo();
436 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000437 } else if (Tok.is(tok::annot_template_id)) {
438 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
439 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000440
Douglas Gregorc45c2322009-03-31 00:43:58 +0000441 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000442 // The template-name in the simple-template-id refers to
443 // something other than a class template. Give an appropriate
444 // error message and skip to the ';'.
445 SourceRange Range(NameLoc);
446 if (SS.isNotEmpty())
447 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000448
Douglas Gregor39a8de12009-02-25 19:37:18 +0000449 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
450 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000451
Douglas Gregor39a8de12009-02-25 19:37:18 +0000452 DS.SetTypeSpecError();
453 SkipUntil(tok::semi, false, true);
454 TemplateId->Destroy();
455 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000456 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000457 }
458
459 // There are three options here. If we have 'struct foo;', then
460 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000461 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000462 // something like 'struct foo xyz', a reference.
463 Action::TagKind TK;
464 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
465 TK = Action::TK_Definition;
Anders Carlsson5dc2af12009-05-11 22:25:03 +0000466 else if (Tok.is(tok::semi) && !DS.isFriendSpecified())
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000467 TK = Action::TK_Declaration;
468 else
469 TK = Action::TK_Reference;
470
Douglas Gregor39a8de12009-02-25 19:37:18 +0000471 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000472 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000473 Diag(StartLoc, diag::err_anon_type_definition)
474 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000475
476 // Skip the rest of this declarator, up until the comma or semicolon.
477 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000478
479 if (TemplateId)
480 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000481 return;
482 }
483
Douglas Gregorddc29e12009-02-06 22:42:48 +0000484 // Create the tag portion of the class or class template.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000485 Action::DeclResult TagOrTempResult;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000486 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
487
488 // FIXME: When TK == TK_Reference and we have a template-id, we need
489 // to turn that template-id into a type.
490
Douglas Gregor402abb52009-05-28 23:31:59 +0000491 bool Owned = false;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000492 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000493 // Explicit specialization, class template partial specialization,
494 // or explicit instantiation.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000495 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
496 TemplateId->getTemplateArgs(),
497 TemplateId->getTemplateArgIsType(),
498 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000499 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
500 TK == Action::TK_Declaration) {
501 // This is an explicit instantiation of a class template.
502 TagOrTempResult
503 = Actions.ActOnExplicitInstantiation(CurScope,
504 TemplateInfo.TemplateLoc,
505 TagType,
506 StartLoc,
507 SS,
508 TemplateTy::make(TemplateId->Template),
509 TemplateId->TemplateNameLoc,
510 TemplateId->LAngleLoc,
511 TemplateArgsPtr,
512 TemplateId->getTemplateArgLocations(),
513 TemplateId->RAngleLoc,
514 Attr);
515 } else {
516 // This is an explicit specialization or a class template
517 // partial specialization.
518 TemplateParameterLists FakedParamLists;
519
520 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
521 // This looks like an explicit instantiation, because we have
522 // something like
523 //
524 // template class Foo<X>
525 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000526 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000527 // meant to be an explicit specialization, but the user forgot
528 // the '<>' after 'template'.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000529 assert(TK == Action::TK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000530
531 SourceLocation LAngleLoc
532 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
533 Diag(TemplateId->TemplateNameLoc,
534 diag::err_explicit_instantiation_with_definition)
535 << SourceRange(TemplateInfo.TemplateLoc)
536 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
537
538 // Create a fake template parameter list that contains only
539 // "template<>", so that we treat this construct as a class
540 // template specialization.
541 FakedParamLists.push_back(
542 Actions.ActOnTemplateParameterList(0, SourceLocation(),
543 TemplateInfo.TemplateLoc,
544 LAngleLoc,
545 0, 0,
546 LAngleLoc));
547 TemplateParams = &FakedParamLists;
548 }
549
550 // Build the class template specialization.
551 TagOrTempResult
552 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000553 StartLoc, SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000554 TemplateTy::make(TemplateId->Template),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000555 TemplateId->TemplateNameLoc,
556 TemplateId->LAngleLoc,
557 TemplateArgsPtr,
558 TemplateId->getTemplateArgLocations(),
559 TemplateId->RAngleLoc,
560 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000561 Action::MultiTemplateParamsArg(Actions,
562 TemplateParams? &(*TemplateParams)[0] : 0,
563 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000564 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000565 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000566 } else if (TemplateParams && TK != Action::TK_Reference) {
567 // Class template declaration or definition.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000568 TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
569 StartLoc, SS, Name, NameLoc,
570 Attr,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000571 Action::MultiTemplateParamsArg(Actions,
572 &(*TemplateParams)[0],
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000573 TemplateParams->size()),
574 AS);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000575 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
576 TK == Action::TK_Declaration) {
577 // Explicit instantiation of a member of a class template
578 // specialization, e.g.,
579 //
580 // template struct Outer<int>::Inner;
581 //
582 TagOrTempResult
583 = Actions.ActOnExplicitInstantiation(CurScope,
584 TemplateInfo.TemplateLoc,
585 TagType, StartLoc, SS, Name,
586 NameLoc, Attr);
587 } else {
588 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
589 TK == Action::TK_Definition) {
590 // FIXME: Diagnose this particular error.
591 }
592
593 // Declaration or definition of a class type
594 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS,
Douglas Gregor402abb52009-05-28 23:31:59 +0000595 Name, NameLoc, Attr, AS, Owned);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000596 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000597
598 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000599 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000600 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000601
602 // If there is a body, parse it and inform the actions module.
603 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000604 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000605 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000606 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000607 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000608 else if (TK == Action::TK_Definition) {
609 // FIXME: Complain that we have a base-specifier list but no
610 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000611 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000612 }
613
614 const char *PrevSpec = 0;
Anders Carlsson66e99772009-05-11 22:27:47 +0000615 if (TagOrTempResult.isInvalid()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000616 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000617 return;
618 }
619
Anders Carlsson66e99772009-05-11 22:27:47 +0000620 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
Douglas Gregor402abb52009-05-28 23:31:59 +0000621 TagOrTempResult.get().getAs<void>(), Owned))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000622 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Anders Carlssond4f551b2009-05-11 22:42:30 +0000623
624 if (DS.isFriendSpecified())
625 Actions.ActOnFriendDecl(CurScope, DS.getFriendSpecLoc(),
626 TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000627}
628
629/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
630///
631/// base-clause : [C++ class.derived]
632/// ':' base-specifier-list
633/// base-specifier-list:
634/// base-specifier '...'[opt]
635/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000636void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000637 assert(Tok.is(tok::colon) && "Not a base clause");
638 ConsumeToken();
639
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000640 // Build up an array of parsed base specifiers.
641 llvm::SmallVector<BaseTy *, 8> BaseInfo;
642
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000643 while (true) {
644 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000645 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000646 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000647 // Skip the rest of this base specifier, up until the comma or
648 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000649 SkipUntil(tok::comma, tok::l_brace, true, true);
650 } else {
651 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000652 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000653 }
654
655 // If the next token is a comma, consume it and keep reading
656 // base-specifiers.
657 if (Tok.isNot(tok::comma)) break;
658
659 // Consume the comma.
660 ConsumeToken();
661 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000662
663 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000664 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000665}
666
667/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
668/// one entry in the base class list of a class specifier, for example:
669/// class foo : public bar, virtual private baz {
670/// 'public bar' and 'virtual private baz' are each base-specifiers.
671///
672/// base-specifier: [C++ class.derived]
673/// ::[opt] nested-name-specifier[opt] class-name
674/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
675/// class-name
676/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
677/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000678Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000679 bool IsVirtual = false;
680 SourceLocation StartLoc = Tok.getLocation();
681
682 // Parse the 'virtual' keyword.
683 if (Tok.is(tok::kw_virtual)) {
684 ConsumeToken();
685 IsVirtual = true;
686 }
687
688 // Parse an (optional) access specifier.
689 AccessSpecifier Access = getAccessSpecifierIfPresent();
690 if (Access)
691 ConsumeToken();
692
693 // Parse the 'virtual' keyword (again!), in case it came after the
694 // access specifier.
695 if (Tok.is(tok::kw_virtual)) {
696 SourceLocation VirtualLoc = ConsumeToken();
697 if (IsVirtual) {
698 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000699 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000700 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000701 }
702
703 IsVirtual = true;
704 }
705
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000706 // Parse optional '::' and optional nested-name-specifier.
707 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000708 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000709
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000710 // The location of the base class itself.
711 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000712
713 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000714 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000715 TypeResult BaseType = ParseClassName(EndLocation, &SS);
716 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000717 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000718
719 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000720 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000721
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000722 // Notify semantic analysis that we have parsed a complete
723 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000724 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000725 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000726}
727
728/// getAccessSpecifierIfPresent - Determine whether the next token is
729/// a C++ access-specifier.
730///
731/// access-specifier: [C++ class.derived]
732/// 'private'
733/// 'protected'
734/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000735AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000736{
737 switch (Tok.getKind()) {
738 default: return AS_none;
739 case tok::kw_private: return AS_private;
740 case tok::kw_protected: return AS_protected;
741 case tok::kw_public: return AS_public;
742 }
743}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000744
745/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
746///
747/// member-declaration:
748/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
749/// function-definition ';'[opt]
750/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
751/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000752/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000753/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000754/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000755///
756/// member-declarator-list:
757/// member-declarator
758/// member-declarator-list ',' member-declarator
759///
760/// member-declarator:
761/// declarator pure-specifier[opt]
762/// declarator constant-initializer[opt]
763/// identifier[opt] ':' constant-expression
764///
Sebastian Redle2b68332009-04-12 17:16:29 +0000765/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000766/// '= 0'
767///
768/// constant-initializer:
769/// '=' constant-expression
770///
Chris Lattner682bf922009-03-29 16:50:03 +0000771void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000772 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000773 if (Tok.is(tok::kw_static_assert)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000774 SourceLocation DeclEnd;
775 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000776 return;
777 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000778
Chris Lattner682bf922009-03-29 16:50:03 +0000779 if (Tok.is(tok::kw_template)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000780 SourceLocation DeclEnd;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000781 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
782 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000783 return;
784 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000785
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000786 // Handle: member-declaration ::= '__extension__' member-declaration
787 if (Tok.is(tok::kw___extension__)) {
788 // __extension__ silences extension warnings in the subexpression.
789 ExtensionRAIIObject O(Diags); // Use RAII to do this.
790 ConsumeToken();
791 return ParseCXXClassMemberDeclaration(AS);
792 }
793
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000794 SourceLocation DSStart = Tok.getLocation();
795 // decl-specifier-seq:
796 // Parse the common declaration-specifiers piece.
797 DeclSpec DS;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000798 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000799
800 if (Tok.is(tok::semi)) {
801 ConsumeToken();
802 // C++ 9.2p7: The member-declarator-list can be omitted only after a
803 // class-specifier or an enum-specifier or in a friend declaration.
804 // FIXME: Friend declarations.
805 switch (DS.getTypeSpecType()) {
Chris Lattner682bf922009-03-29 16:50:03 +0000806 case DeclSpec::TST_struct:
807 case DeclSpec::TST_union:
808 case DeclSpec::TST_class:
809 case DeclSpec::TST_enum:
810 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
811 return;
812 default:
813 Diag(DSStart, diag::err_no_declarators);
814 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000815 }
816 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000817
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000818 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000819
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000820 if (Tok.isNot(tok::colon)) {
821 // Parse the first declarator.
822 ParseDeclarator(DeclaratorInfo);
823 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000824 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000825 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000826 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000827 if (Tok.is(tok::semi))
828 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000829 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000830 }
831
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000832 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000833 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +0000834 || (DeclaratorInfo.isFunctionDeclarator() &&
835 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000836 if (!DeclaratorInfo.isFunctionDeclarator()) {
837 Diag(Tok, diag::err_func_def_no_params);
838 ConsumeBrace();
839 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000840 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000841 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000842
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000843 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
844 Diag(Tok, diag::err_function_declared_typedef);
845 // This recovery skips the entire function body. It would be nice
846 // to simply call ParseCXXInlineMethodDef() below, however Sema
847 // assumes the declarator represents a function, not a typedef.
848 ConsumeBrace();
849 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000850 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000851 }
852
Chris Lattner682bf922009-03-29 16:50:03 +0000853 ParseCXXInlineMethodDef(AS, DeclaratorInfo);
854 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000855 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000856 }
857
858 // member-declarator-list:
859 // member-declarator
860 // member-declarator-list ',' member-declarator
861
Chris Lattner682bf922009-03-29 16:50:03 +0000862 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000863 OwningExprResult BitfieldSize(Actions);
864 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +0000865 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000866
867 while (1) {
868
869 // member-declarator:
870 // declarator pure-specifier[opt]
871 // declarator constant-initializer[opt]
872 // identifier[opt] ':' constant-expression
873
874 if (Tok.is(tok::colon)) {
875 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000876 BitfieldSize = ParseConstantExpression();
877 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000878 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000879 }
880
881 // pure-specifier:
882 // '= 0'
883 //
884 // constant-initializer:
885 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +0000886 //
887 // defaulted/deleted function-definition:
888 // '=' 'default' [TODO]
889 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000890
891 if (Tok.is(tok::equal)) {
892 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +0000893 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
894 ConsumeToken();
895 Deleted = true;
896 } else {
897 Init = ParseInitializer();
898 if (Init.isInvalid())
899 SkipUntil(tok::comma, true, true);
900 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000901 }
902
903 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000904 if (Tok.is(tok::kw___attribute)) {
905 SourceLocation Loc;
906 AttributeList *AttrList = ParseAttributes(&Loc);
907 DeclaratorInfo.AddAttributes(AttrList, Loc);
908 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000909
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000910 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +0000911 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000912 // See Sema::ActOnCXXMemberDeclarator for details.
Chris Lattner682bf922009-03-29 16:50:03 +0000913 DeclPtrTy ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
914 DeclaratorInfo,
915 BitfieldSize.release(),
Sebastian Redle2b68332009-04-12 17:16:29 +0000916 Init.release(),
917 Deleted);
Chris Lattner682bf922009-03-29 16:50:03 +0000918 if (ThisDecl)
919 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000920
Douglas Gregor72b505b2008-12-16 21:30:33 +0000921 if (DeclaratorInfo.isFunctionDeclarator() &&
922 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
923 != DeclSpec::SCS_typedef) {
924 // We just declared a member function. If this member function
925 // has any default arguments, we'll need to parse them later.
926 LateParsedMethodDeclaration *LateMethod = 0;
927 DeclaratorChunk::FunctionTypeInfo &FTI
928 = DeclaratorInfo.getTypeObject(0).Fun;
929 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
930 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
931 if (!LateMethod) {
932 // Push this method onto the stack of late-parsed method
933 // declarations.
Douglas Gregor6569d682009-05-27 23:11:45 +0000934 getCurrentClass().MethodDecls.push_back(
Chris Lattner682bf922009-03-29 16:50:03 +0000935 LateParsedMethodDeclaration(ThisDecl));
Douglas Gregor6569d682009-05-27 23:11:45 +0000936 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000937
938 // Add all of the parameters prior to this one (they don't
939 // have default arguments).
940 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
941 for (unsigned I = 0; I < ParamIdx; ++I)
942 LateMethod->DefaultArgs.push_back(
943 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
944 }
945
946 // Add this parameter to the list of parameters (it or may
947 // not have a default argument).
948 LateMethod->DefaultArgs.push_back(
949 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
950 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
951 }
952 }
953 }
954
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000955 // If we don't have a comma, it is either the end of the list (a ';')
956 // or an error, bail out.
957 if (Tok.isNot(tok::comma))
958 break;
959
960 // Consume the comma.
961 ConsumeToken();
962
963 // Parse the next declarator.
964 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000965 BitfieldSize = 0;
966 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +0000967 Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000968
969 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000970 if (Tok.is(tok::kw___attribute)) {
971 SourceLocation Loc;
972 AttributeList *AttrList = ParseAttributes(&Loc);
973 DeclaratorInfo.AddAttributes(AttrList, Loc);
974 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000975
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000976 if (Tok.isNot(tok::colon))
977 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000978 }
979
980 if (Tok.is(tok::semi)) {
981 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +0000982 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +0000983 DeclsInGroup.size());
984 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000985 }
986
987 Diag(Tok, diag::err_expected_semi_decl_list);
988 // Skip to end of block or statement
989 SkipUntil(tok::r_brace, true, true);
990 if (Tok.is(tok::semi))
991 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000992 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000993}
994
995/// ParseCXXMemberSpecification - Parse the class definition.
996///
997/// member-specification:
998/// member-declaration member-specification[opt]
999/// access-specifier ':' member-specification[opt]
1000///
1001void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001002 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001003 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001004 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001005 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001006
Chris Lattner49f28ca2009-03-05 08:00:35 +00001007 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1008 PP.getSourceManager(),
1009 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001010
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001011 SourceLocation LBraceLoc = ConsumeBrace();
1012
Douglas Gregor6569d682009-05-27 23:11:45 +00001013 // Determine whether this is a top-level (non-nested) class.
1014 bool TopLevelClass = ClassStack.empty() ||
1015 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001016
1017 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001018 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001019
Douglas Gregor6569d682009-05-27 23:11:45 +00001020 // Note that we are parsing a new (potentially-nested) class definition.
1021 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1022
Douglas Gregorddc29e12009-02-06 22:42:48 +00001023 if (TagDecl)
1024 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1025 else {
1026 SkipUntil(tok::r_brace, false, false);
1027 return;
1028 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001029
1030 // C++ 11p3: Members of a class defined with the keyword class are private
1031 // by default. Members of a class defined with the keywords struct or union
1032 // are public by default.
1033 AccessSpecifier CurAS;
1034 if (TagType == DeclSpec::TST_class)
1035 CurAS = AS_private;
1036 else
1037 CurAS = AS_public;
1038
1039 // While we still have something to read, read the member-declarations.
1040 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1041 // Each iteration of this loop reads one member-declaration.
1042
1043 // Check for extraneous top-level semicolon.
1044 if (Tok.is(tok::semi)) {
1045 Diag(Tok, diag::ext_extra_struct_semi);
1046 ConsumeToken();
1047 continue;
1048 }
1049
1050 AccessSpecifier AS = getAccessSpecifierIfPresent();
1051 if (AS != AS_none) {
1052 // Current token is a C++ access specifier.
1053 CurAS = AS;
1054 ConsumeToken();
1055 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1056 continue;
1057 }
1058
1059 // Parse all the comma separated declarators.
1060 ParseCXXClassMemberDeclaration(CurAS);
1061 }
1062
1063 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1064
1065 AttributeList *AttrList = 0;
1066 // If attributes exist after class contents, parse them.
1067 if (Tok.is(tok::kw___attribute))
1068 AttrList = ParseAttributes(); // FIXME: where should I put them?
1069
1070 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1071 LBraceLoc, RBraceLoc);
1072
1073 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1074 // complete within function bodies, default arguments,
1075 // exception-specifications, and constructor ctor-initializers (including
1076 // such things in nested classes).
1077 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001078 // FIXME: Only function bodies and constructor ctor-initializers are
1079 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001080 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001081 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001082 // are complete and we can parse the delayed portions of method
1083 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001084 ParseLexedMethodDeclarations(getCurrentClass());
1085 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001086 }
1087
1088 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001089 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001090 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001091
Douglas Gregor72de6672009-01-08 20:45:30 +00001092 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001093}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001094
1095/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1096/// which explicitly initializes the members or base classes of a
1097/// class (C++ [class.base.init]). For example, the three initializers
1098/// after the ':' in the Derived constructor below:
1099///
1100/// @code
1101/// class Base { };
1102/// class Derived : Base {
1103/// int x;
1104/// float f;
1105/// public:
1106/// Derived(float f) : Base(), x(17), f(f) { }
1107/// };
1108/// @endcode
1109///
1110/// [C++] ctor-initializer:
1111/// ':' mem-initializer-list
1112///
1113/// [C++] mem-initializer-list:
1114/// mem-initializer
1115/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001116void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001117 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1118
1119 SourceLocation ColonLoc = ConsumeToken();
1120
1121 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1122
1123 do {
1124 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001125 if (!MemInit.isInvalid())
1126 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001127
1128 if (Tok.is(tok::comma))
1129 ConsumeToken();
1130 else if (Tok.is(tok::l_brace))
1131 break;
1132 else {
1133 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001134 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001135 SkipUntil(tok::l_brace, true, true);
1136 break;
1137 }
1138 } while (true);
1139
1140 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001141 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001142}
1143
1144/// ParseMemInitializer - Parse a C++ member initializer, which is
1145/// part of a constructor initializer that explicitly initializes one
1146/// member or base class (C++ [class.base.init]). See
1147/// ParseConstructorInitializer for an example.
1148///
1149/// [C++] mem-initializer:
1150/// mem-initializer-id '(' expression-list[opt] ')'
1151///
1152/// [C++] mem-initializer-id:
1153/// '::'[opt] nested-name-specifier[opt] class-name
1154/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001155Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001156 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1157
1158 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001159 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001160 return true;
1161 }
1162
1163 // Get the identifier. This may be a member name or a class name,
1164 // but we'll let the semantic analysis determine which it is.
1165 IdentifierInfo *II = Tok.getIdentifierInfo();
1166 SourceLocation IdLoc = ConsumeToken();
1167
1168 // Parse the '('.
1169 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001170 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001171 return true;
1172 }
1173 SourceLocation LParenLoc = ConsumeParen();
1174
1175 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001176 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001177 CommaLocsTy CommaLocs;
1178 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1179 SkipUntil(tok::r_paren);
1180 return true;
1181 }
1182
1183 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1184
Sebastian Redla55e52c2008-11-25 22:21:31 +00001185 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1186 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001187 ArgExprs.size(), CommaLocs.data(),
1188 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001189}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001190
1191/// ParseExceptionSpecification - Parse a C++ exception-specification
1192/// (C++ [except.spec]).
1193///
Douglas Gregora4745612008-12-01 18:00:20 +00001194/// exception-specification:
1195/// 'throw' '(' type-id-list [opt] ')'
1196/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001197///
Douglas Gregora4745612008-12-01 18:00:20 +00001198/// type-id-list:
1199/// type-id
1200/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001201///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001202bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001203 llvm::SmallVector<TypeTy*, 2>
1204 &Exceptions,
1205 llvm::SmallVector<SourceRange, 2>
1206 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001207 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001208 assert(Tok.is(tok::kw_throw) && "expected throw");
1209
1210 SourceLocation ThrowLoc = ConsumeToken();
1211
1212 if (!Tok.is(tok::l_paren)) {
1213 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1214 }
1215 SourceLocation LParenLoc = ConsumeParen();
1216
Douglas Gregora4745612008-12-01 18:00:20 +00001217 // Parse throw(...), a Microsoft extension that means "this function
1218 // can throw anything".
1219 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001220 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001221 SourceLocation EllipsisLoc = ConsumeToken();
1222 if (!getLang().Microsoft)
1223 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001224 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001225 return false;
1226 }
1227
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001228 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001229 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001230 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001231 TypeResult Res(ParseTypeName(&Range));
1232 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001233 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001234 Ranges.push_back(Range);
1235 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001236 if (Tok.is(tok::comma))
1237 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001238 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001239 break;
1240 }
1241
Sebastian Redlab197ba2009-02-09 18:23:29 +00001242 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001243 return false;
1244}
Douglas Gregor6569d682009-05-27 23:11:45 +00001245
1246/// \brief We have just started parsing the definition of a new class,
1247/// so push that class onto our stack of classes that is currently
1248/// being parsed.
1249void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1250 assert((TopLevelClass || !ClassStack.empty()) &&
1251 "Nested class without outer class");
1252 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1253}
1254
1255/// \brief Deallocate the given parsed class and all of its nested
1256/// classes.
1257void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1258 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1259 DeallocateParsedClasses(Class->NestedClasses[I]);
1260 delete Class;
1261}
1262
1263/// \brief Pop the top class of the stack of classes that are
1264/// currently being parsed.
1265///
1266/// This routine should be called when we have finished parsing the
1267/// definition of a class, but have not yet popped the Scope
1268/// associated with the class's definition.
1269///
1270/// \returns true if the class we've popped is a top-level class,
1271/// false otherwise.
1272void Parser::PopParsingClass() {
1273 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1274
1275 ParsingClass *Victim = ClassStack.top();
1276 ClassStack.pop();
1277 if (Victim->TopLevelClass) {
1278 // Deallocate all of the nested classes of this class,
1279 // recursively: we don't need to keep any of this information.
1280 DeallocateParsedClasses(Victim);
1281 return;
1282 }
1283 assert(!ClassStack.empty() && "Missing top-level class?");
1284
1285 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1286 Victim->NestedClasses.empty()) {
1287 // The victim is a nested class, but we will not need to perform
1288 // any processing after the definition of this class since it has
1289 // no members whose handling was delayed. Therefore, we can just
1290 // remove this nested class.
1291 delete Victim;
1292 return;
1293 }
1294
1295 // This nested class has some members that will need to be processed
1296 // after the top-level class is completely defined. Therefore, add
1297 // it to the list of nested classes within its parent.
1298 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1299 ClassStack.top()->NestedClasses.push_back(Victim);
1300 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1301}