blob: 7f0aa8f32795f2b89048af5918cea17a4da82e95 [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 Lattner2254a9f2009-03-05 02:09:07 +000076 PrettyStackTraceDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
77 PP.getSourceManager(),
78 "parsing namespace");
79
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
Douglas Gregor42a552f2008-11-05 20:51:48 +0000220/// ParseClassName - Parse a C++ class-name, which names a class. Note
221/// that we only check that the result names a type; semantic analysis
222/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000223/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000224/// found.
225///
226/// class-name: [C++ 9.1]
227/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000228/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000229///
Douglas Gregor7f43d672009-02-25 23:52:28 +0000230Parser::TypeTy *Parser::ParseClassName(SourceLocation &EndLocation,
231 const CXXScopeSpec *SS) {
232 // Check whether we have a template-id that names a type.
233 if (Tok.is(tok::annot_template_id)) {
234 TemplateIdAnnotation *TemplateId
235 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
236 if (TemplateId->Kind == TNK_Class_template) {
237 if (AnnotateTemplateIdTokenAsType(SS))
238 return 0;
239
240 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
241 TypeTy *Type = Tok.getAnnotationValue();
242 EndLocation = Tok.getAnnotationEndLoc();
243 ConsumeToken();
244 return Type;
245 }
246
247 // Fall through to produce an error below.
248 }
249
Douglas Gregor42a552f2008-11-05 20:51:48 +0000250 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000251 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000252 return 0;
253 }
254
255 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000256 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
257 Tok.getLocation(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000258 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000259 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000260 return 0;
261 }
262
263 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000264 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000265 return Type;
266}
267
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000268/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
269/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
270/// until we reach the start of a definition or see a token that
271/// cannot start a definition.
272///
273/// class-specifier: [C++ class]
274/// class-head '{' member-specification[opt] '}'
275/// class-head '{' member-specification[opt] '}' attributes[opt]
276/// class-head:
277/// class-key identifier[opt] base-clause[opt]
278/// class-key nested-name-specifier identifier base-clause[opt]
279/// class-key nested-name-specifier[opt] simple-template-id
280/// base-clause[opt]
281/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
282/// [GNU] class-key attributes[opt] nested-name-specifier
283/// identifier base-clause[opt]
284/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
285/// simple-template-id base-clause[opt]
286/// class-key:
287/// 'class'
288/// 'struct'
289/// 'union'
290///
291/// elaborated-type-specifier: [C++ dcl.type.elab]
292/// class-key ::[opt] nested-name-specifier[opt] identifier
293/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
294/// simple-template-id
295///
296/// Note that the C++ class-specifier and elaborated-type-specifier,
297/// together, subsume the C99 struct-or-union-specifier:
298///
299/// struct-or-union-specifier: [C99 6.7.2.1]
300/// struct-or-union identifier[opt] '{' struct-contents '}'
301/// struct-or-union identifier
302/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
303/// '}' attributes[opt]
304/// [GNU] struct-or-union attributes[opt] identifier
305/// struct-or-union:
306/// 'struct'
307/// 'union'
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000308void Parser::ParseClassSpecifier(DeclSpec &DS,
309 TemplateParameterLists *TemplateParams) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000310 assert((Tok.is(tok::kw_class) ||
311 Tok.is(tok::kw_struct) ||
312 Tok.is(tok::kw_union)) &&
313 "Not a class specifier");
314 DeclSpec::TST TagType =
315 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
316 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
317 DeclSpec::TST_union;
318
319 SourceLocation StartLoc = ConsumeToken();
320
321 AttributeList *Attr = 0;
322 // If attributes exist after tag, parse them.
323 if (Tok.is(tok::kw___attribute))
324 Attr = ParseAttributes();
325
Steve Narofff59e17e2008-12-24 20:59:21 +0000326 // If declspecs exist after tag, parse them.
327 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
328 FuzzyParseMicrosoftDeclSpec();
329
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000330 // Parse the (optional) nested-name-specifier.
331 CXXScopeSpec SS;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000332 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
333 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000334 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000335
336 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000337 IdentifierInfo *Name = 0;
338 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000339 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000340 if (Tok.is(tok::identifier)) {
341 Name = Tok.getIdentifierInfo();
342 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000343 } else if (Tok.is(tok::annot_template_id)) {
344 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
345 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000346
Douglas Gregor39a8de12009-02-25 19:37:18 +0000347 if (TemplateId->Kind != TNK_Class_template) {
348 // The template-name in the simple-template-id refers to
349 // something other than a class template. Give an appropriate
350 // error message and skip to the ';'.
351 SourceRange Range(NameLoc);
352 if (SS.isNotEmpty())
353 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000354
Douglas Gregor39a8de12009-02-25 19:37:18 +0000355 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
356 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000357
Douglas Gregor39a8de12009-02-25 19:37:18 +0000358 DS.SetTypeSpecError();
359 SkipUntil(tok::semi, false, true);
360 TemplateId->Destroy();
361 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000362 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000363 }
364
365 // There are three options here. If we have 'struct foo;', then
366 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000367 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000368 // something like 'struct foo xyz', a reference.
369 Action::TagKind TK;
370 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
371 TK = Action::TK_Definition;
372 else if (Tok.is(tok::semi))
373 TK = Action::TK_Declaration;
374 else
375 TK = Action::TK_Reference;
376
Douglas Gregor39a8de12009-02-25 19:37:18 +0000377 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000378 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000379 Diag(StartLoc, diag::err_anon_type_definition)
380 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000381
382 // Skip the rest of this declarator, up until the comma or semicolon.
383 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000384
385 if (TemplateId)
386 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000387 return;
388 }
389
Douglas Gregorddc29e12009-02-06 22:42:48 +0000390 // Create the tag portion of the class or class template.
391 DeclTy *TagOrTempDecl;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000392 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000393 // Explicit specialization or class template partial
394 // specialization. Let semantic analysis decide.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000395 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
396 TemplateId->getTemplateArgs(),
397 TemplateId->getTemplateArgIsType(),
398 TemplateId->NumArgs);
Douglas Gregorcc636682009-02-17 23:15:12 +0000399 TagOrTempDecl
400 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000401 StartLoc, SS,
402 TemplateId->Template,
403 TemplateId->TemplateNameLoc,
404 TemplateId->LAngleLoc,
405 TemplateArgsPtr,
406 TemplateId->getTemplateArgLocations(),
407 TemplateId->RAngleLoc,
408 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000409 Action::MultiTemplateParamsArg(Actions,
410 TemplateParams? &(*TemplateParams)[0] : 0,
411 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor39a8de12009-02-25 19:37:18 +0000412 TemplateId->Destroy();
413 } else if (TemplateParams && TK != Action::TK_Reference)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000414 TagOrTempDecl = Actions.ActOnClassTemplate(CurScope, TagType, TK, StartLoc,
415 SS, Name, NameLoc, Attr,
416 Action::MultiTemplateParamsArg(Actions,
417 &(*TemplateParams)[0],
418 TemplateParams->size()));
419 else
420 TagOrTempDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
421 NameLoc, Attr);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000422
423 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000424 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000425 ParseBaseClause(TagOrTempDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000426
427 // If there is a body, parse it and inform the actions module.
428 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000429 if (getLang().CPlusPlus)
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000430 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempDecl);
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000431 else
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000432 ParseStructUnionBody(StartLoc, TagType, TagOrTempDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000433 else if (TK == Action::TK_Definition) {
434 // FIXME: Complain that we have a base-specifier list but no
435 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000436 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000437 }
438
439 const char *PrevSpec = 0;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000440 if (!TagOrTempDecl)
441 DS.SetTypeSpecError();
442 else if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagOrTempDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000443 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000444}
445
446/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
447///
448/// base-clause : [C++ class.derived]
449/// ':' base-specifier-list
450/// base-specifier-list:
451/// base-specifier '...'[opt]
452/// base-specifier-list ',' base-specifier '...'[opt]
453void Parser::ParseBaseClause(DeclTy *ClassDecl)
454{
455 assert(Tok.is(tok::colon) && "Not a base clause");
456 ConsumeToken();
457
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000458 // Build up an array of parsed base specifiers.
459 llvm::SmallVector<BaseTy *, 8> BaseInfo;
460
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000461 while (true) {
462 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000463 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000464 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000465 // Skip the rest of this base specifier, up until the comma or
466 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000467 SkipUntil(tok::comma, tok::l_brace, true, true);
468 } else {
469 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000470 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000471 }
472
473 // If the next token is a comma, consume it and keep reading
474 // base-specifiers.
475 if (Tok.isNot(tok::comma)) break;
476
477 // Consume the comma.
478 ConsumeToken();
479 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000480
481 // Attach the base specifiers
482 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000483}
484
485/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
486/// one entry in the base class list of a class specifier, for example:
487/// class foo : public bar, virtual private baz {
488/// 'public bar' and 'virtual private baz' are each base-specifiers.
489///
490/// base-specifier: [C++ class.derived]
491/// ::[opt] nested-name-specifier[opt] class-name
492/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
493/// class-name
494/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
495/// class-name
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000496Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000497{
498 bool IsVirtual = false;
499 SourceLocation StartLoc = Tok.getLocation();
500
501 // Parse the 'virtual' keyword.
502 if (Tok.is(tok::kw_virtual)) {
503 ConsumeToken();
504 IsVirtual = true;
505 }
506
507 // Parse an (optional) access specifier.
508 AccessSpecifier Access = getAccessSpecifierIfPresent();
509 if (Access)
510 ConsumeToken();
511
512 // Parse the 'virtual' keyword (again!), in case it came after the
513 // access specifier.
514 if (Tok.is(tok::kw_virtual)) {
515 SourceLocation VirtualLoc = ConsumeToken();
516 if (IsVirtual) {
517 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000518 Diag(VirtualLoc, diag::err_dup_virtual)
519 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000520 }
521
522 IsVirtual = true;
523 }
524
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000525 // Parse optional '::' and optional nested-name-specifier.
526 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000527 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000528
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000529 // The location of the base class itself.
530 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000531
532 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000533 SourceLocation EndLocation;
534 TypeTy *BaseType = ParseClassName(EndLocation, &SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000535 if (!BaseType)
536 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000537
538 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000539 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000540
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000541 // Notify semantic analysis that we have parsed a complete
542 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000543 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
544 BaseType, BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000545}
546
547/// getAccessSpecifierIfPresent - Determine whether the next token is
548/// a C++ access-specifier.
549///
550/// access-specifier: [C++ class.derived]
551/// 'private'
552/// 'protected'
553/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000554AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000555{
556 switch (Tok.getKind()) {
557 default: return AS_none;
558 case tok::kw_private: return AS_private;
559 case tok::kw_protected: return AS_protected;
560 case tok::kw_public: return AS_public;
561 }
562}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000563
564/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
565///
566/// member-declaration:
567/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
568/// function-definition ';'[opt]
569/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
570/// using-declaration [TODO]
571/// [C++0x] static_assert-declaration [TODO]
572/// template-declaration [TODO]
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000573/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000574///
575/// member-declarator-list:
576/// member-declarator
577/// member-declarator-list ',' member-declarator
578///
579/// member-declarator:
580/// declarator pure-specifier[opt]
581/// declarator constant-initializer[opt]
582/// identifier[opt] ':' constant-expression
583///
584/// pure-specifier: [TODO]
585/// '= 0'
586///
587/// constant-initializer:
588/// '=' constant-expression
589///
590Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000591 // Handle: member-declaration ::= '__extension__' member-declaration
592 if (Tok.is(tok::kw___extension__)) {
593 // __extension__ silences extension warnings in the subexpression.
594 ExtensionRAIIObject O(Diags); // Use RAII to do this.
595 ConsumeToken();
596 return ParseCXXClassMemberDeclaration(AS);
597 }
598
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000599 SourceLocation DSStart = Tok.getLocation();
600 // decl-specifier-seq:
601 // Parse the common declaration-specifiers piece.
602 DeclSpec DS;
603 ParseDeclarationSpecifiers(DS);
604
605 if (Tok.is(tok::semi)) {
606 ConsumeToken();
607 // C++ 9.2p7: The member-declarator-list can be omitted only after a
608 // class-specifier or an enum-specifier or in a friend declaration.
609 // FIXME: Friend declarations.
610 switch (DS.getTypeSpecType()) {
611 case DeclSpec::TST_struct:
612 case DeclSpec::TST_union:
613 case DeclSpec::TST_class:
614 case DeclSpec::TST_enum:
615 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
616 default:
617 Diag(DSStart, diag::err_no_declarators);
618 return 0;
619 }
620 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000621
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000622 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000623
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000624 if (Tok.isNot(tok::colon)) {
625 // Parse the first declarator.
626 ParseDeclarator(DeclaratorInfo);
627 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000628 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000629 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000630 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000631 if (Tok.is(tok::semi))
632 ConsumeToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000633 return 0;
634 }
635
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000636 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000637 if (Tok.is(tok::l_brace)
638 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000639 if (!DeclaratorInfo.isFunctionDeclarator()) {
640 Diag(Tok, diag::err_func_def_no_params);
641 ConsumeBrace();
642 SkipUntil(tok::r_brace, true);
643 return 0;
644 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000645
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000646 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
647 Diag(Tok, diag::err_function_declared_typedef);
648 // This recovery skips the entire function body. It would be nice
649 // to simply call ParseCXXInlineMethodDef() below, however Sema
650 // assumes the declarator represents a function, not a typedef.
651 ConsumeBrace();
652 SkipUntil(tok::r_brace, true);
653 return 0;
654 }
655
656 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
657 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000658 }
659
660 // member-declarator-list:
661 // member-declarator
662 // member-declarator-list ',' member-declarator
663
664 DeclTy *LastDeclInGroup = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000665 OwningExprResult BitfieldSize(Actions);
666 OwningExprResult Init(Actions);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000667
668 while (1) {
669
670 // member-declarator:
671 // declarator pure-specifier[opt]
672 // declarator constant-initializer[opt]
673 // identifier[opt] ':' constant-expression
674
675 if (Tok.is(tok::colon)) {
676 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000677 BitfieldSize = ParseConstantExpression();
678 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000679 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000680 }
681
682 // pure-specifier:
683 // '= 0'
684 //
685 // constant-initializer:
686 // '=' constant-expression
687
688 if (Tok.is(tok::equal)) {
689 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000690 Init = ParseInitializer();
691 if (Init.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000692 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000693 }
694
695 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000696 if (Tok.is(tok::kw___attribute)) {
697 SourceLocation Loc;
698 AttributeList *AttrList = ParseAttributes(&Loc);
699 DeclaratorInfo.AddAttributes(AttrList, Loc);
700 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000701
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000702 // NOTE: If Sema is the Action module and declarator is an instance field,
703 // this call will *not* return the created decl; LastDeclInGroup will be
704 // returned instead.
705 // See Sema::ActOnCXXMemberDeclarator for details.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000706 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
707 DeclaratorInfo,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000708 BitfieldSize.release(),
709 Init.release(),
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000710 LastDeclInGroup);
711
Douglas Gregor72b505b2008-12-16 21:30:33 +0000712 if (DeclaratorInfo.isFunctionDeclarator() &&
713 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
714 != DeclSpec::SCS_typedef) {
715 // We just declared a member function. If this member function
716 // has any default arguments, we'll need to parse them later.
717 LateParsedMethodDeclaration *LateMethod = 0;
718 DeclaratorChunk::FunctionTypeInfo &FTI
719 = DeclaratorInfo.getTypeObject(0).Fun;
720 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
721 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
722 if (!LateMethod) {
723 // Push this method onto the stack of late-parsed method
724 // declarations.
725 getCurTopClassStack().MethodDecls.push_back(
726 LateParsedMethodDeclaration(LastDeclInGroup));
727 LateMethod = &getCurTopClassStack().MethodDecls.back();
728
729 // Add all of the parameters prior to this one (they don't
730 // have default arguments).
731 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
732 for (unsigned I = 0; I < ParamIdx; ++I)
733 LateMethod->DefaultArgs.push_back(
734 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
735 }
736
737 // Add this parameter to the list of parameters (it or may
738 // not have a default argument).
739 LateMethod->DefaultArgs.push_back(
740 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
741 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
742 }
743 }
744 }
745
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000746 // If we don't have a comma, it is either the end of the list (a ';')
747 // or an error, bail out.
748 if (Tok.isNot(tok::comma))
749 break;
750
751 // Consume the comma.
752 ConsumeToken();
753
754 // Parse the next declarator.
755 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000756 BitfieldSize = 0;
757 Init = 0;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000758
759 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000760 if (Tok.is(tok::kw___attribute)) {
761 SourceLocation Loc;
762 AttributeList *AttrList = ParseAttributes(&Loc);
763 DeclaratorInfo.AddAttributes(AttrList, Loc);
764 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000765
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000766 if (Tok.isNot(tok::colon))
767 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000768 }
769
770 if (Tok.is(tok::semi)) {
771 ConsumeToken();
772 // Reverse the chain list.
773 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
774 }
775
776 Diag(Tok, diag::err_expected_semi_decl_list);
777 // Skip to end of block or statement
778 SkipUntil(tok::r_brace, true, true);
779 if (Tok.is(tok::semi))
780 ConsumeToken();
781 return 0;
782}
783
784/// ParseCXXMemberSpecification - Parse the class definition.
785///
786/// member-specification:
787/// member-declaration member-specification[opt]
788/// access-specifier ':' member-specification[opt]
789///
790void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
791 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000792 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000793 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000794 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000795
796 SourceLocation LBraceLoc = ConsumeBrace();
797
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000798 if (!CurScope->isClassScope() && // Not about to define a nested class.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000799 CurScope->isInCXXInlineMethodScope()) {
800 // We will define a local class of an inline method.
801 // Push a new LexedMethodsForTopClass for its inline methods.
802 PushTopClassStack();
803 }
804
805 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000806 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000807
Douglas Gregorddc29e12009-02-06 22:42:48 +0000808 if (TagDecl)
809 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
810 else {
811 SkipUntil(tok::r_brace, false, false);
812 return;
813 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000814
815 // C++ 11p3: Members of a class defined with the keyword class are private
816 // by default. Members of a class defined with the keywords struct or union
817 // are public by default.
818 AccessSpecifier CurAS;
819 if (TagType == DeclSpec::TST_class)
820 CurAS = AS_private;
821 else
822 CurAS = AS_public;
823
824 // While we still have something to read, read the member-declarations.
825 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
826 // Each iteration of this loop reads one member-declaration.
827
828 // Check for extraneous top-level semicolon.
829 if (Tok.is(tok::semi)) {
830 Diag(Tok, diag::ext_extra_struct_semi);
831 ConsumeToken();
832 continue;
833 }
834
835 AccessSpecifier AS = getAccessSpecifierIfPresent();
836 if (AS != AS_none) {
837 // Current token is a C++ access specifier.
838 CurAS = AS;
839 ConsumeToken();
840 ExpectAndConsume(tok::colon, diag::err_expected_colon);
841 continue;
842 }
843
844 // Parse all the comma separated declarators.
845 ParseCXXClassMemberDeclaration(CurAS);
846 }
847
848 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
849
850 AttributeList *AttrList = 0;
851 // If attributes exist after class contents, parse them.
852 if (Tok.is(tok::kw___attribute))
853 AttrList = ParseAttributes(); // FIXME: where should I put them?
854
855 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
856 LBraceLoc, RBraceLoc);
857
858 // C++ 9.2p2: Within the class member-specification, the class is regarded as
859 // complete within function bodies, default arguments,
860 // exception-specifications, and constructor ctor-initializers (including
861 // such things in nested classes).
862 //
Douglas Gregor72b505b2008-12-16 21:30:33 +0000863 // FIXME: Only function bodies and constructor ctor-initializers are
864 // parsed correctly, fix the rest.
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000865 if (!CurScope->getParent()->isClassScope()) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000866 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +0000867 // are complete and we can parse the delayed portions of method
868 // declarations and the lexed inline method definitions.
869 ParseLexedMethodDeclarations();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000870 ParseLexedMethodDefs();
871
872 // For a local class of inline method, pop the LexedMethodsForTopClass that
873 // was previously pushed.
874
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000875 assert((CurScope->isInCXXInlineMethodScope() ||
876 TopClassStacks.size() == 1) &&
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000877 "MethodLexers not getting popped properly!");
878 if (CurScope->isInCXXInlineMethodScope())
879 PopTopClassStack();
880 }
881
882 // Leave the class scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000883 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000884
Douglas Gregor72de6672009-01-08 20:45:30 +0000885 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000886}
Douglas Gregor7ad83902008-11-05 04:29:56 +0000887
888/// ParseConstructorInitializer - Parse a C++ constructor initializer,
889/// which explicitly initializes the members or base classes of a
890/// class (C++ [class.base.init]). For example, the three initializers
891/// after the ':' in the Derived constructor below:
892///
893/// @code
894/// class Base { };
895/// class Derived : Base {
896/// int x;
897/// float f;
898/// public:
899/// Derived(float f) : Base(), x(17), f(f) { }
900/// };
901/// @endcode
902///
903/// [C++] ctor-initializer:
904/// ':' mem-initializer-list
905///
906/// [C++] mem-initializer-list:
907/// mem-initializer
908/// mem-initializer , mem-initializer-list
909void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
910 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
911
912 SourceLocation ColonLoc = ConsumeToken();
913
914 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
915
916 do {
917 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000918 if (!MemInit.isInvalid())
919 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000920
921 if (Tok.is(tok::comma))
922 ConsumeToken();
923 else if (Tok.is(tok::l_brace))
924 break;
925 else {
926 // Skip over garbage, until we get to '{'. Don't eat the '{'.
927 SkipUntil(tok::l_brace, true, true);
928 break;
929 }
930 } while (true);
931
932 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
933 &MemInitializers[0], MemInitializers.size());
934}
935
936/// ParseMemInitializer - Parse a C++ member initializer, which is
937/// part of a constructor initializer that explicitly initializes one
938/// member or base class (C++ [class.base.init]). See
939/// ParseConstructorInitializer for an example.
940///
941/// [C++] mem-initializer:
942/// mem-initializer-id '(' expression-list[opt] ')'
943///
944/// [C++] mem-initializer-id:
945/// '::'[opt] nested-name-specifier[opt] class-name
946/// identifier
947Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
948 // FIXME: parse '::'[opt] nested-name-specifier[opt]
949
950 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000951 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000952 return true;
953 }
954
955 // Get the identifier. This may be a member name or a class name,
956 // but we'll let the semantic analysis determine which it is.
957 IdentifierInfo *II = Tok.getIdentifierInfo();
958 SourceLocation IdLoc = ConsumeToken();
959
960 // Parse the '('.
961 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000962 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000963 return true;
964 }
965 SourceLocation LParenLoc = ConsumeParen();
966
967 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000968 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000969 CommaLocsTy CommaLocs;
970 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
971 SkipUntil(tok::r_paren);
972 return true;
973 }
974
975 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
976
Sebastian Redla55e52c2008-11-25 22:21:31 +0000977 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
978 LParenLoc, ArgExprs.take(),
979 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000980}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000981
982/// ParseExceptionSpecification - Parse a C++ exception-specification
983/// (C++ [except.spec]).
984///
Douglas Gregora4745612008-12-01 18:00:20 +0000985/// exception-specification:
986/// 'throw' '(' type-id-list [opt] ')'
987/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000988///
Douglas Gregora4745612008-12-01 18:00:20 +0000989/// type-id-list:
990/// type-id
991/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000992///
Sebastian Redlab197ba2009-02-09 18:23:29 +0000993bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000994 assert(Tok.is(tok::kw_throw) && "expected throw");
995
996 SourceLocation ThrowLoc = ConsumeToken();
997
998 if (!Tok.is(tok::l_paren)) {
999 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1000 }
1001 SourceLocation LParenLoc = ConsumeParen();
1002
Douglas Gregora4745612008-12-01 18:00:20 +00001003 // Parse throw(...), a Microsoft extension that means "this function
1004 // can throw anything".
1005 if (Tok.is(tok::ellipsis)) {
1006 SourceLocation EllipsisLoc = ConsumeToken();
1007 if (!getLang().Microsoft)
1008 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001009 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001010 return false;
1011 }
1012
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001013 // Parse the sequence of type-ids.
1014 while (Tok.isNot(tok::r_paren)) {
1015 ParseTypeName();
1016 if (Tok.is(tok::comma))
1017 ConsumeToken();
1018 else
1019 break;
1020 }
1021
Sebastian Redlab197ba2009-02-09 18:23:29 +00001022 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001023 return false;
1024}