blob: 0a97825fd92b80076f7b4e8f76f6722c208e301b [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;
John McCallfec54012009-08-03 20:12:06 +0000413 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000414 // Check for duplicate type specifiers (e.g. "int decltype(a)").
415 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000416 DiagID, Result.release()))
417 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000418}
419
Douglas Gregor42a552f2008-11-05 20:51:48 +0000420/// ParseClassName - Parse a C++ class-name, which names a class. Note
421/// that we only check that the result names a type; semantic analysis
422/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000423/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000424/// found.
425///
426/// class-name: [C++ 9.1]
427/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000428/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000429///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000430Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000431 const CXXScopeSpec *SS,
432 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000433 // Check whether we have a template-id that names a type.
434 if (Tok.is(tok::annot_template_id)) {
435 TemplateIdAnnotation *TemplateId
436 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000437 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000438 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000439
440 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
441 TypeTy *Type = Tok.getAnnotationValue();
442 EndLocation = Tok.getAnnotationEndLoc();
443 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000444
445 if (Type)
446 return Type;
447 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000448 }
449
450 // Fall through to produce an error below.
451 }
452
Douglas Gregor42a552f2008-11-05 20:51:48 +0000453 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000454 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000455 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000456 }
457
458 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000459 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor42c39f32009-08-26 18:27:52 +0000460 Tok.getLocation(), CurScope, SS,
461 true);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000462 if (!Type) {
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000463 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
464 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000465 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000466 }
467
468 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000469 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000470 return Type;
471}
472
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000473/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
474/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
475/// until we reach the start of a definition or see a token that
476/// cannot start a definition.
477///
478/// class-specifier: [C++ class]
479/// class-head '{' member-specification[opt] '}'
480/// class-head '{' member-specification[opt] '}' attributes[opt]
481/// class-head:
482/// class-key identifier[opt] base-clause[opt]
483/// class-key nested-name-specifier identifier base-clause[opt]
484/// class-key nested-name-specifier[opt] simple-template-id
485/// base-clause[opt]
486/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
487/// [GNU] class-key attributes[opt] nested-name-specifier
488/// identifier base-clause[opt]
489/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
490/// simple-template-id base-clause[opt]
491/// class-key:
492/// 'class'
493/// 'struct'
494/// 'union'
495///
496/// elaborated-type-specifier: [C++ dcl.type.elab]
497/// class-key ::[opt] nested-name-specifier[opt] identifier
498/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
499/// simple-template-id
500///
501/// Note that the C++ class-specifier and elaborated-type-specifier,
502/// together, subsume the C99 struct-or-union-specifier:
503///
504/// struct-or-union-specifier: [C99 6.7.2.1]
505/// struct-or-union identifier[opt] '{' struct-contents '}'
506/// struct-or-union identifier
507/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
508/// '}' attributes[opt]
509/// [GNU] struct-or-union attributes[opt] identifier
510/// struct-or-union:
511/// 'struct'
512/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000513void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
514 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000515 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000516 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000517 DeclSpec::TST TagType;
518 if (TagTokKind == tok::kw_struct)
519 TagType = DeclSpec::TST_struct;
520 else if (TagTokKind == tok::kw_class)
521 TagType = DeclSpec::TST_class;
522 else {
523 assert(TagTokKind == tok::kw_union && "Not a class specifier");
524 TagType = DeclSpec::TST_union;
525 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000526
527 AttributeList *Attr = 0;
528 // If attributes exist after tag, parse them.
529 if (Tok.is(tok::kw___attribute))
530 Attr = ParseAttributes();
531
Steve Narofff59e17e2008-12-24 20:59:21 +0000532 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000533 if (Tok.is(tok::kw___declspec))
534 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Narofff59e17e2008-12-24 20:59:21 +0000535
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000536 // Parse the (optional) nested-name-specifier.
537 CXXScopeSpec SS;
Douglas Gregor495c35d2009-08-25 22:51:20 +0000538 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, true))
Douglas Gregor39a8de12009-02-25 19:37:18 +0000539 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000540 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000541
542 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000543 IdentifierInfo *Name = 0;
544 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000545 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000546 if (Tok.is(tok::identifier)) {
547 Name = Tok.getIdentifierInfo();
548 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000549 } else if (Tok.is(tok::annot_template_id)) {
550 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
551 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000552
Douglas Gregorc45c2322009-03-31 00:43:58 +0000553 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000554 // The template-name in the simple-template-id refers to
555 // something other than a class template. Give an appropriate
556 // error message and skip to the ';'.
557 SourceRange Range(NameLoc);
558 if (SS.isNotEmpty())
559 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000560
Douglas Gregor39a8de12009-02-25 19:37:18 +0000561 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
562 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000563
Douglas Gregor39a8de12009-02-25 19:37:18 +0000564 DS.SetTypeSpecError();
565 SkipUntil(tok::semi, false, true);
566 TemplateId->Destroy();
567 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000568 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000569 }
570
John McCall67d1a672009-08-06 02:15:43 +0000571 // There are four options here. If we have 'struct foo;', then this
572 // is either a forward declaration or a friend declaration, which
573 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000574 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000575 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000576 Action::TagUseKind TUK;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000577 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
John McCall0f434ec2009-07-31 02:45:11 +0000578 TUK = Action::TUK_Definition;
John McCall67d1a672009-08-06 02:15:43 +0000579 else if (Tok.is(tok::semi))
580 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000581 else
John McCall0f434ec2009-07-31 02:45:11 +0000582 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000583
John McCall0f434ec2009-07-31 02:45:11 +0000584 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000585 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000586 Diag(StartLoc, diag::err_anon_type_definition)
587 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000588
589 // Skip the rest of this declarator, up until the comma or semicolon.
590 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000591
592 if (TemplateId)
593 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000594 return;
595 }
596
Douglas Gregorddc29e12009-02-06 22:42:48 +0000597 // Create the tag portion of the class or class template.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000598 Action::DeclResult TagOrTempResult;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000599 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
600
John McCall0f434ec2009-07-31 02:45:11 +0000601 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000602 // to turn that template-id into a type.
603
Douglas Gregor402abb52009-05-28 23:31:59 +0000604 bool Owned = false;
John McCall67d1a672009-08-06 02:15:43 +0000605 if (TemplateId && TUK != Action::TUK_Reference && TUK != Action::TUK_Friend) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000606 // Explicit specialization, class template partial specialization,
607 // or explicit instantiation.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000608 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
609 TemplateId->getTemplateArgs(),
610 TemplateId->getTemplateArgIsType(),
611 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000612 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000613 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000614 // This is an explicit instantiation of a class template.
615 TagOrTempResult
616 = Actions.ActOnExplicitInstantiation(CurScope,
617 TemplateInfo.TemplateLoc,
618 TagType,
619 StartLoc,
620 SS,
621 TemplateTy::make(TemplateId->Template),
622 TemplateId->TemplateNameLoc,
623 TemplateId->LAngleLoc,
624 TemplateArgsPtr,
625 TemplateId->getTemplateArgLocations(),
626 TemplateId->RAngleLoc,
627 Attr);
628 } else {
629 // This is an explicit specialization or a class template
630 // partial specialization.
631 TemplateParameterLists FakedParamLists;
632
633 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
634 // This looks like an explicit instantiation, because we have
635 // something like
636 //
637 // template class Foo<X>
638 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000639 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000640 // meant to be an explicit specialization, but the user forgot
641 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000642 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000643
644 SourceLocation LAngleLoc
645 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
646 Diag(TemplateId->TemplateNameLoc,
647 diag::err_explicit_instantiation_with_definition)
648 << SourceRange(TemplateInfo.TemplateLoc)
649 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
650
651 // Create a fake template parameter list that contains only
652 // "template<>", so that we treat this construct as a class
653 // template specialization.
654 FakedParamLists.push_back(
655 Actions.ActOnTemplateParameterList(0, SourceLocation(),
656 TemplateInfo.TemplateLoc,
657 LAngleLoc,
658 0, 0,
659 LAngleLoc));
660 TemplateParams = &FakedParamLists;
661 }
662
663 // Build the class template specialization.
664 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000665 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000666 StartLoc, SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000667 TemplateTy::make(TemplateId->Template),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000668 TemplateId->TemplateNameLoc,
669 TemplateId->LAngleLoc,
670 TemplateArgsPtr,
671 TemplateId->getTemplateArgLocations(),
672 TemplateId->RAngleLoc,
673 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000674 Action::MultiTemplateParamsArg(Actions,
675 TemplateParams? &(*TemplateParams)[0] : 0,
676 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000677 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000678 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000679 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000680 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000681 // Explicit instantiation of a member of a class template
682 // specialization, e.g.,
683 //
684 // template struct Outer<int>::Inner;
685 //
686 TagOrTempResult
687 = Actions.ActOnExplicitInstantiation(CurScope,
688 TemplateInfo.TemplateLoc,
689 TagType, StartLoc, SS, Name,
690 NameLoc, Attr);
691 } else {
692 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000693 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000694 // FIXME: Diagnose this particular error.
695 }
696
697 // Declaration or definition of a class type
John McCall0f434ec2009-07-31 02:45:11 +0000698 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000699 Name, NameLoc, Attr, AS,
700 Action::MultiTemplateParamsArg(Actions,
701 TemplateParams? &(*TemplateParams)[0] : 0,
702 TemplateParams? TemplateParams->size() : 0),
703 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());
John McCall0f434ec2009-07-31 02:45:11 +0000716 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000717 // 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
Anders Carlsson66e99772009-05-11 22:27:47 +0000722 if (TagOrTempResult.isInvalid()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000723 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000724 return;
725 }
726
John McCallfec54012009-08-03 20:12:06 +0000727 const char *PrevSpec = 0;
728 unsigned DiagID;
729 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +0000730 TagOrTempResult.get().getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +0000731 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000732}
733
734/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
735///
736/// base-clause : [C++ class.derived]
737/// ':' base-specifier-list
738/// base-specifier-list:
739/// base-specifier '...'[opt]
740/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000741void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000742 assert(Tok.is(tok::colon) && "Not a base clause");
743 ConsumeToken();
744
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000745 // Build up an array of parsed base specifiers.
746 llvm::SmallVector<BaseTy *, 8> BaseInfo;
747
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000748 while (true) {
749 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000750 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000751 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000752 // Skip the rest of this base specifier, up until the comma or
753 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000754 SkipUntil(tok::comma, tok::l_brace, true, true);
755 } else {
756 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000757 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000758 }
759
760 // If the next token is a comma, consume it and keep reading
761 // base-specifiers.
762 if (Tok.isNot(tok::comma)) break;
763
764 // Consume the comma.
765 ConsumeToken();
766 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000767
768 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000769 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000770}
771
772/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
773/// one entry in the base class list of a class specifier, for example:
774/// class foo : public bar, virtual private baz {
775/// 'public bar' and 'virtual private baz' are each base-specifiers.
776///
777/// base-specifier: [C++ class.derived]
778/// ::[opt] nested-name-specifier[opt] class-name
779/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
780/// class-name
781/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
782/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000783Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000784 bool IsVirtual = false;
785 SourceLocation StartLoc = Tok.getLocation();
786
787 // Parse the 'virtual' keyword.
788 if (Tok.is(tok::kw_virtual)) {
789 ConsumeToken();
790 IsVirtual = true;
791 }
792
793 // Parse an (optional) access specifier.
794 AccessSpecifier Access = getAccessSpecifierIfPresent();
795 if (Access)
796 ConsumeToken();
797
798 // Parse the 'virtual' keyword (again!), in case it came after the
799 // access specifier.
800 if (Tok.is(tok::kw_virtual)) {
801 SourceLocation VirtualLoc = ConsumeToken();
802 if (IsVirtual) {
803 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000804 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000805 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000806 }
807
808 IsVirtual = true;
809 }
810
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000811 // Parse optional '::' and optional nested-name-specifier.
812 CXXScopeSpec SS;
Douglas Gregor495c35d2009-08-25 22:51:20 +0000813 ParseOptionalCXXScopeSpecifier(SS, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000814
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000815 // The location of the base class itself.
816 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000817
818 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000819 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000820 TypeResult BaseType = ParseClassName(EndLocation, &SS);
821 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000822 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000823
824 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000825 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000826
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000827 // Notify semantic analysis that we have parsed a complete
828 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000829 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000830 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000831}
832
833/// getAccessSpecifierIfPresent - Determine whether the next token is
834/// a C++ access-specifier.
835///
836/// access-specifier: [C++ class.derived]
837/// 'private'
838/// 'protected'
839/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000840AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000841{
842 switch (Tok.getKind()) {
843 default: return AS_none;
844 case tok::kw_private: return AS_private;
845 case tok::kw_protected: return AS_protected;
846 case tok::kw_public: return AS_public;
847 }
848}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000849
Eli Friedmand33133c2009-07-22 21:45:50 +0000850void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
851 DeclPtrTy ThisDecl) {
852 // We just declared a member function. If this member function
853 // has any default arguments, we'll need to parse them later.
854 LateParsedMethodDeclaration *LateMethod = 0;
855 DeclaratorChunk::FunctionTypeInfo &FTI
856 = DeclaratorInfo.getTypeObject(0).Fun;
857 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
858 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
859 if (!LateMethod) {
860 // Push this method onto the stack of late-parsed method
861 // declarations.
862 getCurrentClass().MethodDecls.push_back(
863 LateParsedMethodDeclaration(ThisDecl));
864 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +0000865 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +0000866
867 // Add all of the parameters prior to this one (they don't
868 // have default arguments).
869 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
870 for (unsigned I = 0; I < ParamIdx; ++I)
871 LateMethod->DefaultArgs.push_back(
872 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
873 }
874
875 // Add this parameter to the list of parameters (it or may
876 // not have a default argument).
877 LateMethod->DefaultArgs.push_back(
878 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
879 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
880 }
881 }
882}
883
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000884/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
885///
886/// member-declaration:
887/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
888/// function-definition ';'[opt]
889/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
890/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000891/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000892/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000893/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000894///
895/// member-declarator-list:
896/// member-declarator
897/// member-declarator-list ',' member-declarator
898///
899/// member-declarator:
900/// declarator pure-specifier[opt]
901/// declarator constant-initializer[opt]
902/// identifier[opt] ':' constant-expression
903///
Sebastian Redle2b68332009-04-12 17:16:29 +0000904/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000905/// '= 0'
906///
907/// constant-initializer:
908/// '=' constant-expression
909///
Douglas Gregor37b372b2009-08-20 22:52:58 +0000910void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
911 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000912 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000913 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000914 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +0000915 SourceLocation DeclEnd;
916 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000917 return;
918 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000919
Chris Lattner682bf922009-03-29 16:50:03 +0000920 if (Tok.is(tok::kw_template)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000921 assert(!TemplateInfo.TemplateParams &&
922 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +0000923 SourceLocation DeclEnd;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000924 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
925 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000926 return;
927 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000928
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000929 // Handle: member-declaration ::= '__extension__' member-declaration
930 if (Tok.is(tok::kw___extension__)) {
931 // __extension__ silences extension warnings in the subexpression.
932 ExtensionRAIIObject O(Diags); // Use RAII to do this.
933 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +0000934 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000935 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000936
937 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000938 // FIXME: Check for template aliases
939
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000940 // Eat 'using'.
941 SourceLocation UsingLoc = ConsumeToken();
942
943 if (Tok.is(tok::kw_namespace)) {
944 Diag(UsingLoc, diag::err_using_namespace_in_class);
945 SkipUntil(tok::semi, true, true);
946 }
947 else {
948 SourceLocation DeclEnd;
949 // Otherwise, it must be using-declaration.
950 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd);
951 }
952 return;
953 }
954
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000955 SourceLocation DSStart = Tok.getLocation();
956 // decl-specifier-seq:
957 // Parse the common declaration-specifiers piece.
958 DeclSpec DS;
Douglas Gregor37b372b2009-08-20 22:52:58 +0000959 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000960
961 if (Tok.is(tok::semi)) {
962 ConsumeToken();
John McCall67d1a672009-08-06 02:15:43 +0000963
Douglas Gregor37b372b2009-08-20 22:52:58 +0000964 // FIXME: Friend templates?
John McCall67d1a672009-08-06 02:15:43 +0000965 if (DS.isFriendSpecified())
John McCall3f9a8a62009-08-11 06:59:38 +0000966 Actions.ActOnFriendDecl(CurScope, &DS, /*IsDefinition*/ false);
John McCall67d1a672009-08-06 02:15:43 +0000967 else
Chris Lattner682bf922009-03-29 16:50:03 +0000968 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +0000969
970 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000971 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000972
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000973 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000974
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000975 if (Tok.isNot(tok::colon)) {
976 // Parse the first declarator.
977 ParseDeclarator(DeclaratorInfo);
978 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000979 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000980 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000981 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000982 if (Tok.is(tok::semi))
983 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000984 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000985 }
986
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000987 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000988 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +0000989 || (DeclaratorInfo.isFunctionDeclarator() &&
990 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000991 if (!DeclaratorInfo.isFunctionDeclarator()) {
992 Diag(Tok, diag::err_func_def_no_params);
993 ConsumeBrace();
994 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000995 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000996 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000997
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000998 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
999 Diag(Tok, diag::err_function_declared_typedef);
1000 // This recovery skips the entire function body. It would be nice
1001 // to simply call ParseCXXInlineMethodDef() below, however Sema
1002 // assumes the declarator represents a function, not a typedef.
1003 ConsumeBrace();
1004 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001005 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001006 }
1007
Douglas Gregor37b372b2009-08-20 22:52:58 +00001008 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001009 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001010 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001011 }
1012
1013 // member-declarator-list:
1014 // member-declarator
1015 // member-declarator-list ',' member-declarator
1016
Chris Lattner682bf922009-03-29 16:50:03 +00001017 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001018 OwningExprResult BitfieldSize(Actions);
1019 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001020 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001021
1022 while (1) {
1023
1024 // member-declarator:
1025 // declarator pure-specifier[opt]
1026 // declarator constant-initializer[opt]
1027 // identifier[opt] ':' constant-expression
1028
1029 if (Tok.is(tok::colon)) {
1030 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001031 BitfieldSize = ParseConstantExpression();
1032 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001033 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001034 }
1035
1036 // pure-specifier:
1037 // '= 0'
1038 //
1039 // constant-initializer:
1040 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001041 //
1042 // defaulted/deleted function-definition:
1043 // '=' 'default' [TODO]
1044 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001045
1046 if (Tok.is(tok::equal)) {
1047 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001048 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1049 ConsumeToken();
1050 Deleted = true;
1051 } else {
1052 Init = ParseInitializer();
1053 if (Init.isInvalid())
1054 SkipUntil(tok::comma, true, true);
1055 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001056 }
1057
1058 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001059 if (Tok.is(tok::kw___attribute)) {
1060 SourceLocation Loc;
1061 AttributeList *AttrList = ParseAttributes(&Loc);
1062 DeclaratorInfo.AddAttributes(AttrList, Loc);
1063 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001064
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001065 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001066 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001067 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001068
1069 DeclPtrTy ThisDecl;
1070 if (DS.isFriendSpecified()) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001071 // TODO: handle initializers, bitfields, 'delete', friend templates
John McCall3f9a8a62009-08-11 06:59:38 +00001072 ThisDecl = Actions.ActOnFriendDecl(CurScope, &DeclaratorInfo,
1073 /*IsDefinition*/ false);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001074 } else {
1075 Action::MultiTemplateParamsArg TemplateParams(Actions,
1076 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1077 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
John McCall67d1a672009-08-06 02:15:43 +00001078 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1079 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001080 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001081 BitfieldSize.release(),
1082 Init.release(),
1083 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001084 }
Chris Lattner682bf922009-03-29 16:50:03 +00001085 if (ThisDecl)
1086 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001087
Douglas Gregor72b505b2008-12-16 21:30:33 +00001088 if (DeclaratorInfo.isFunctionDeclarator() &&
1089 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1090 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001091 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001092 }
1093
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001094 // If we don't have a comma, it is either the end of the list (a ';')
1095 // or an error, bail out.
1096 if (Tok.isNot(tok::comma))
1097 break;
1098
1099 // Consume the comma.
1100 ConsumeToken();
1101
1102 // Parse the next declarator.
1103 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001104 BitfieldSize = 0;
1105 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001106 Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001107
1108 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001109 if (Tok.is(tok::kw___attribute)) {
1110 SourceLocation Loc;
1111 AttributeList *AttrList = ParseAttributes(&Loc);
1112 DeclaratorInfo.AddAttributes(AttrList, Loc);
1113 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001114
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001115 if (Tok.isNot(tok::colon))
1116 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001117 }
1118
1119 if (Tok.is(tok::semi)) {
1120 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001121 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001122 DeclsInGroup.size());
1123 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001124 }
1125
1126 Diag(Tok, diag::err_expected_semi_decl_list);
1127 // Skip to end of block or statement
1128 SkipUntil(tok::r_brace, true, true);
1129 if (Tok.is(tok::semi))
1130 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001131 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001132}
1133
1134/// ParseCXXMemberSpecification - Parse the class definition.
1135///
1136/// member-specification:
1137/// member-declaration member-specification[opt]
1138/// access-specifier ':' member-specification[opt]
1139///
1140void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001141 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001142 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001143 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001144 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001145
Chris Lattner49f28ca2009-03-05 08:00:35 +00001146 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1147 PP.getSourceManager(),
1148 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001149
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001150 SourceLocation LBraceLoc = ConsumeBrace();
1151
Douglas Gregor6569d682009-05-27 23:11:45 +00001152 // Determine whether this is a top-level (non-nested) class.
1153 bool TopLevelClass = ClassStack.empty() ||
1154 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001155
1156 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001157 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001158
Douglas Gregor6569d682009-05-27 23:11:45 +00001159 // Note that we are parsing a new (potentially-nested) class definition.
1160 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1161
Douglas Gregorddc29e12009-02-06 22:42:48 +00001162 if (TagDecl)
1163 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1164 else {
1165 SkipUntil(tok::r_brace, false, false);
1166 return;
1167 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001168
1169 // C++ 11p3: Members of a class defined with the keyword class are private
1170 // by default. Members of a class defined with the keywords struct or union
1171 // are public by default.
1172 AccessSpecifier CurAS;
1173 if (TagType == DeclSpec::TST_class)
1174 CurAS = AS_private;
1175 else
1176 CurAS = AS_public;
1177
1178 // While we still have something to read, read the member-declarations.
1179 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1180 // Each iteration of this loop reads one member-declaration.
1181
1182 // Check for extraneous top-level semicolon.
1183 if (Tok.is(tok::semi)) {
1184 Diag(Tok, diag::ext_extra_struct_semi);
1185 ConsumeToken();
1186 continue;
1187 }
1188
1189 AccessSpecifier AS = getAccessSpecifierIfPresent();
1190 if (AS != AS_none) {
1191 // Current token is a C++ access specifier.
1192 CurAS = AS;
1193 ConsumeToken();
1194 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1195 continue;
1196 }
1197
Douglas Gregor37b372b2009-08-20 22:52:58 +00001198 // FIXME: Make sure we don't have a template here.
1199
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001200 // Parse all the comma separated declarators.
1201 ParseCXXClassMemberDeclaration(CurAS);
1202 }
1203
1204 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1205
1206 AttributeList *AttrList = 0;
1207 // If attributes exist after class contents, parse them.
1208 if (Tok.is(tok::kw___attribute))
1209 AttrList = ParseAttributes(); // FIXME: where should I put them?
1210
1211 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1212 LBraceLoc, RBraceLoc);
1213
1214 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1215 // complete within function bodies, default arguments,
1216 // exception-specifications, and constructor ctor-initializers (including
1217 // such things in nested classes).
1218 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001219 // FIXME: Only function bodies and constructor ctor-initializers are
1220 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001221 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001222 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001223 // are complete and we can parse the delayed portions of method
1224 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001225 ParseLexedMethodDeclarations(getCurrentClass());
1226 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001227 }
1228
1229 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001230 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001231 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001232
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001233 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001234}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001235
1236/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1237/// which explicitly initializes the members or base classes of a
1238/// class (C++ [class.base.init]). For example, the three initializers
1239/// after the ':' in the Derived constructor below:
1240///
1241/// @code
1242/// class Base { };
1243/// class Derived : Base {
1244/// int x;
1245/// float f;
1246/// public:
1247/// Derived(float f) : Base(), x(17), f(f) { }
1248/// };
1249/// @endcode
1250///
1251/// [C++] ctor-initializer:
1252/// ':' mem-initializer-list
1253///
1254/// [C++] mem-initializer-list:
1255/// mem-initializer
1256/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001257void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001258 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1259
1260 SourceLocation ColonLoc = ConsumeToken();
1261
1262 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1263
1264 do {
1265 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001266 if (!MemInit.isInvalid())
1267 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001268
1269 if (Tok.is(tok::comma))
1270 ConsumeToken();
1271 else if (Tok.is(tok::l_brace))
1272 break;
1273 else {
1274 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001275 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001276 SkipUntil(tok::l_brace, true, true);
1277 break;
1278 }
1279 } while (true);
1280
1281 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001282 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001283}
1284
1285/// ParseMemInitializer - Parse a C++ member initializer, which is
1286/// part of a constructor initializer that explicitly initializes one
1287/// member or base class (C++ [class.base.init]). See
1288/// ParseConstructorInitializer for an example.
1289///
1290/// [C++] mem-initializer:
1291/// mem-initializer-id '(' expression-list[opt] ')'
1292///
1293/// [C++] mem-initializer-id:
1294/// '::'[opt] nested-name-specifier[opt] class-name
1295/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001296Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001297 // parse '::'[opt] nested-name-specifier[opt]
1298 CXXScopeSpec SS;
1299 ParseOptionalCXXScopeSpecifier(SS);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001300 TypeTy *TemplateTypeTy = 0;
1301 if (Tok.is(tok::annot_template_id)) {
1302 TemplateIdAnnotation *TemplateId
1303 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1304 if (TemplateId->Kind == TNK_Type_template) {
1305 AnnotateTemplateIdTokenAsType(&SS);
1306 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1307 TemplateTypeTy = Tok.getAnnotationValue();
1308 }
1309 // FIXME. May need to check for TNK_Dependent_template as well.
1310 }
1311 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001312 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001313 return true;
1314 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001315
Douglas Gregor7ad83902008-11-05 04:29:56 +00001316 // Get the identifier. This may be a member name or a class name,
1317 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001318 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001319 SourceLocation IdLoc = ConsumeToken();
1320
1321 // Parse the '('.
1322 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001323 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001324 return true;
1325 }
1326 SourceLocation LParenLoc = ConsumeParen();
1327
1328 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001329 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001330 CommaLocsTy CommaLocs;
1331 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1332 SkipUntil(tok::r_paren);
1333 return true;
1334 }
1335
1336 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1337
Fariborz Jahanian96174332009-07-01 19:21:19 +00001338 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1339 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001340 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001341 ArgExprs.size(), CommaLocs.data(),
1342 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001343}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001344
1345/// ParseExceptionSpecification - Parse a C++ exception-specification
1346/// (C++ [except.spec]).
1347///
Douglas Gregora4745612008-12-01 18:00:20 +00001348/// exception-specification:
1349/// 'throw' '(' type-id-list [opt] ')'
1350/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001351///
Douglas Gregora4745612008-12-01 18:00:20 +00001352/// type-id-list:
1353/// type-id
1354/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001355///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001356bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001357 llvm::SmallVector<TypeTy*, 2>
1358 &Exceptions,
1359 llvm::SmallVector<SourceRange, 2>
1360 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001361 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001362 assert(Tok.is(tok::kw_throw) && "expected throw");
1363
1364 SourceLocation ThrowLoc = ConsumeToken();
1365
1366 if (!Tok.is(tok::l_paren)) {
1367 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1368 }
1369 SourceLocation LParenLoc = ConsumeParen();
1370
Douglas Gregora4745612008-12-01 18:00:20 +00001371 // Parse throw(...), a Microsoft extension that means "this function
1372 // can throw anything".
1373 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001374 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001375 SourceLocation EllipsisLoc = ConsumeToken();
1376 if (!getLang().Microsoft)
1377 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001378 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001379 return false;
1380 }
1381
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001382 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001383 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001384 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001385 TypeResult Res(ParseTypeName(&Range));
1386 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001387 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001388 Ranges.push_back(Range);
1389 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001390 if (Tok.is(tok::comma))
1391 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001392 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001393 break;
1394 }
1395
Sebastian Redlab197ba2009-02-09 18:23:29 +00001396 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001397 return false;
1398}
Douglas Gregor6569d682009-05-27 23:11:45 +00001399
1400/// \brief We have just started parsing the definition of a new class,
1401/// so push that class onto our stack of classes that is currently
1402/// being parsed.
1403void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1404 assert((TopLevelClass || !ClassStack.empty()) &&
1405 "Nested class without outer class");
1406 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1407}
1408
1409/// \brief Deallocate the given parsed class and all of its nested
1410/// classes.
1411void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1412 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1413 DeallocateParsedClasses(Class->NestedClasses[I]);
1414 delete Class;
1415}
1416
1417/// \brief Pop the top class of the stack of classes that are
1418/// currently being parsed.
1419///
1420/// This routine should be called when we have finished parsing the
1421/// definition of a class, but have not yet popped the Scope
1422/// associated with the class's definition.
1423///
1424/// \returns true if the class we've popped is a top-level class,
1425/// false otherwise.
1426void Parser::PopParsingClass() {
1427 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1428
1429 ParsingClass *Victim = ClassStack.top();
1430 ClassStack.pop();
1431 if (Victim->TopLevelClass) {
1432 // Deallocate all of the nested classes of this class,
1433 // recursively: we don't need to keep any of this information.
1434 DeallocateParsedClasses(Victim);
1435 return;
1436 }
1437 assert(!ClassStack.empty() && "Missing top-level class?");
1438
1439 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1440 Victim->NestedClasses.empty()) {
1441 // The victim is a nested class, but we will not need to perform
1442 // any processing after the definition of this class since it has
1443 // no members whose handling was delayed. Therefore, we can just
1444 // remove this nested class.
1445 delete Victim;
1446 return;
1447 }
1448
1449 // This nested class has some members that will need to be processed
1450 // after the top-level class is completely defined. Therefore, add
1451 // it to the list of nested classes within its parent.
1452 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1453 ClassStack.top()->NestedClasses.push_back(Victim);
1454 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1455}