blob: 225f9261ef8fb34e25964f98b067b8e3a31352fc [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Chris Lattnerbc8d5642008-12-18 01:12:00 +000019#include "ExtensionRAIIObject.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000020using namespace clang;
21
22/// ParseNamespace - We know that the current token is a namespace keyword. This
23/// may either be a top level namespace or a block-level namespace alias.
24///
25/// namespace-definition: [C++ 7.3: basic.namespace]
26/// named-namespace-definition
27/// unnamed-namespace-definition
28///
29/// unnamed-namespace-definition:
30/// 'namespace' attributes[opt] '{' namespace-body '}'
31///
32/// named-namespace-definition:
33/// original-namespace-definition
34/// extension-namespace-definition
35///
36/// original-namespace-definition:
37/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
38///
39/// extension-namespace-definition:
40/// 'namespace' original-namespace-name '{' namespace-body '}'
41///
42/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
43/// 'namespace' identifier '=' qualified-namespace-specifier ';'
44///
Chris Lattner97144fc2009-04-02 04:16:50 +000045Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
46 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000047 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000048 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
49
50 SourceLocation IdentLoc;
51 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000052
53 Token attrTok;
Chris Lattner8f08cb72007-08-25 06:57:03 +000054
Chris Lattner04d66662007-10-09 17:33:22 +000055 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000056 Ident = Tok.getIdentifierInfo();
57 IdentLoc = ConsumeToken(); // eat the identifier.
58 }
59
60 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +000061 Action::AttrTy *AttrList = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000062 if (Tok.is(tok::kw___attribute)) {
63 attrTok = Tok;
64
Chris Lattner8f08cb72007-08-25 06:57:03 +000065 // FIXME: save these somewhere.
66 AttrList = ParseAttributes();
Douglas Gregor6a588dd2009-06-17 19:49:00 +000067 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000068
Douglas Gregor6a588dd2009-06-17 19:49:00 +000069 if (Tok.is(tok::equal)) {
70 if (AttrList)
71 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
72
Chris Lattner97144fc2009-04-02 04:16:50 +000073 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000074 }
Anders Carlssonf67606a2009-03-28 04:07:16 +000075
Chris Lattner51448322009-03-29 14:02:43 +000076 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +000077 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000078 diag::err_expected_ident_lbrace);
79 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000080 }
81
Chris Lattner51448322009-03-29 14:02:43 +000082 SourceLocation LBrace = ConsumeBrace();
83
84 // Enter a scope for the namespace.
85 ParseScope NamespaceScope(this, Scope::DeclScope);
86
87 DeclPtrTy NamespcDecl =
88 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
89
90 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
91 PP.getSourceManager(),
92 "parsing namespace");
93
94 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
95 ParseExternalDeclaration();
96
97 // Leave the namespace scope.
98 NamespaceScope.Exit();
99
Chris Lattner97144fc2009-04-02 04:16:50 +0000100 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
101 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000102
Chris Lattner97144fc2009-04-02 04:16:50 +0000103 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000104 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000105}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000106
Anders Carlssonf67606a2009-03-28 04:07:16 +0000107/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
108/// alias definition.
109///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000110Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
111 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000112 IdentifierInfo *Alias,
113 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000114 assert(Tok.is(tok::equal) && "Not equal token");
115
116 ConsumeToken(); // eat the '='.
117
118 CXXScopeSpec SS;
119 // Parse (optional) nested-name-specifier.
120 ParseOptionalCXXScopeSpecifier(SS);
121
122 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
123 Diag(Tok, diag::err_expected_namespace_name);
124 // Skip to end of the definition and eat the ';'.
125 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000126 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000127 }
128
129 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000130 IdentifierInfo *Ident = Tok.getIdentifierInfo();
131 SourceLocation IdentLoc = ConsumeToken();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000132
133 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000134 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000135 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
136 "", tok::semi);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000137
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000138 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
139 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000140}
141
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000142/// ParseLinkage - We know that the current token is a string_literal
143/// and just before that, that extern was seen.
144///
145/// linkage-specification: [C++ 7.5p2: dcl.link]
146/// 'extern' string-literal '{' declaration-seq[opt] '}'
147/// 'extern' string-literal declaration
148///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000149Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000150 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000151 llvm::SmallVector<char, 8> LangBuffer;
152 // LangBuffer is guaranteed to be big enough.
153 LangBuffer.resize(Tok.getLength());
154 const char *LangBufPtr = &LangBuffer[0];
155 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
156
157 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000158
Douglas Gregor074149e2009-01-05 19:45:36 +0000159 ParseScope LinkageScope(this, Scope::DeclScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000160 DeclPtrTy LinkageSpec
Douglas Gregor074149e2009-01-05 19:45:36 +0000161 = Actions.ActOnStartLinkageSpecification(CurScope,
162 /*FIXME: */SourceLocation(),
163 Loc, LangBufPtr, StrSize,
164 Tok.is(tok::l_brace)? Tok.getLocation()
165 : SourceLocation());
166
167 if (Tok.isNot(tok::l_brace)) {
168 ParseDeclarationOrFunctionDefinition();
169 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
170 SourceLocation());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000171 }
172
173 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000174 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000175 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000176 }
177
Douglas Gregorf44515a2008-12-16 22:23:02 +0000178 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000179 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000180}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000181
Douglas Gregorf780abc2008-12-30 03:27:21 +0000182/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
183/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000184Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
185 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000186 assert(Tok.is(tok::kw_using) && "Not using token");
187
188 // Eat 'using'.
189 SourceLocation UsingLoc = ConsumeToken();
190
Chris Lattner2f274772009-01-06 06:55:51 +0000191 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000192 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner97144fc2009-04-02 04:16:50 +0000193 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner2f274772009-01-06 06:55:51 +0000194
195 // Otherwise, it must be using-declaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000196 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000197}
198
199/// ParseUsingDirective - Parse C++ using-directive, assumes
200/// that current token is 'namespace' and 'using' was already parsed.
201///
202/// using-directive: [C++ 7.3.p4: namespace.udir]
203/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
204/// namespace-name ;
205/// [GNU] using-directive:
206/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
207/// namespace-name attributes[opt] ;
208///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000209Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000210 SourceLocation UsingLoc,
211 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000212 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
213
214 // Eat 'namespace'.
215 SourceLocation NamespcLoc = ConsumeToken();
216
217 CXXScopeSpec SS;
218 // Parse (optional) nested-name-specifier.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000219 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000220
221 AttributeList *AttrList = 0;
222 IdentifierInfo *NamespcName = 0;
223 SourceLocation IdentLoc = SourceLocation();
224
225 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000226 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000227 Diag(Tok, diag::err_expected_namespace_name);
228 // If there was invalid namespace name, skip to end of decl, and eat ';'.
229 SkipUntil(tok::semi);
230 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000231 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000232 }
Chris Lattner823c44e2009-01-06 07:27:21 +0000233
234 // Parse identifier.
235 NamespcName = Tok.getIdentifierInfo();
236 IdentLoc = ConsumeToken();
237
238 // Parse (optional) attributes (most likely GNU strong-using extension).
239 if (Tok.is(tok::kw___attribute))
240 AttrList = ParseAttributes();
241
242 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000243 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000244 ExpectAndConsume(tok::semi,
245 AttrList ? diag::err_expected_semi_after_attribute_list :
246 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000247
248 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000249 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000250}
251
252/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
253/// 'using' was already seen.
254///
255/// using-declaration: [C++ 7.3.p3: namespace.udecl]
256/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000257/// unqualified-id
258/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000259///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000260Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000261 SourceLocation UsingLoc,
262 SourceLocation &DeclEnd) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000263 CXXScopeSpec SS;
264 bool IsTypeName;
265
266 // Ignore optional 'typename'.
267 if (Tok.is(tok::kw_typename)) {
268 ConsumeToken();
269 IsTypeName = true;
270 }
271 else
272 IsTypeName = false;
273
274 // Parse nested-name-specifier.
275 ParseOptionalCXXScopeSpecifier(SS);
276
277 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000278
279 // Check nested-name specifier.
280 if (SS.isInvalid()) {
281 SkipUntil(tok::semi);
282 return DeclPtrTy();
283 }
284 if (Tok.is(tok::annot_template_id)) {
285 Diag(Tok, diag::err_unexpected_template_spec_in_using);
286 SkipUntil(tok::semi);
287 return DeclPtrTy();
288 }
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000289
290 IdentifierInfo *TargetName = 0;
291 OverloadedOperatorKind Op = OO_None;
292 SourceLocation IdentLoc;
293
294 if (Tok.is(tok::kw_operator)) {
295 IdentLoc = Tok.getLocation();
296
297 Op = TryParseOperatorFunctionId();
298 if (!Op) {
299 // If there was an invalid operator, skip to end of decl, and eat ';'.
300 SkipUntil(tok::semi);
301 return DeclPtrTy();
302 }
303 } else if (Tok.is(tok::identifier)) {
304 // Parse identifier.
305 TargetName = Tok.getIdentifierInfo();
306 IdentLoc = ConsumeToken();
307 } else {
308 // FIXME: Use a better diagnostic here.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000309 Diag(Tok, diag::err_expected_ident_in_using);
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000310
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000311 // If there was invalid identifier, skip to end of decl, and eat ';'.
312 SkipUntil(tok::semi);
313 return DeclPtrTy();
314 }
315
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000316 // Parse (optional) attributes (most likely GNU strong-using extension).
317 if (Tok.is(tok::kw___attribute))
318 AttrList = ParseAttributes();
319
320 // Eat ';'.
321 DeclEnd = Tok.getLocation();
322 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
323 AttrList ? "attributes list" : "namespace name", tok::semi);
324
325 return Actions.ActOnUsingDeclaration(CurScope, UsingLoc, SS,
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000326 IdentLoc, TargetName, Op,
327 AttrList, IsTypeName);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000328}
329
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000330/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
331///
332/// static_assert-declaration:
333/// static_assert ( constant-expression , string-literal ) ;
334///
Chris Lattner97144fc2009-04-02 04:16:50 +0000335Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000336 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
337 SourceLocation StaticAssertLoc = ConsumeToken();
338
339 if (Tok.isNot(tok::l_paren)) {
340 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000341 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000342 }
343
344 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000345
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000346 OwningExprResult AssertExpr(ParseConstantExpression());
347 if (AssertExpr.isInvalid()) {
348 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000349 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000350 }
351
Anders Carlssonad5f9602009-03-13 23:29:20 +0000352 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000353 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000354
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000355 if (Tok.isNot(tok::string_literal)) {
356 Diag(Tok, diag::err_expected_string_literal);
357 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000358 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000359 }
360
361 OwningExprResult AssertMessage(ParseStringLiteralExpression());
362 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000363 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000364
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000365 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000366
Chris Lattner97144fc2009-04-02 04:16:50 +0000367 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000368 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
369
Anders Carlssonad5f9602009-03-13 23:29:20 +0000370 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000371 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000372}
373
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000374/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
375///
376/// 'decltype' ( expression )
377///
378void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
379 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
380
381 SourceLocation StartLoc = ConsumeToken();
382 SourceLocation LParenLoc = Tok.getLocation();
383
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000384 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
385 "decltype")) {
386 SkipUntil(tok::r_paren);
387 return;
388 }
389
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000390 // Parse the expression
391
392 // C++0x [dcl.type.simple]p4:
393 // The operand of the decltype specifier is an unevaluated operand.
394 EnterExpressionEvaluationContext Unevaluated(Actions,
395 Action::Unevaluated);
396 OwningExprResult Result = ParseExpression();
397 if (Result.isInvalid()) {
398 SkipUntil(tok::r_paren);
399 return;
400 }
401
402 // Match the ')'
403 SourceLocation RParenLoc;
404 if (Tok.is(tok::r_paren))
405 RParenLoc = ConsumeParen();
406 else
407 MatchRHSPunctuation(tok::r_paren, LParenLoc);
408
409 if (RParenLoc.isInvalid())
410 return;
411
412 const char *PrevSpec = 0;
413 // Check for duplicate type specifiers (e.g. "int decltype(a)").
414 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
415 Result.release()))
416 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
417}
418
Douglas Gregor42a552f2008-11-05 20:51:48 +0000419/// ParseClassName - Parse a C++ class-name, which names a class. Note
420/// that we only check that the result names a type; semantic analysis
421/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000422/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000423/// found.
424///
425/// class-name: [C++ 9.1]
426/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000427/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000428///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000429Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
430 const CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000431 // Check whether we have a template-id that names a type.
432 if (Tok.is(tok::annot_template_id)) {
433 TemplateIdAnnotation *TemplateId
434 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000435 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000436 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000437
438 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
439 TypeTy *Type = Tok.getAnnotationValue();
440 EndLocation = Tok.getAnnotationEndLoc();
441 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000442
443 if (Type)
444 return Type;
445 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000446 }
447
448 // Fall through to produce an error below.
449 }
450
Douglas Gregor42a552f2008-11-05 20:51:48 +0000451 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000452 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000453 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000454 }
455
456 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000457 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
458 Tok.getLocation(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000459 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000460 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000461 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000462 }
463
464 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000465 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000466 return Type;
467}
468
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000469/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
470/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
471/// until we reach the start of a definition or see a token that
472/// cannot start a definition.
473///
474/// class-specifier: [C++ class]
475/// class-head '{' member-specification[opt] '}'
476/// class-head '{' member-specification[opt] '}' attributes[opt]
477/// class-head:
478/// class-key identifier[opt] base-clause[opt]
479/// class-key nested-name-specifier identifier base-clause[opt]
480/// class-key nested-name-specifier[opt] simple-template-id
481/// base-clause[opt]
482/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
483/// [GNU] class-key attributes[opt] nested-name-specifier
484/// identifier base-clause[opt]
485/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
486/// simple-template-id base-clause[opt]
487/// class-key:
488/// 'class'
489/// 'struct'
490/// 'union'
491///
492/// elaborated-type-specifier: [C++ dcl.type.elab]
493/// class-key ::[opt] nested-name-specifier[opt] identifier
494/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
495/// simple-template-id
496///
497/// Note that the C++ class-specifier and elaborated-type-specifier,
498/// together, subsume the C99 struct-or-union-specifier:
499///
500/// struct-or-union-specifier: [C99 6.7.2.1]
501/// struct-or-union identifier[opt] '{' struct-contents '}'
502/// struct-or-union identifier
503/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
504/// '}' attributes[opt]
505/// [GNU] struct-or-union attributes[opt] identifier
506/// struct-or-union:
507/// 'struct'
508/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000509void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
510 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000511 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000512 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000513 DeclSpec::TST TagType;
514 if (TagTokKind == tok::kw_struct)
515 TagType = DeclSpec::TST_struct;
516 else if (TagTokKind == tok::kw_class)
517 TagType = DeclSpec::TST_class;
518 else {
519 assert(TagTokKind == tok::kw_union && "Not a class specifier");
520 TagType = DeclSpec::TST_union;
521 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000522
523 AttributeList *Attr = 0;
524 // If attributes exist after tag, parse them.
525 if (Tok.is(tok::kw___attribute))
526 Attr = ParseAttributes();
527
Steve Narofff59e17e2008-12-24 20:59:21 +0000528 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000529 if (Tok.is(tok::kw___declspec))
530 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Narofff59e17e2008-12-24 20:59:21 +0000531
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000532 // Parse the (optional) nested-name-specifier.
533 CXXScopeSpec SS;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000534 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
535 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000536 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000537
538 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000539 IdentifierInfo *Name = 0;
540 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000541 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000542 if (Tok.is(tok::identifier)) {
543 Name = Tok.getIdentifierInfo();
544 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000545 } else if (Tok.is(tok::annot_template_id)) {
546 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
547 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000548
Douglas Gregorc45c2322009-03-31 00:43:58 +0000549 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000550 // The template-name in the simple-template-id refers to
551 // something other than a class template. Give an appropriate
552 // error message and skip to the ';'.
553 SourceRange Range(NameLoc);
554 if (SS.isNotEmpty())
555 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000556
Douglas Gregor39a8de12009-02-25 19:37:18 +0000557 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
558 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000559
Douglas Gregor39a8de12009-02-25 19:37:18 +0000560 DS.SetTypeSpecError();
561 SkipUntil(tok::semi, false, true);
562 TemplateId->Destroy();
563 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000564 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000565 }
566
567 // There are three options here. If we have 'struct foo;', then
568 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000569 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000570 // something like 'struct foo xyz', a reference.
571 Action::TagKind TK;
572 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
573 TK = Action::TK_Definition;
Anders Carlsson5dc2af12009-05-11 22:25:03 +0000574 else if (Tok.is(tok::semi) && !DS.isFriendSpecified())
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000575 TK = Action::TK_Declaration;
576 else
577 TK = Action::TK_Reference;
578
Douglas Gregor39a8de12009-02-25 19:37:18 +0000579 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000580 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000581 Diag(StartLoc, diag::err_anon_type_definition)
582 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000583
584 // Skip the rest of this declarator, up until the comma or semicolon.
585 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000586
587 if (TemplateId)
588 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000589 return;
590 }
591
Douglas Gregorddc29e12009-02-06 22:42:48 +0000592 // Create the tag portion of the class or class template.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000593 Action::DeclResult TagOrTempResult;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000594 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
595
596 // FIXME: When TK == TK_Reference and we have a template-id, we need
597 // to turn that template-id into a type.
598
Douglas Gregor402abb52009-05-28 23:31:59 +0000599 bool Owned = false;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000600 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000601 // Explicit specialization, class template partial specialization,
602 // or explicit instantiation.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000603 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
604 TemplateId->getTemplateArgs(),
605 TemplateId->getTemplateArgIsType(),
606 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000607 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
608 TK == Action::TK_Declaration) {
609 // This is an explicit instantiation of a class template.
610 TagOrTempResult
611 = Actions.ActOnExplicitInstantiation(CurScope,
612 TemplateInfo.TemplateLoc,
613 TagType,
614 StartLoc,
615 SS,
616 TemplateTy::make(TemplateId->Template),
617 TemplateId->TemplateNameLoc,
618 TemplateId->LAngleLoc,
619 TemplateArgsPtr,
620 TemplateId->getTemplateArgLocations(),
621 TemplateId->RAngleLoc,
622 Attr);
623 } else {
624 // This is an explicit specialization or a class template
625 // partial specialization.
626 TemplateParameterLists FakedParamLists;
627
628 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
629 // This looks like an explicit instantiation, because we have
630 // something like
631 //
632 // template class Foo<X>
633 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000634 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000635 // meant to be an explicit specialization, but the user forgot
636 // the '<>' after 'template'.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000637 assert(TK == Action::TK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000638
639 SourceLocation LAngleLoc
640 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
641 Diag(TemplateId->TemplateNameLoc,
642 diag::err_explicit_instantiation_with_definition)
643 << SourceRange(TemplateInfo.TemplateLoc)
644 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
645
646 // Create a fake template parameter list that contains only
647 // "template<>", so that we treat this construct as a class
648 // template specialization.
649 FakedParamLists.push_back(
650 Actions.ActOnTemplateParameterList(0, SourceLocation(),
651 TemplateInfo.TemplateLoc,
652 LAngleLoc,
653 0, 0,
654 LAngleLoc));
655 TemplateParams = &FakedParamLists;
656 }
657
658 // Build the class template specialization.
659 TagOrTempResult
660 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000661 StartLoc, SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000662 TemplateTy::make(TemplateId->Template),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000663 TemplateId->TemplateNameLoc,
664 TemplateId->LAngleLoc,
665 TemplateArgsPtr,
666 TemplateId->getTemplateArgLocations(),
667 TemplateId->RAngleLoc,
668 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000669 Action::MultiTemplateParamsArg(Actions,
670 TemplateParams? &(*TemplateParams)[0] : 0,
671 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000672 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000673 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000674 } else if (TemplateParams && TK != Action::TK_Reference) {
675 // Class template declaration or definition.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000676 TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
677 StartLoc, SS, Name, NameLoc,
678 Attr,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000679 Action::MultiTemplateParamsArg(Actions,
680 &(*TemplateParams)[0],
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000681 TemplateParams->size()),
682 AS);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000683 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
684 TK == Action::TK_Declaration) {
685 // Explicit instantiation of a member of a class template
686 // specialization, e.g.,
687 //
688 // template struct Outer<int>::Inner;
689 //
690 TagOrTempResult
691 = Actions.ActOnExplicitInstantiation(CurScope,
692 TemplateInfo.TemplateLoc,
693 TagType, StartLoc, SS, Name,
694 NameLoc, Attr);
695 } else {
696 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
697 TK == Action::TK_Definition) {
698 // FIXME: Diagnose this particular error.
699 }
700
701 // Declaration or definition of a class type
702 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS,
Douglas Gregor402abb52009-05-28 23:31:59 +0000703 Name, NameLoc, Attr, AS, Owned);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000704 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000705
706 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000707 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000708 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000709
710 // If there is a body, parse it and inform the actions module.
711 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000712 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000713 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000714 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000715 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000716 else if (TK == Action::TK_Definition) {
717 // FIXME: Complain that we have a base-specifier list but no
718 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000719 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000720 }
721
722 const char *PrevSpec = 0;
Anders Carlsson66e99772009-05-11 22:27:47 +0000723 if (TagOrTempResult.isInvalid()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000724 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000725 return;
726 }
727
Anders Carlsson66e99772009-05-11 22:27:47 +0000728 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
Douglas Gregor402abb52009-05-28 23:31:59 +0000729 TagOrTempResult.get().getAs<void>(), Owned))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000730 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Anders Carlssond4f551b2009-05-11 22:42:30 +0000731
732 if (DS.isFriendSpecified())
733 Actions.ActOnFriendDecl(CurScope, DS.getFriendSpecLoc(),
734 TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000735}
736
737/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
738///
739/// base-clause : [C++ class.derived]
740/// ':' base-specifier-list
741/// base-specifier-list:
742/// base-specifier '...'[opt]
743/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000744void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000745 assert(Tok.is(tok::colon) && "Not a base clause");
746 ConsumeToken();
747
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000748 // Build up an array of parsed base specifiers.
749 llvm::SmallVector<BaseTy *, 8> BaseInfo;
750
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000751 while (true) {
752 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000753 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000754 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000755 // Skip the rest of this base specifier, up until the comma or
756 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000757 SkipUntil(tok::comma, tok::l_brace, true, true);
758 } else {
759 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000760 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000761 }
762
763 // If the next token is a comma, consume it and keep reading
764 // base-specifiers.
765 if (Tok.isNot(tok::comma)) break;
766
767 // Consume the comma.
768 ConsumeToken();
769 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000770
771 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000772 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000773}
774
775/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
776/// one entry in the base class list of a class specifier, for example:
777/// class foo : public bar, virtual private baz {
778/// 'public bar' and 'virtual private baz' are each base-specifiers.
779///
780/// base-specifier: [C++ class.derived]
781/// ::[opt] nested-name-specifier[opt] class-name
782/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
783/// class-name
784/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
785/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000786Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000787 bool IsVirtual = false;
788 SourceLocation StartLoc = Tok.getLocation();
789
790 // Parse the 'virtual' keyword.
791 if (Tok.is(tok::kw_virtual)) {
792 ConsumeToken();
793 IsVirtual = true;
794 }
795
796 // Parse an (optional) access specifier.
797 AccessSpecifier Access = getAccessSpecifierIfPresent();
798 if (Access)
799 ConsumeToken();
800
801 // Parse the 'virtual' keyword (again!), in case it came after the
802 // access specifier.
803 if (Tok.is(tok::kw_virtual)) {
804 SourceLocation VirtualLoc = ConsumeToken();
805 if (IsVirtual) {
806 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000807 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000808 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000809 }
810
811 IsVirtual = true;
812 }
813
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000814 // Parse optional '::' and optional nested-name-specifier.
815 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000816 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000817
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000818 // The location of the base class itself.
819 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000820
821 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000822 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000823 TypeResult BaseType = ParseClassName(EndLocation, &SS);
824 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000825 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000826
827 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000828 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000829
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000830 // Notify semantic analysis that we have parsed a complete
831 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000832 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000833 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000834}
835
836/// getAccessSpecifierIfPresent - Determine whether the next token is
837/// a C++ access-specifier.
838///
839/// access-specifier: [C++ class.derived]
840/// 'private'
841/// 'protected'
842/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000843AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000844{
845 switch (Tok.getKind()) {
846 default: return AS_none;
847 case tok::kw_private: return AS_private;
848 case tok::kw_protected: return AS_protected;
849 case tok::kw_public: return AS_public;
850 }
851}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000852
853/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
854///
855/// member-declaration:
856/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
857/// function-definition ';'[opt]
858/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
859/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000860/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000861/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000862/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000863///
864/// member-declarator-list:
865/// member-declarator
866/// member-declarator-list ',' member-declarator
867///
868/// member-declarator:
869/// declarator pure-specifier[opt]
870/// declarator constant-initializer[opt]
871/// identifier[opt] ':' constant-expression
872///
Sebastian Redle2b68332009-04-12 17:16:29 +0000873/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000874/// '= 0'
875///
876/// constant-initializer:
877/// '=' constant-expression
878///
Chris Lattner682bf922009-03-29 16:50:03 +0000879void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000880 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000881 if (Tok.is(tok::kw_static_assert)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000882 SourceLocation DeclEnd;
883 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000884 return;
885 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000886
Chris Lattner682bf922009-03-29 16:50:03 +0000887 if (Tok.is(tok::kw_template)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000888 SourceLocation DeclEnd;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000889 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
890 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000891 return;
892 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000893
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000894 // Handle: member-declaration ::= '__extension__' member-declaration
895 if (Tok.is(tok::kw___extension__)) {
896 // __extension__ silences extension warnings in the subexpression.
897 ExtensionRAIIObject O(Diags); // Use RAII to do this.
898 ConsumeToken();
899 return ParseCXXClassMemberDeclaration(AS);
900 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000901
902 if (Tok.is(tok::kw_using)) {
903 // Eat 'using'.
904 SourceLocation UsingLoc = ConsumeToken();
905
906 if (Tok.is(tok::kw_namespace)) {
907 Diag(UsingLoc, diag::err_using_namespace_in_class);
908 SkipUntil(tok::semi, true, true);
909 }
910 else {
911 SourceLocation DeclEnd;
912 // Otherwise, it must be using-declaration.
913 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd);
914 }
915 return;
916 }
917
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000918 SourceLocation DSStart = Tok.getLocation();
919 // decl-specifier-seq:
920 // Parse the common declaration-specifiers piece.
921 DeclSpec DS;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000922 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000923
924 if (Tok.is(tok::semi)) {
925 ConsumeToken();
926 // C++ 9.2p7: The member-declarator-list can be omitted only after a
927 // class-specifier or an enum-specifier or in a friend declaration.
928 // FIXME: Friend declarations.
929 switch (DS.getTypeSpecType()) {
Chris Lattner682bf922009-03-29 16:50:03 +0000930 case DeclSpec::TST_struct:
931 case DeclSpec::TST_union:
932 case DeclSpec::TST_class:
933 case DeclSpec::TST_enum:
934 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
935 return;
936 default:
937 Diag(DSStart, diag::err_no_declarators);
938 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000939 }
940 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000941
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000942 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000943
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000944 if (Tok.isNot(tok::colon)) {
945 // Parse the first declarator.
946 ParseDeclarator(DeclaratorInfo);
947 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000948 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000949 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000950 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000951 if (Tok.is(tok::semi))
952 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000953 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000954 }
955
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000956 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000957 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +0000958 || (DeclaratorInfo.isFunctionDeclarator() &&
959 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000960 if (!DeclaratorInfo.isFunctionDeclarator()) {
961 Diag(Tok, diag::err_func_def_no_params);
962 ConsumeBrace();
963 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000964 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000965 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000966
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000967 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
968 Diag(Tok, diag::err_function_declared_typedef);
969 // This recovery skips the entire function body. It would be nice
970 // to simply call ParseCXXInlineMethodDef() below, however Sema
971 // assumes the declarator represents a function, not a typedef.
972 ConsumeBrace();
973 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000974 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000975 }
976
Chris Lattner682bf922009-03-29 16:50:03 +0000977 ParseCXXInlineMethodDef(AS, DeclaratorInfo);
978 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000979 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000980 }
981
982 // member-declarator-list:
983 // member-declarator
984 // member-declarator-list ',' member-declarator
985
Chris Lattner682bf922009-03-29 16:50:03 +0000986 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000987 OwningExprResult BitfieldSize(Actions);
988 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +0000989 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000990
991 while (1) {
992
993 // member-declarator:
994 // declarator pure-specifier[opt]
995 // declarator constant-initializer[opt]
996 // identifier[opt] ':' constant-expression
997
998 if (Tok.is(tok::colon)) {
999 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001000 BitfieldSize = ParseConstantExpression();
1001 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001002 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001003 }
1004
1005 // pure-specifier:
1006 // '= 0'
1007 //
1008 // constant-initializer:
1009 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001010 //
1011 // defaulted/deleted function-definition:
1012 // '=' 'default' [TODO]
1013 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001014
1015 if (Tok.is(tok::equal)) {
1016 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001017 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1018 ConsumeToken();
1019 Deleted = true;
1020 } else {
1021 Init = ParseInitializer();
1022 if (Init.isInvalid())
1023 SkipUntil(tok::comma, true, true);
1024 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001025 }
1026
1027 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001028 if (Tok.is(tok::kw___attribute)) {
1029 SourceLocation Loc;
1030 AttributeList *AttrList = ParseAttributes(&Loc);
1031 DeclaratorInfo.AddAttributes(AttrList, Loc);
1032 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001033
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001034 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001035 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001036 // See Sema::ActOnCXXMemberDeclarator for details.
Chris Lattner682bf922009-03-29 16:50:03 +00001037 DeclPtrTy ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1038 DeclaratorInfo,
1039 BitfieldSize.release(),
Sebastian Redle2b68332009-04-12 17:16:29 +00001040 Init.release(),
1041 Deleted);
Chris Lattner682bf922009-03-29 16:50:03 +00001042 if (ThisDecl)
1043 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001044
Douglas Gregor72b505b2008-12-16 21:30:33 +00001045 if (DeclaratorInfo.isFunctionDeclarator() &&
1046 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1047 != DeclSpec::SCS_typedef) {
1048 // We just declared a member function. If this member function
1049 // has any default arguments, we'll need to parse them later.
1050 LateParsedMethodDeclaration *LateMethod = 0;
1051 DeclaratorChunk::FunctionTypeInfo &FTI
1052 = DeclaratorInfo.getTypeObject(0).Fun;
1053 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1054 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1055 if (!LateMethod) {
1056 // Push this method onto the stack of late-parsed method
1057 // declarations.
Douglas Gregor6569d682009-05-27 23:11:45 +00001058 getCurrentClass().MethodDecls.push_back(
Chris Lattner682bf922009-03-29 16:50:03 +00001059 LateParsedMethodDeclaration(ThisDecl));
Douglas Gregor6569d682009-05-27 23:11:45 +00001060 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor72b505b2008-12-16 21:30:33 +00001061
1062 // Add all of the parameters prior to this one (they don't
1063 // have default arguments).
1064 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1065 for (unsigned I = 0; I < ParamIdx; ++I)
1066 LateMethod->DefaultArgs.push_back(
1067 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1068 }
1069
1070 // Add this parameter to the list of parameters (it or may
1071 // not have a default argument).
1072 LateMethod->DefaultArgs.push_back(
1073 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1074 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1075 }
1076 }
1077 }
1078
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001079 // If we don't have a comma, it is either the end of the list (a ';')
1080 // or an error, bail out.
1081 if (Tok.isNot(tok::comma))
1082 break;
1083
1084 // Consume the comma.
1085 ConsumeToken();
1086
1087 // Parse the next declarator.
1088 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001089 BitfieldSize = 0;
1090 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001091 Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001092
1093 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001094 if (Tok.is(tok::kw___attribute)) {
1095 SourceLocation Loc;
1096 AttributeList *AttrList = ParseAttributes(&Loc);
1097 DeclaratorInfo.AddAttributes(AttrList, Loc);
1098 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001099
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001100 if (Tok.isNot(tok::colon))
1101 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001102 }
1103
1104 if (Tok.is(tok::semi)) {
1105 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001106 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001107 DeclsInGroup.size());
1108 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001109 }
1110
1111 Diag(Tok, diag::err_expected_semi_decl_list);
1112 // Skip to end of block or statement
1113 SkipUntil(tok::r_brace, true, true);
1114 if (Tok.is(tok::semi))
1115 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001116 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001117}
1118
1119/// ParseCXXMemberSpecification - Parse the class definition.
1120///
1121/// member-specification:
1122/// member-declaration member-specification[opt]
1123/// access-specifier ':' member-specification[opt]
1124///
1125void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001126 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001127 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001128 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001129 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001130
Chris Lattner49f28ca2009-03-05 08:00:35 +00001131 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1132 PP.getSourceManager(),
1133 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001134
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001135 SourceLocation LBraceLoc = ConsumeBrace();
1136
Douglas Gregor6569d682009-05-27 23:11:45 +00001137 // Determine whether this is a top-level (non-nested) class.
1138 bool TopLevelClass = ClassStack.empty() ||
1139 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001140
1141 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001142 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001143
Douglas Gregor6569d682009-05-27 23:11:45 +00001144 // Note that we are parsing a new (potentially-nested) class definition.
1145 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1146
Douglas Gregorddc29e12009-02-06 22:42:48 +00001147 if (TagDecl)
1148 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1149 else {
1150 SkipUntil(tok::r_brace, false, false);
1151 return;
1152 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001153
1154 // C++ 11p3: Members of a class defined with the keyword class are private
1155 // by default. Members of a class defined with the keywords struct or union
1156 // are public by default.
1157 AccessSpecifier CurAS;
1158 if (TagType == DeclSpec::TST_class)
1159 CurAS = AS_private;
1160 else
1161 CurAS = AS_public;
1162
1163 // While we still have something to read, read the member-declarations.
1164 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1165 // Each iteration of this loop reads one member-declaration.
1166
1167 // Check for extraneous top-level semicolon.
1168 if (Tok.is(tok::semi)) {
1169 Diag(Tok, diag::ext_extra_struct_semi);
1170 ConsumeToken();
1171 continue;
1172 }
1173
1174 AccessSpecifier AS = getAccessSpecifierIfPresent();
1175 if (AS != AS_none) {
1176 // Current token is a C++ access specifier.
1177 CurAS = AS;
1178 ConsumeToken();
1179 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1180 continue;
1181 }
1182
1183 // Parse all the comma separated declarators.
1184 ParseCXXClassMemberDeclaration(CurAS);
1185 }
1186
1187 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1188
1189 AttributeList *AttrList = 0;
1190 // If attributes exist after class contents, parse them.
1191 if (Tok.is(tok::kw___attribute))
1192 AttrList = ParseAttributes(); // FIXME: where should I put them?
1193
1194 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1195 LBraceLoc, RBraceLoc);
1196
1197 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1198 // complete within function bodies, default arguments,
1199 // exception-specifications, and constructor ctor-initializers (including
1200 // such things in nested classes).
1201 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001202 // FIXME: Only function bodies and constructor ctor-initializers are
1203 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001204 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001205 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001206 // are complete and we can parse the delayed portions of method
1207 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001208 ParseLexedMethodDeclarations(getCurrentClass());
1209 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001210 }
1211
1212 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001213 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001214 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001215
Douglas Gregor72de6672009-01-08 20:45:30 +00001216 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001217}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001218
1219/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1220/// which explicitly initializes the members or base classes of a
1221/// class (C++ [class.base.init]). For example, the three initializers
1222/// after the ':' in the Derived constructor below:
1223///
1224/// @code
1225/// class Base { };
1226/// class Derived : Base {
1227/// int x;
1228/// float f;
1229/// public:
1230/// Derived(float f) : Base(), x(17), f(f) { }
1231/// };
1232/// @endcode
1233///
1234/// [C++] ctor-initializer:
1235/// ':' mem-initializer-list
1236///
1237/// [C++] mem-initializer-list:
1238/// mem-initializer
1239/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001240void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001241 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1242
1243 SourceLocation ColonLoc = ConsumeToken();
1244
1245 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1246
1247 do {
1248 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001249 if (!MemInit.isInvalid())
1250 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001251
1252 if (Tok.is(tok::comma))
1253 ConsumeToken();
1254 else if (Tok.is(tok::l_brace))
1255 break;
1256 else {
1257 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001258 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001259 SkipUntil(tok::l_brace, true, true);
1260 break;
1261 }
1262 } while (true);
1263
1264 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001265 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001266}
1267
1268/// ParseMemInitializer - Parse a C++ member initializer, which is
1269/// part of a constructor initializer that explicitly initializes one
1270/// member or base class (C++ [class.base.init]). See
1271/// ParseConstructorInitializer for an example.
1272///
1273/// [C++] mem-initializer:
1274/// mem-initializer-id '(' expression-list[opt] ')'
1275///
1276/// [C++] mem-initializer-id:
1277/// '::'[opt] nested-name-specifier[opt] class-name
1278/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001279Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001280 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1281
1282 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001283 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001284 return true;
1285 }
1286
1287 // Get the identifier. This may be a member name or a class name,
1288 // but we'll let the semantic analysis determine which it is.
1289 IdentifierInfo *II = Tok.getIdentifierInfo();
1290 SourceLocation IdLoc = ConsumeToken();
1291
1292 // Parse the '('.
1293 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001294 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001295 return true;
1296 }
1297 SourceLocation LParenLoc = ConsumeParen();
1298
1299 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001300 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001301 CommaLocsTy CommaLocs;
1302 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1303 SkipUntil(tok::r_paren);
1304 return true;
1305 }
1306
1307 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1308
Sebastian Redla55e52c2008-11-25 22:21:31 +00001309 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1310 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001311 ArgExprs.size(), CommaLocs.data(),
1312 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001313}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001314
1315/// ParseExceptionSpecification - Parse a C++ exception-specification
1316/// (C++ [except.spec]).
1317///
Douglas Gregora4745612008-12-01 18:00:20 +00001318/// exception-specification:
1319/// 'throw' '(' type-id-list [opt] ')'
1320/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001321///
Douglas Gregora4745612008-12-01 18:00:20 +00001322/// type-id-list:
1323/// type-id
1324/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001325///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001326bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001327 llvm::SmallVector<TypeTy*, 2>
1328 &Exceptions,
1329 llvm::SmallVector<SourceRange, 2>
1330 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001331 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001332 assert(Tok.is(tok::kw_throw) && "expected throw");
1333
1334 SourceLocation ThrowLoc = ConsumeToken();
1335
1336 if (!Tok.is(tok::l_paren)) {
1337 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1338 }
1339 SourceLocation LParenLoc = ConsumeParen();
1340
Douglas Gregora4745612008-12-01 18:00:20 +00001341 // Parse throw(...), a Microsoft extension that means "this function
1342 // can throw anything".
1343 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001344 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001345 SourceLocation EllipsisLoc = ConsumeToken();
1346 if (!getLang().Microsoft)
1347 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001348 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001349 return false;
1350 }
1351
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001352 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001353 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001354 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001355 TypeResult Res(ParseTypeName(&Range));
1356 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001357 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001358 Ranges.push_back(Range);
1359 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001360 if (Tok.is(tok::comma))
1361 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001362 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001363 break;
1364 }
1365
Sebastian Redlab197ba2009-02-09 18:23:29 +00001366 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001367 return false;
1368}
Douglas Gregor6569d682009-05-27 23:11:45 +00001369
1370/// \brief We have just started parsing the definition of a new class,
1371/// so push that class onto our stack of classes that is currently
1372/// being parsed.
1373void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1374 assert((TopLevelClass || !ClassStack.empty()) &&
1375 "Nested class without outer class");
1376 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1377}
1378
1379/// \brief Deallocate the given parsed class and all of its nested
1380/// classes.
1381void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1382 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1383 DeallocateParsedClasses(Class->NestedClasses[I]);
1384 delete Class;
1385}
1386
1387/// \brief Pop the top class of the stack of classes that are
1388/// currently being parsed.
1389///
1390/// This routine should be called when we have finished parsing the
1391/// definition of a class, but have not yet popped the Scope
1392/// associated with the class's definition.
1393///
1394/// \returns true if the class we've popped is a top-level class,
1395/// false otherwise.
1396void Parser::PopParsingClass() {
1397 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1398
1399 ParsingClass *Victim = ClassStack.top();
1400 ClassStack.pop();
1401 if (Victim->TopLevelClass) {
1402 // Deallocate all of the nested classes of this class,
1403 // recursively: we don't need to keep any of this information.
1404 DeallocateParsedClasses(Victim);
1405 return;
1406 }
1407 assert(!ClassStack.empty() && "Missing top-level class?");
1408
1409 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1410 Victim->NestedClasses.empty()) {
1411 // The victim is a nested class, but we will not need to perform
1412 // any processing after the definition of this class since it has
1413 // no members whose handling was delayed. Therefore, we can just
1414 // remove this nested class.
1415 delete Victim;
1416 return;
1417 }
1418
1419 // This nested class has some members that will need to be processed
1420 // after the top-level class is completely defined. Therefore, add
1421 // it to the list of nested classes within its parent.
1422 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1423 ClassStack.top()->NestedClasses.push_back(Victim);
1424 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1425}