blob: b21ac778ba5cecd2e42a4ae8efd82fa3cda9e416 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000017#include "clang/Parse/Scope.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000018#include "AstGuard.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///
45Parser::DeclTy *Parser::ParseNamespace(unsigned Context) {
Chris Lattner04d66662007-10-09 17:33:22 +000046 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000047 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
48
49 SourceLocation IdentLoc;
50 IdentifierInfo *Ident = 0;
51
Chris Lattner04d66662007-10-09 17:33:22 +000052 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000053 Ident = Tok.getIdentifierInfo();
54 IdentLoc = ConsumeToken(); // eat the identifier.
55 }
56
57 // Read label attributes, if present.
58 DeclTy *AttrList = 0;
Chris Lattner04d66662007-10-09 17:33:22 +000059 if (Tok.is(tok::kw___attribute))
Chris Lattner8f08cb72007-08-25 06:57:03 +000060 // FIXME: save these somewhere.
61 AttrList = ParseAttributes();
62
Chris Lattner04d66662007-10-09 17:33:22 +000063 if (Tok.is(tok::equal)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000064 // FIXME: Verify no attributes were present.
65 // FIXME: parse this.
Chris Lattner04d66662007-10-09 17:33:22 +000066 } else if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000067
Chris Lattner8f08cb72007-08-25 06:57:03 +000068 SourceLocation LBrace = ConsumeBrace();
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000069
70 // Enter a scope for the namespace.
Douglas Gregor8935b8b2008-12-10 06:34:36 +000071 ParseScope NamespaceScope(this, Scope::DeclScope);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000072
73 DeclTy *NamespcDecl =
74 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
75
Chris Lattner49f28ca2009-03-05 08:00:35 +000076 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
77 PP.getSourceManager(),
78 "parsing namespace");
Chris Lattner2254a9f2009-03-05 02:09:07 +000079
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000080 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
Chris Lattnerbae35112007-08-25 18:15:16 +000081 ParseExternalDeclaration();
Chris Lattner8f08cb72007-08-25 06:57:03 +000082
Argyrios Kyrtzidis8ba5d792008-05-01 21:44:34 +000083 // Leave the namespace scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +000084 NamespaceScope.Exit();
Argyrios Kyrtzidis8ba5d792008-05-01 21:44:34 +000085
Chris Lattner8f08cb72007-08-25 06:57:03 +000086 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000087 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace);
88
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000089 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +000090
Chris Lattner8f08cb72007-08-25 06:57:03 +000091 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +000092 Diag(Tok, Ident ? diag::err_expected_lbrace :
93 diag::err_expected_ident_lbrace);
Chris Lattner8f08cb72007-08-25 06:57:03 +000094 }
95
96 return 0;
97}
Chris Lattnerc6fdc342008-01-12 07:05:38 +000098
99/// ParseLinkage - We know that the current token is a string_literal
100/// and just before that, that extern was seen.
101///
102/// linkage-specification: [C++ 7.5p2: dcl.link]
103/// 'extern' string-literal '{' declaration-seq[opt] '}'
104/// 'extern' string-literal declaration
105///
106Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000107 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000108 llvm::SmallVector<char, 8> LangBuffer;
109 // LangBuffer is guaranteed to be big enough.
110 LangBuffer.resize(Tok.getLength());
111 const char *LangBufPtr = &LangBuffer[0];
112 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
113
114 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000115
Douglas Gregor074149e2009-01-05 19:45:36 +0000116 ParseScope LinkageScope(this, Scope::DeclScope);
117 DeclTy *LinkageSpec
118 = Actions.ActOnStartLinkageSpecification(CurScope,
119 /*FIXME: */SourceLocation(),
120 Loc, LangBufPtr, StrSize,
121 Tok.is(tok::l_brace)? Tok.getLocation()
122 : SourceLocation());
123
124 if (Tok.isNot(tok::l_brace)) {
125 ParseDeclarationOrFunctionDefinition();
126 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
127 SourceLocation());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000128 }
129
130 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000131 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000132 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000133 }
134
Douglas Gregorf44515a2008-12-16 22:23:02 +0000135 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000136 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000137}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000138
Douglas Gregorf780abc2008-12-30 03:27:21 +0000139/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
140/// using-directive. Assumes that current token is 'using'.
Chris Lattner2f274772009-01-06 06:55:51 +0000141Parser::DeclTy *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000142 assert(Tok.is(tok::kw_using) && "Not using token");
143
144 // Eat 'using'.
145 SourceLocation UsingLoc = ConsumeToken();
146
Chris Lattner2f274772009-01-06 06:55:51 +0000147 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000148 // Next token after 'using' is 'namespace' so it must be using-directive
149 return ParseUsingDirective(Context, UsingLoc);
Chris Lattner2f274772009-01-06 06:55:51 +0000150
151 // Otherwise, it must be using-declaration.
152 return ParseUsingDeclaration(Context, UsingLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000153}
154
155/// ParseUsingDirective - Parse C++ using-directive, assumes
156/// that current token is 'namespace' and 'using' was already parsed.
157///
158/// using-directive: [C++ 7.3.p4: namespace.udir]
159/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
160/// namespace-name ;
161/// [GNU] using-directive:
162/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
163/// namespace-name attributes[opt] ;
164///
165Parser::DeclTy *Parser::ParseUsingDirective(unsigned Context,
166 SourceLocation UsingLoc) {
167 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
168
169 // Eat 'namespace'.
170 SourceLocation NamespcLoc = ConsumeToken();
171
172 CXXScopeSpec SS;
173 // Parse (optional) nested-name-specifier.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000174 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000175
176 AttributeList *AttrList = 0;
177 IdentifierInfo *NamespcName = 0;
178 SourceLocation IdentLoc = SourceLocation();
179
180 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000181 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000182 Diag(Tok, diag::err_expected_namespace_name);
183 // If there was invalid namespace name, skip to end of decl, and eat ';'.
184 SkipUntil(tok::semi);
185 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
186 return 0;
187 }
Chris Lattner823c44e2009-01-06 07:27:21 +0000188
189 // Parse identifier.
190 NamespcName = Tok.getIdentifierInfo();
191 IdentLoc = ConsumeToken();
192
193 // Parse (optional) attributes (most likely GNU strong-using extension).
194 if (Tok.is(tok::kw___attribute))
195 AttrList = ParseAttributes();
196
197 // Eat ';'.
198 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
199 AttrList ? "attributes list" : "namespace name", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000200
201 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000202 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000203}
204
205/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
206/// 'using' was already seen.
207///
208/// using-declaration: [C++ 7.3.p3: namespace.udecl]
209/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
210/// unqualified-id [TODO]
211/// 'using' :: unqualified-id [TODO]
212///
213Parser::DeclTy *Parser::ParseUsingDeclaration(unsigned Context,
214 SourceLocation UsingLoc) {
215 assert(false && "Not implemented");
216 // FIXME: Implement parsing.
217 return 0;
218}
219
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000220/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
221///
222/// static_assert-declaration:
223/// static_assert ( constant-expression , string-literal ) ;
224///
225Parser::DeclTy *Parser::ParseStaticAssertDeclaration() {
226 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
227 SourceLocation StaticAssertLoc = ConsumeToken();
228
229 if (Tok.isNot(tok::l_paren)) {
230 Diag(Tok, diag::err_expected_lparen);
231 return 0;
232 }
233
234 SourceLocation LParenLoc = ConsumeParen();
235
236 OwningExprResult AssertExpr(ParseConstantExpression());
237 if (AssertExpr.isInvalid()) {
238 SkipUntil(tok::semi);
239 return 0;
240 }
241
242 if (Tok.isNot(tok::comma)) {
243 Diag(Tok, diag::err_expected_comma);
244 SkipUntil(tok::semi);
245 return 0;
246 }
247
248 SourceLocation CommaLoc = ConsumeToken();
249
250 if (Tok.isNot(tok::string_literal)) {
251 Diag(Tok, diag::err_expected_string_literal);
252 SkipUntil(tok::semi);
253 return 0;
254 }
255
256 OwningExprResult AssertMessage(ParseStringLiteralExpression());
257 if (AssertMessage.isInvalid())
258 return 0;
259
260 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
261
262 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
263
264 return Actions.ActOnStaticAssertDeclaration(LParenLoc, move(AssertExpr),
265 CommaLoc, move(AssertMessage),
266 RParenLoc);
267}
268
Douglas Gregor42a552f2008-11-05 20:51:48 +0000269/// ParseClassName - Parse a C++ class-name, which names a class. Note
270/// that we only check that the result names a type; semantic analysis
271/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000272/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000273/// found.
274///
275/// class-name: [C++ 9.1]
276/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000277/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000278///
Douglas Gregor7f43d672009-02-25 23:52:28 +0000279Parser::TypeTy *Parser::ParseClassName(SourceLocation &EndLocation,
280 const CXXScopeSpec *SS) {
281 // Check whether we have a template-id that names a type.
282 if (Tok.is(tok::annot_template_id)) {
283 TemplateIdAnnotation *TemplateId
284 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
285 if (TemplateId->Kind == TNK_Class_template) {
286 if (AnnotateTemplateIdTokenAsType(SS))
287 return 0;
288
289 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
290 TypeTy *Type = Tok.getAnnotationValue();
291 EndLocation = Tok.getAnnotationEndLoc();
292 ConsumeToken();
293 return Type;
294 }
295
296 // Fall through to produce an error below.
297 }
298
Douglas Gregor42a552f2008-11-05 20:51:48 +0000299 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000300 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000301 return 0;
302 }
303
304 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000305 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
306 Tok.getLocation(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000307 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000308 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000309 return 0;
310 }
311
312 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000313 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000314 return Type;
315}
316
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000317/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
318/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
319/// until we reach the start of a definition or see a token that
320/// cannot start a definition.
321///
322/// class-specifier: [C++ class]
323/// class-head '{' member-specification[opt] '}'
324/// class-head '{' member-specification[opt] '}' attributes[opt]
325/// class-head:
326/// class-key identifier[opt] base-clause[opt]
327/// class-key nested-name-specifier identifier base-clause[opt]
328/// class-key nested-name-specifier[opt] simple-template-id
329/// base-clause[opt]
330/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
331/// [GNU] class-key attributes[opt] nested-name-specifier
332/// identifier base-clause[opt]
333/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
334/// simple-template-id base-clause[opt]
335/// class-key:
336/// 'class'
337/// 'struct'
338/// 'union'
339///
340/// elaborated-type-specifier: [C++ dcl.type.elab]
341/// class-key ::[opt] nested-name-specifier[opt] identifier
342/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
343/// simple-template-id
344///
345/// Note that the C++ class-specifier and elaborated-type-specifier,
346/// together, subsume the C99 struct-or-union-specifier:
347///
348/// struct-or-union-specifier: [C99 6.7.2.1]
349/// struct-or-union identifier[opt] '{' struct-contents '}'
350/// struct-or-union identifier
351/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
352/// '}' attributes[opt]
353/// [GNU] struct-or-union attributes[opt] identifier
354/// struct-or-union:
355/// 'struct'
356/// 'union'
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000357void Parser::ParseClassSpecifier(DeclSpec &DS,
358 TemplateParameterLists *TemplateParams) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000359 assert((Tok.is(tok::kw_class) ||
360 Tok.is(tok::kw_struct) ||
361 Tok.is(tok::kw_union)) &&
362 "Not a class specifier");
363 DeclSpec::TST TagType =
364 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
365 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
366 DeclSpec::TST_union;
367
368 SourceLocation StartLoc = ConsumeToken();
369
370 AttributeList *Attr = 0;
371 // If attributes exist after tag, parse them.
372 if (Tok.is(tok::kw___attribute))
373 Attr = ParseAttributes();
374
Steve Narofff59e17e2008-12-24 20:59:21 +0000375 // If declspecs exist after tag, parse them.
376 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
377 FuzzyParseMicrosoftDeclSpec();
378
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000379 // Parse the (optional) nested-name-specifier.
380 CXXScopeSpec SS;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000381 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
382 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000383 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000384
385 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000386 IdentifierInfo *Name = 0;
387 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000388 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000389 if (Tok.is(tok::identifier)) {
390 Name = Tok.getIdentifierInfo();
391 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000392 } else if (Tok.is(tok::annot_template_id)) {
393 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
394 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000395
Douglas Gregor39a8de12009-02-25 19:37:18 +0000396 if (TemplateId->Kind != TNK_Class_template) {
397 // The template-name in the simple-template-id refers to
398 // something other than a class template. Give an appropriate
399 // error message and skip to the ';'.
400 SourceRange Range(NameLoc);
401 if (SS.isNotEmpty())
402 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000403
Douglas Gregor39a8de12009-02-25 19:37:18 +0000404 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
405 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000406
Douglas Gregor39a8de12009-02-25 19:37:18 +0000407 DS.SetTypeSpecError();
408 SkipUntil(tok::semi, false, true);
409 TemplateId->Destroy();
410 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000411 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000412 }
413
414 // There are three options here. If we have 'struct foo;', then
415 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000416 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000417 // something like 'struct foo xyz', a reference.
418 Action::TagKind TK;
419 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
420 TK = Action::TK_Definition;
421 else if (Tok.is(tok::semi))
422 TK = Action::TK_Declaration;
423 else
424 TK = Action::TK_Reference;
425
Douglas Gregor39a8de12009-02-25 19:37:18 +0000426 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000427 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000428 Diag(StartLoc, diag::err_anon_type_definition)
429 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000430
431 // Skip the rest of this declarator, up until the comma or semicolon.
432 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000433
434 if (TemplateId)
435 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000436 return;
437 }
438
Douglas Gregorddc29e12009-02-06 22:42:48 +0000439 // Create the tag portion of the class or class template.
440 DeclTy *TagOrTempDecl;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000441 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000442 // Explicit specialization or class template partial
443 // specialization. Let semantic analysis decide.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000444 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
445 TemplateId->getTemplateArgs(),
446 TemplateId->getTemplateArgIsType(),
447 TemplateId->NumArgs);
Douglas Gregorcc636682009-02-17 23:15:12 +0000448 TagOrTempDecl
449 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000450 StartLoc, SS,
451 TemplateId->Template,
452 TemplateId->TemplateNameLoc,
453 TemplateId->LAngleLoc,
454 TemplateArgsPtr,
455 TemplateId->getTemplateArgLocations(),
456 TemplateId->RAngleLoc,
457 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000458 Action::MultiTemplateParamsArg(Actions,
459 TemplateParams? &(*TemplateParams)[0] : 0,
460 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor39a8de12009-02-25 19:37:18 +0000461 TemplateId->Destroy();
462 } else if (TemplateParams && TK != Action::TK_Reference)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000463 TagOrTempDecl = Actions.ActOnClassTemplate(CurScope, TagType, TK, StartLoc,
464 SS, Name, NameLoc, Attr,
465 Action::MultiTemplateParamsArg(Actions,
466 &(*TemplateParams)[0],
467 TemplateParams->size()));
468 else
469 TagOrTempDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
470 NameLoc, Attr);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000471
472 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000473 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000474 ParseBaseClause(TagOrTempDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000475
476 // If there is a body, parse it and inform the actions module.
477 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000478 if (getLang().CPlusPlus)
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000479 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempDecl);
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000480 else
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000481 ParseStructUnionBody(StartLoc, TagType, TagOrTempDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000482 else if (TK == Action::TK_Definition) {
483 // FIXME: Complain that we have a base-specifier list but no
484 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000485 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000486 }
487
488 const char *PrevSpec = 0;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000489 if (!TagOrTempDecl)
490 DS.SetTypeSpecError();
491 else if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagOrTempDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000492 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000493}
494
495/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
496///
497/// base-clause : [C++ class.derived]
498/// ':' base-specifier-list
499/// base-specifier-list:
500/// base-specifier '...'[opt]
501/// base-specifier-list ',' base-specifier '...'[opt]
502void Parser::ParseBaseClause(DeclTy *ClassDecl)
503{
504 assert(Tok.is(tok::colon) && "Not a base clause");
505 ConsumeToken();
506
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000507 // Build up an array of parsed base specifiers.
508 llvm::SmallVector<BaseTy *, 8> BaseInfo;
509
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000510 while (true) {
511 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000512 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000513 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000514 // Skip the rest of this base specifier, up until the comma or
515 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000516 SkipUntil(tok::comma, tok::l_brace, true, true);
517 } else {
518 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000519 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000520 }
521
522 // If the next token is a comma, consume it and keep reading
523 // base-specifiers.
524 if (Tok.isNot(tok::comma)) break;
525
526 // Consume the comma.
527 ConsumeToken();
528 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000529
530 // Attach the base specifiers
531 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000532}
533
534/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
535/// one entry in the base class list of a class specifier, for example:
536/// class foo : public bar, virtual private baz {
537/// 'public bar' and 'virtual private baz' are each base-specifiers.
538///
539/// base-specifier: [C++ class.derived]
540/// ::[opt] nested-name-specifier[opt] class-name
541/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
542/// class-name
543/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
544/// class-name
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000545Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000546{
547 bool IsVirtual = false;
548 SourceLocation StartLoc = Tok.getLocation();
549
550 // Parse the 'virtual' keyword.
551 if (Tok.is(tok::kw_virtual)) {
552 ConsumeToken();
553 IsVirtual = true;
554 }
555
556 // Parse an (optional) access specifier.
557 AccessSpecifier Access = getAccessSpecifierIfPresent();
558 if (Access)
559 ConsumeToken();
560
561 // Parse the 'virtual' keyword (again!), in case it came after the
562 // access specifier.
563 if (Tok.is(tok::kw_virtual)) {
564 SourceLocation VirtualLoc = ConsumeToken();
565 if (IsVirtual) {
566 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000567 Diag(VirtualLoc, diag::err_dup_virtual)
568 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000569 }
570
571 IsVirtual = true;
572 }
573
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000574 // Parse optional '::' and optional nested-name-specifier.
575 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000576 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000577
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000578 // The location of the base class itself.
579 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000580
581 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000582 SourceLocation EndLocation;
583 TypeTy *BaseType = ParseClassName(EndLocation, &SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000584 if (!BaseType)
585 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000586
587 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000588 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000589
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000590 // Notify semantic analysis that we have parsed a complete
591 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000592 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
593 BaseType, BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000594}
595
596/// getAccessSpecifierIfPresent - Determine whether the next token is
597/// a C++ access-specifier.
598///
599/// access-specifier: [C++ class.derived]
600/// 'private'
601/// 'protected'
602/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000603AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000604{
605 switch (Tok.getKind()) {
606 default: return AS_none;
607 case tok::kw_private: return AS_private;
608 case tok::kw_protected: return AS_protected;
609 case tok::kw_public: return AS_public;
610 }
611}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000612
613/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
614///
615/// member-declaration:
616/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
617/// function-definition ';'[opt]
618/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
619/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000620/// [C++0x] static_assert-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000621/// template-declaration [TODO]
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000622/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000623///
624/// member-declarator-list:
625/// member-declarator
626/// member-declarator-list ',' member-declarator
627///
628/// member-declarator:
629/// declarator pure-specifier[opt]
630/// declarator constant-initializer[opt]
631/// identifier[opt] ':' constant-expression
632///
633/// pure-specifier: [TODO]
634/// '= 0'
635///
636/// constant-initializer:
637/// '=' constant-expression
638///
639Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000640 // static_assert-declaration
641 if (Tok.is(tok::kw_static_assert))
642 return ParseStaticAssertDeclaration();
643
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000644 // Handle: member-declaration ::= '__extension__' member-declaration
645 if (Tok.is(tok::kw___extension__)) {
646 // __extension__ silences extension warnings in the subexpression.
647 ExtensionRAIIObject O(Diags); // Use RAII to do this.
648 ConsumeToken();
649 return ParseCXXClassMemberDeclaration(AS);
650 }
651
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000652 SourceLocation DSStart = Tok.getLocation();
653 // decl-specifier-seq:
654 // Parse the common declaration-specifiers piece.
655 DeclSpec DS;
656 ParseDeclarationSpecifiers(DS);
657
658 if (Tok.is(tok::semi)) {
659 ConsumeToken();
660 // C++ 9.2p7: The member-declarator-list can be omitted only after a
661 // class-specifier or an enum-specifier or in a friend declaration.
662 // FIXME: Friend declarations.
663 switch (DS.getTypeSpecType()) {
664 case DeclSpec::TST_struct:
665 case DeclSpec::TST_union:
666 case DeclSpec::TST_class:
667 case DeclSpec::TST_enum:
668 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
669 default:
670 Diag(DSStart, diag::err_no_declarators);
671 return 0;
672 }
673 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000674
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000675 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000676
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000677 if (Tok.isNot(tok::colon)) {
678 // Parse the first declarator.
679 ParseDeclarator(DeclaratorInfo);
680 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000681 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000682 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000683 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000684 if (Tok.is(tok::semi))
685 ConsumeToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000686 return 0;
687 }
688
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000689 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000690 if (Tok.is(tok::l_brace)
691 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000692 if (!DeclaratorInfo.isFunctionDeclarator()) {
693 Diag(Tok, diag::err_func_def_no_params);
694 ConsumeBrace();
695 SkipUntil(tok::r_brace, true);
696 return 0;
697 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000698
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000699 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
700 Diag(Tok, diag::err_function_declared_typedef);
701 // This recovery skips the entire function body. It would be nice
702 // to simply call ParseCXXInlineMethodDef() below, however Sema
703 // assumes the declarator represents a function, not a typedef.
704 ConsumeBrace();
705 SkipUntil(tok::r_brace, true);
706 return 0;
707 }
708
709 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
710 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000711 }
712
713 // member-declarator-list:
714 // member-declarator
715 // member-declarator-list ',' member-declarator
716
717 DeclTy *LastDeclInGroup = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000718 OwningExprResult BitfieldSize(Actions);
719 OwningExprResult Init(Actions);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000720
721 while (1) {
722
723 // member-declarator:
724 // declarator pure-specifier[opt]
725 // declarator constant-initializer[opt]
726 // identifier[opt] ':' constant-expression
727
728 if (Tok.is(tok::colon)) {
729 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000730 BitfieldSize = ParseConstantExpression();
731 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000732 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000733 }
734
735 // pure-specifier:
736 // '= 0'
737 //
738 // constant-initializer:
739 // '=' constant-expression
740
741 if (Tok.is(tok::equal)) {
742 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000743 Init = ParseInitializer();
744 if (Init.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000745 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000746 }
747
748 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000749 if (Tok.is(tok::kw___attribute)) {
750 SourceLocation Loc;
751 AttributeList *AttrList = ParseAttributes(&Loc);
752 DeclaratorInfo.AddAttributes(AttrList, Loc);
753 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000754
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000755 // NOTE: If Sema is the Action module and declarator is an instance field,
756 // this call will *not* return the created decl; LastDeclInGroup will be
757 // returned instead.
758 // See Sema::ActOnCXXMemberDeclarator for details.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000759 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
760 DeclaratorInfo,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000761 BitfieldSize.release(),
762 Init.release(),
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000763 LastDeclInGroup);
764
Douglas Gregor72b505b2008-12-16 21:30:33 +0000765 if (DeclaratorInfo.isFunctionDeclarator() &&
766 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
767 != DeclSpec::SCS_typedef) {
768 // We just declared a member function. If this member function
769 // has any default arguments, we'll need to parse them later.
770 LateParsedMethodDeclaration *LateMethod = 0;
771 DeclaratorChunk::FunctionTypeInfo &FTI
772 = DeclaratorInfo.getTypeObject(0).Fun;
773 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
774 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
775 if (!LateMethod) {
776 // Push this method onto the stack of late-parsed method
777 // declarations.
778 getCurTopClassStack().MethodDecls.push_back(
779 LateParsedMethodDeclaration(LastDeclInGroup));
780 LateMethod = &getCurTopClassStack().MethodDecls.back();
781
782 // Add all of the parameters prior to this one (they don't
783 // have default arguments).
784 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
785 for (unsigned I = 0; I < ParamIdx; ++I)
786 LateMethod->DefaultArgs.push_back(
787 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
788 }
789
790 // Add this parameter to the list of parameters (it or may
791 // not have a default argument).
792 LateMethod->DefaultArgs.push_back(
793 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
794 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
795 }
796 }
797 }
798
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000799 // If we don't have a comma, it is either the end of the list (a ';')
800 // or an error, bail out.
801 if (Tok.isNot(tok::comma))
802 break;
803
804 // Consume the comma.
805 ConsumeToken();
806
807 // Parse the next declarator.
808 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000809 BitfieldSize = 0;
810 Init = 0;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000811
812 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000813 if (Tok.is(tok::kw___attribute)) {
814 SourceLocation Loc;
815 AttributeList *AttrList = ParseAttributes(&Loc);
816 DeclaratorInfo.AddAttributes(AttrList, Loc);
817 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000818
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000819 if (Tok.isNot(tok::colon))
820 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000821 }
822
823 if (Tok.is(tok::semi)) {
824 ConsumeToken();
825 // Reverse the chain list.
826 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
827 }
828
829 Diag(Tok, diag::err_expected_semi_decl_list);
830 // Skip to end of block or statement
831 SkipUntil(tok::r_brace, true, true);
832 if (Tok.is(tok::semi))
833 ConsumeToken();
834 return 0;
835}
836
837/// ParseCXXMemberSpecification - Parse the class definition.
838///
839/// member-specification:
840/// member-declaration member-specification[opt]
841/// access-specifier ':' member-specification[opt]
842///
843void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
844 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000845 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000846 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000847 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000848
Chris Lattner49f28ca2009-03-05 08:00:35 +0000849 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
850 PP.getSourceManager(),
851 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +0000852
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000853 SourceLocation LBraceLoc = ConsumeBrace();
854
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000855 if (!CurScope->isClassScope() && // Not about to define a nested class.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000856 CurScope->isInCXXInlineMethodScope()) {
857 // We will define a local class of an inline method.
858 // Push a new LexedMethodsForTopClass for its inline methods.
859 PushTopClassStack();
860 }
861
862 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000863 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000864
Douglas Gregorddc29e12009-02-06 22:42:48 +0000865 if (TagDecl)
866 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
867 else {
868 SkipUntil(tok::r_brace, false, false);
869 return;
870 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000871
872 // C++ 11p3: Members of a class defined with the keyword class are private
873 // by default. Members of a class defined with the keywords struct or union
874 // are public by default.
875 AccessSpecifier CurAS;
876 if (TagType == DeclSpec::TST_class)
877 CurAS = AS_private;
878 else
879 CurAS = AS_public;
880
881 // While we still have something to read, read the member-declarations.
882 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
883 // Each iteration of this loop reads one member-declaration.
884
885 // Check for extraneous top-level semicolon.
886 if (Tok.is(tok::semi)) {
887 Diag(Tok, diag::ext_extra_struct_semi);
888 ConsumeToken();
889 continue;
890 }
891
892 AccessSpecifier AS = getAccessSpecifierIfPresent();
893 if (AS != AS_none) {
894 // Current token is a C++ access specifier.
895 CurAS = AS;
896 ConsumeToken();
897 ExpectAndConsume(tok::colon, diag::err_expected_colon);
898 continue;
899 }
900
901 // Parse all the comma separated declarators.
902 ParseCXXClassMemberDeclaration(CurAS);
903 }
904
905 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
906
907 AttributeList *AttrList = 0;
908 // If attributes exist after class contents, parse them.
909 if (Tok.is(tok::kw___attribute))
910 AttrList = ParseAttributes(); // FIXME: where should I put them?
911
912 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
913 LBraceLoc, RBraceLoc);
914
915 // C++ 9.2p2: Within the class member-specification, the class is regarded as
916 // complete within function bodies, default arguments,
917 // exception-specifications, and constructor ctor-initializers (including
918 // such things in nested classes).
919 //
Douglas Gregor72b505b2008-12-16 21:30:33 +0000920 // FIXME: Only function bodies and constructor ctor-initializers are
921 // parsed correctly, fix the rest.
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000922 if (!CurScope->getParent()->isClassScope()) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000923 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +0000924 // are complete and we can parse the delayed portions of method
925 // declarations and the lexed inline method definitions.
926 ParseLexedMethodDeclarations();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000927 ParseLexedMethodDefs();
928
929 // For a local class of inline method, pop the LexedMethodsForTopClass that
930 // was previously pushed.
931
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000932 assert((CurScope->isInCXXInlineMethodScope() ||
933 TopClassStacks.size() == 1) &&
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000934 "MethodLexers not getting popped properly!");
935 if (CurScope->isInCXXInlineMethodScope())
936 PopTopClassStack();
937 }
938
939 // Leave the class scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000940 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000941
Douglas Gregor72de6672009-01-08 20:45:30 +0000942 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000943}
Douglas Gregor7ad83902008-11-05 04:29:56 +0000944
945/// ParseConstructorInitializer - Parse a C++ constructor initializer,
946/// which explicitly initializes the members or base classes of a
947/// class (C++ [class.base.init]). For example, the three initializers
948/// after the ':' in the Derived constructor below:
949///
950/// @code
951/// class Base { };
952/// class Derived : Base {
953/// int x;
954/// float f;
955/// public:
956/// Derived(float f) : Base(), x(17), f(f) { }
957/// };
958/// @endcode
959///
960/// [C++] ctor-initializer:
961/// ':' mem-initializer-list
962///
963/// [C++] mem-initializer-list:
964/// mem-initializer
965/// mem-initializer , mem-initializer-list
966void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
967 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
968
969 SourceLocation ColonLoc = ConsumeToken();
970
971 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
972
973 do {
974 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000975 if (!MemInit.isInvalid())
976 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000977
978 if (Tok.is(tok::comma))
979 ConsumeToken();
980 else if (Tok.is(tok::l_brace))
981 break;
982 else {
983 // Skip over garbage, until we get to '{'. Don't eat the '{'.
984 SkipUntil(tok::l_brace, true, true);
985 break;
986 }
987 } while (true);
988
989 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
990 &MemInitializers[0], MemInitializers.size());
991}
992
993/// ParseMemInitializer - Parse a C++ member initializer, which is
994/// part of a constructor initializer that explicitly initializes one
995/// member or base class (C++ [class.base.init]). See
996/// ParseConstructorInitializer for an example.
997///
998/// [C++] mem-initializer:
999/// mem-initializer-id '(' expression-list[opt] ')'
1000///
1001/// [C++] mem-initializer-id:
1002/// '::'[opt] nested-name-specifier[opt] class-name
1003/// identifier
1004Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
1005 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1006
1007 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001008 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001009 return true;
1010 }
1011
1012 // Get the identifier. This may be a member name or a class name,
1013 // but we'll let the semantic analysis determine which it is.
1014 IdentifierInfo *II = Tok.getIdentifierInfo();
1015 SourceLocation IdLoc = ConsumeToken();
1016
1017 // Parse the '('.
1018 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001019 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001020 return true;
1021 }
1022 SourceLocation LParenLoc = ConsumeParen();
1023
1024 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001025 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001026 CommaLocsTy CommaLocs;
1027 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1028 SkipUntil(tok::r_paren);
1029 return true;
1030 }
1031
1032 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1033
Sebastian Redla55e52c2008-11-25 22:21:31 +00001034 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1035 LParenLoc, ArgExprs.take(),
1036 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001037}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001038
1039/// ParseExceptionSpecification - Parse a C++ exception-specification
1040/// (C++ [except.spec]).
1041///
Douglas Gregora4745612008-12-01 18:00:20 +00001042/// exception-specification:
1043/// 'throw' '(' type-id-list [opt] ')'
1044/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001045///
Douglas Gregora4745612008-12-01 18:00:20 +00001046/// type-id-list:
1047/// type-id
1048/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001049///
Sebastian Redlab197ba2009-02-09 18:23:29 +00001050bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001051 assert(Tok.is(tok::kw_throw) && "expected throw");
1052
1053 SourceLocation ThrowLoc = ConsumeToken();
1054
1055 if (!Tok.is(tok::l_paren)) {
1056 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1057 }
1058 SourceLocation LParenLoc = ConsumeParen();
1059
Douglas Gregora4745612008-12-01 18:00:20 +00001060 // Parse throw(...), a Microsoft extension that means "this function
1061 // can throw anything".
1062 if (Tok.is(tok::ellipsis)) {
1063 SourceLocation EllipsisLoc = ConsumeToken();
1064 if (!getLang().Microsoft)
1065 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001066 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001067 return false;
1068 }
1069
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001070 // Parse the sequence of type-ids.
1071 while (Tok.isNot(tok::r_paren)) {
1072 ParseTypeName();
1073 if (Tok.is(tok::comma))
1074 ConsumeToken();
1075 else
1076 break;
1077 }
1078
Sebastian Redlab197ba2009-02-09 18:23:29 +00001079 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001080 return false;
1081}