blob: 78539abaadbb50d97f7a168549ac9fb48b9e7a7a [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"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000015#include "clang/Basic/Diagnostic.h"
16#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 Lattner8f08cb72007-08-25 06:57:03 +000019using namespace clang;
20
21/// ParseNamespace - We know that the current token is a namespace keyword. This
22/// may either be a top level namespace or a block-level namespace alias.
23///
24/// namespace-definition: [C++ 7.3: basic.namespace]
25/// named-namespace-definition
26/// unnamed-namespace-definition
27///
28/// unnamed-namespace-definition:
29/// 'namespace' attributes[opt] '{' namespace-body '}'
30///
31/// named-namespace-definition:
32/// original-namespace-definition
33/// extension-namespace-definition
34///
35/// original-namespace-definition:
36/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
37///
38/// extension-namespace-definition:
39/// 'namespace' original-namespace-name '{' namespace-body '}'
40///
41/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
42/// 'namespace' identifier '=' qualified-namespace-specifier ';'
43///
44Parser::DeclTy *Parser::ParseNamespace(unsigned Context) {
Chris Lattner04d66662007-10-09 17:33:22 +000045 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000046 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
47
48 SourceLocation IdentLoc;
49 IdentifierInfo *Ident = 0;
50
Chris Lattner04d66662007-10-09 17:33:22 +000051 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000052 Ident = Tok.getIdentifierInfo();
53 IdentLoc = ConsumeToken(); // eat the identifier.
54 }
55
56 // Read label attributes, if present.
57 DeclTy *AttrList = 0;
Chris Lattner04d66662007-10-09 17:33:22 +000058 if (Tok.is(tok::kw___attribute))
Chris Lattner8f08cb72007-08-25 06:57:03 +000059 // FIXME: save these somewhere.
60 AttrList = ParseAttributes();
61
Chris Lattner04d66662007-10-09 17:33:22 +000062 if (Tok.is(tok::equal)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000063 // FIXME: Verify no attributes were present.
64 // FIXME: parse this.
Chris Lattner04d66662007-10-09 17:33:22 +000065 } else if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000066
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 SourceLocation LBrace = ConsumeBrace();
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000068
69 // Enter a scope for the namespace.
70 EnterScope(Scope::DeclScope);
71
72 DeclTy *NamespcDecl =
73 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
74
75 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
Chris Lattnerbae35112007-08-25 18:15:16 +000076 ParseExternalDeclaration();
Chris Lattner8f08cb72007-08-25 06:57:03 +000077
Argyrios Kyrtzidis8ba5d792008-05-01 21:44:34 +000078 // Leave the namespace scope.
79 ExitScope();
80
Chris Lattner8f08cb72007-08-25 06:57:03 +000081 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000082 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace);
83
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000084 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +000085
Chris Lattner8f08cb72007-08-25 06:57:03 +000086 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +000087 Diag(Tok, Ident ? diag::err_expected_lbrace :
88 diag::err_expected_ident_lbrace);
Chris Lattner8f08cb72007-08-25 06:57:03 +000089 }
90
91 return 0;
92}
Chris Lattnerc6fdc342008-01-12 07:05:38 +000093
94/// ParseLinkage - We know that the current token is a string_literal
95/// and just before that, that extern was seen.
96///
97/// linkage-specification: [C++ 7.5p2: dcl.link]
98/// 'extern' string-literal '{' declaration-seq[opt] '}'
99/// 'extern' string-literal declaration
100///
101Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000102 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000103 llvm::SmallVector<char, 8> LangBuffer;
104 // LangBuffer is guaranteed to be big enough.
105 LangBuffer.resize(Tok.getLength());
106 const char *LangBufPtr = &LangBuffer[0];
107 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
108
109 SourceLocation Loc = ConsumeStringToken();
110 DeclTy *D = 0;
111 SourceLocation LBrace, RBrace;
112
113 if (Tok.isNot(tok::l_brace)) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000114 D = ParseDeclarationOrFunctionDefinition();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000115 } else {
116 LBrace = ConsumeBrace();
117 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
118 // FIXME capture the decls.
119 D = ParseExternalDeclaration();
120 }
121
122 RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
123 }
124
125 if (!D)
126 return 0;
127
128 return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize, D);
129}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000130
Douglas Gregor42a552f2008-11-05 20:51:48 +0000131/// ParseClassName - Parse a C++ class-name, which names a class. Note
132/// that we only check that the result names a type; semantic analysis
133/// will need to verify that the type names a class. The result is
134/// either a type or NULL, dependending on whether a type name was
135/// found.
136///
137/// class-name: [C++ 9.1]
138/// identifier
139/// template-id [TODO]
140///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000141Parser::TypeTy *Parser::ParseClassName(const CXXScopeSpec *SS) {
Douglas Gregor42a552f2008-11-05 20:51:48 +0000142 // Parse the class-name.
143 // FIXME: Alternatively, parse a simple-template-id.
144 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000145 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000146 return 0;
147 }
148
149 // We have an identifier; check whether it is actually a type.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000150 TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000151 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000152 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000153 return 0;
154 }
155
156 // Consume the identifier.
157 ConsumeToken();
158
159 return Type;
160}
161
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000162/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
163/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
164/// until we reach the start of a definition or see a token that
165/// cannot start a definition.
166///
167/// class-specifier: [C++ class]
168/// class-head '{' member-specification[opt] '}'
169/// class-head '{' member-specification[opt] '}' attributes[opt]
170/// class-head:
171/// class-key identifier[opt] base-clause[opt]
172/// class-key nested-name-specifier identifier base-clause[opt]
173/// class-key nested-name-specifier[opt] simple-template-id
174/// base-clause[opt]
175/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
176/// [GNU] class-key attributes[opt] nested-name-specifier
177/// identifier base-clause[opt]
178/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
179/// simple-template-id base-clause[opt]
180/// class-key:
181/// 'class'
182/// 'struct'
183/// 'union'
184///
185/// elaborated-type-specifier: [C++ dcl.type.elab]
186/// class-key ::[opt] nested-name-specifier[opt] identifier
187/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
188/// simple-template-id
189///
190/// Note that the C++ class-specifier and elaborated-type-specifier,
191/// together, subsume the C99 struct-or-union-specifier:
192///
193/// struct-or-union-specifier: [C99 6.7.2.1]
194/// struct-or-union identifier[opt] '{' struct-contents '}'
195/// struct-or-union identifier
196/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
197/// '}' attributes[opt]
198/// [GNU] struct-or-union attributes[opt] identifier
199/// struct-or-union:
200/// 'struct'
201/// 'union'
202void Parser::ParseClassSpecifier(DeclSpec &DS) {
203 assert((Tok.is(tok::kw_class) ||
204 Tok.is(tok::kw_struct) ||
205 Tok.is(tok::kw_union)) &&
206 "Not a class specifier");
207 DeclSpec::TST TagType =
208 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
209 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
210 DeclSpec::TST_union;
211
212 SourceLocation StartLoc = ConsumeToken();
213
214 AttributeList *Attr = 0;
215 // If attributes exist after tag, parse them.
216 if (Tok.is(tok::kw___attribute))
217 Attr = ParseAttributes();
218
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000219 // Parse the (optional) nested-name-specifier.
220 CXXScopeSpec SS;
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000221 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000222 if (Tok.isNot(tok::identifier))
223 Diag(Tok, diag::err_expected_ident);
224 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000225
226 // Parse the (optional) class name.
227 // FIXME: Alternatively, parse a simple-template-id.
228 IdentifierInfo *Name = 0;
229 SourceLocation NameLoc;
230 if (Tok.is(tok::identifier)) {
231 Name = Tok.getIdentifierInfo();
232 NameLoc = ConsumeToken();
233 }
234
235 // There are three options here. If we have 'struct foo;', then
236 // this is a forward declaration. If we have 'struct foo {...' or
237 // 'struct fo :...' then this is a definition. Otherwise we have
238 // something like 'struct foo xyz', a reference.
239 Action::TagKind TK;
240 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
241 TK = Action::TK_Definition;
242 else if (Tok.is(tok::semi))
243 TK = Action::TK_Declaration;
244 else
245 TK = Action::TK_Reference;
246
247 if (!Name && TK != Action::TK_Definition) {
248 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000249 Diag(StartLoc, diag::err_anon_type_definition)
250 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000251
252 // Skip the rest of this declarator, up until the comma or semicolon.
253 SkipUntil(tok::comma, true);
254 return;
255 }
256
257 // Parse the tag portion of this.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000258 DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000259 NameLoc, Attr);
260
261 // Parse the optional base clause (C++ only).
262 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
263 ParseBaseClause(TagDecl);
264 }
265
266 // If there is a body, parse it and inform the actions module.
267 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000268 if (getLang().CPlusPlus)
269 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl);
270 else
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000271 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000272 else if (TK == Action::TK_Definition) {
273 // FIXME: Complain that we have a base-specifier list but no
274 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000275 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000276 }
277
278 const char *PrevSpec = 0;
279 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000280 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000281}
282
283/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
284///
285/// base-clause : [C++ class.derived]
286/// ':' base-specifier-list
287/// base-specifier-list:
288/// base-specifier '...'[opt]
289/// base-specifier-list ',' base-specifier '...'[opt]
290void Parser::ParseBaseClause(DeclTy *ClassDecl)
291{
292 assert(Tok.is(tok::colon) && "Not a base clause");
293 ConsumeToken();
294
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000295 // Build up an array of parsed base specifiers.
296 llvm::SmallVector<BaseTy *, 8> BaseInfo;
297
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000298 while (true) {
299 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000300 BaseResult Result = ParseBaseSpecifier(ClassDecl);
301 if (Result.isInvalid) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000302 // Skip the rest of this base specifier, up until the comma or
303 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000304 SkipUntil(tok::comma, tok::l_brace, true, true);
305 } else {
306 // Add this to our array of base specifiers.
307 BaseInfo.push_back(Result.Val);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000308 }
309
310 // If the next token is a comma, consume it and keep reading
311 // base-specifiers.
312 if (Tok.isNot(tok::comma)) break;
313
314 // Consume the comma.
315 ConsumeToken();
316 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000317
318 // Attach the base specifiers
319 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000320}
321
322/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
323/// one entry in the base class list of a class specifier, for example:
324/// class foo : public bar, virtual private baz {
325/// 'public bar' and 'virtual private baz' are each base-specifiers.
326///
327/// base-specifier: [C++ class.derived]
328/// ::[opt] nested-name-specifier[opt] class-name
329/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
330/// class-name
331/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
332/// class-name
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000333Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000334{
335 bool IsVirtual = false;
336 SourceLocation StartLoc = Tok.getLocation();
337
338 // Parse the 'virtual' keyword.
339 if (Tok.is(tok::kw_virtual)) {
340 ConsumeToken();
341 IsVirtual = true;
342 }
343
344 // Parse an (optional) access specifier.
345 AccessSpecifier Access = getAccessSpecifierIfPresent();
346 if (Access)
347 ConsumeToken();
348
349 // Parse the 'virtual' keyword (again!), in case it came after the
350 // access specifier.
351 if (Tok.is(tok::kw_virtual)) {
352 SourceLocation VirtualLoc = ConsumeToken();
353 if (IsVirtual) {
354 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000355 Diag(VirtualLoc, diag::err_dup_virtual)
356 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000357 }
358
359 IsVirtual = true;
360 }
361
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000362 // Parse optional '::' and optional nested-name-specifier.
363 CXXScopeSpec SS;
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000364 MaybeParseCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000365
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000366 // The location of the base class itself.
367 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000368
369 // Parse the class-name.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000370 TypeTy *BaseType = ParseClassName(&SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000371 if (!BaseType)
372 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000373
374 // Find the complete source range for the base-specifier.
375 SourceRange Range(StartLoc, BaseLoc);
376
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000377 // Notify semantic analysis that we have parsed a complete
378 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000379 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
380 BaseType, BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000381}
382
383/// getAccessSpecifierIfPresent - Determine whether the next token is
384/// a C++ access-specifier.
385///
386/// access-specifier: [C++ class.derived]
387/// 'private'
388/// 'protected'
389/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000390AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000391{
392 switch (Tok.getKind()) {
393 default: return AS_none;
394 case tok::kw_private: return AS_private;
395 case tok::kw_protected: return AS_protected;
396 case tok::kw_public: return AS_public;
397 }
398}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000399
400/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
401///
402/// member-declaration:
403/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
404/// function-definition ';'[opt]
405/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
406/// using-declaration [TODO]
407/// [C++0x] static_assert-declaration [TODO]
408/// template-declaration [TODO]
409///
410/// member-declarator-list:
411/// member-declarator
412/// member-declarator-list ',' member-declarator
413///
414/// member-declarator:
415/// declarator pure-specifier[opt]
416/// declarator constant-initializer[opt]
417/// identifier[opt] ':' constant-expression
418///
419/// pure-specifier: [TODO]
420/// '= 0'
421///
422/// constant-initializer:
423/// '=' constant-expression
424///
425Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
426 SourceLocation DSStart = Tok.getLocation();
427 // decl-specifier-seq:
428 // Parse the common declaration-specifiers piece.
429 DeclSpec DS;
430 ParseDeclarationSpecifiers(DS);
431
432 if (Tok.is(tok::semi)) {
433 ConsumeToken();
434 // C++ 9.2p7: The member-declarator-list can be omitted only after a
435 // class-specifier or an enum-specifier or in a friend declaration.
436 // FIXME: Friend declarations.
437 switch (DS.getTypeSpecType()) {
438 case DeclSpec::TST_struct:
439 case DeclSpec::TST_union:
440 case DeclSpec::TST_class:
441 case DeclSpec::TST_enum:
442 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
443 default:
444 Diag(DSStart, diag::err_no_declarators);
445 return 0;
446 }
447 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000448
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000449 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000450
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000451 if (Tok.isNot(tok::colon)) {
452 // Parse the first declarator.
453 ParseDeclarator(DeclaratorInfo);
454 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000455 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000456 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000457 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000458 if (Tok.is(tok::semi))
459 ConsumeToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000460 return 0;
461 }
462
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000463 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000464 if (Tok.is(tok::l_brace)
465 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000466 if (!DeclaratorInfo.isFunctionDeclarator()) {
467 Diag(Tok, diag::err_func_def_no_params);
468 ConsumeBrace();
469 SkipUntil(tok::r_brace, true);
470 return 0;
471 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000472
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000473 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
474 Diag(Tok, diag::err_function_declared_typedef);
475 // This recovery skips the entire function body. It would be nice
476 // to simply call ParseCXXInlineMethodDef() below, however Sema
477 // assumes the declarator represents a function, not a typedef.
478 ConsumeBrace();
479 SkipUntil(tok::r_brace, true);
480 return 0;
481 }
482
483 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
484 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000485 }
486
487 // member-declarator-list:
488 // member-declarator
489 // member-declarator-list ',' member-declarator
490
491 DeclTy *LastDeclInGroup = 0;
492 ExprTy *BitfieldSize = 0;
493 ExprTy *Init = 0;
494
495 while (1) {
496
497 // member-declarator:
498 // declarator pure-specifier[opt]
499 // declarator constant-initializer[opt]
500 // identifier[opt] ':' constant-expression
501
502 if (Tok.is(tok::colon)) {
503 ConsumeToken();
504 ExprResult Res = ParseConstantExpression();
505 if (Res.isInvalid)
506 SkipUntil(tok::comma, true, true);
507 else
508 BitfieldSize = Res.Val;
509 }
510
511 // pure-specifier:
512 // '= 0'
513 //
514 // constant-initializer:
515 // '=' constant-expression
516
517 if (Tok.is(tok::equal)) {
518 ConsumeToken();
519 ExprResult Res = ParseInitializer();
520 if (Res.isInvalid)
521 SkipUntil(tok::comma, true, true);
522 else
523 Init = Res.Val;
524 }
525
526 // If attributes exist after the declarator, parse them.
527 if (Tok.is(tok::kw___attribute))
528 DeclaratorInfo.AddAttributes(ParseAttributes());
529
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000530 // NOTE: If Sema is the Action module and declarator is an instance field,
531 // this call will *not* return the created decl; LastDeclInGroup will be
532 // returned instead.
533 // See Sema::ActOnCXXMemberDeclarator for details.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000534 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
535 DeclaratorInfo,
536 BitfieldSize, Init,
537 LastDeclInGroup);
538
539 // If we don't have a comma, it is either the end of the list (a ';')
540 // or an error, bail out.
541 if (Tok.isNot(tok::comma))
542 break;
543
544 // Consume the comma.
545 ConsumeToken();
546
547 // Parse the next declarator.
548 DeclaratorInfo.clear();
549 BitfieldSize = Init = 0;
550
551 // Attributes are only allowed on the second declarator.
552 if (Tok.is(tok::kw___attribute))
553 DeclaratorInfo.AddAttributes(ParseAttributes());
554
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000555 if (Tok.isNot(tok::colon))
556 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000557 }
558
559 if (Tok.is(tok::semi)) {
560 ConsumeToken();
561 // Reverse the chain list.
562 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
563 }
564
565 Diag(Tok, diag::err_expected_semi_decl_list);
566 // Skip to end of block or statement
567 SkipUntil(tok::r_brace, true, true);
568 if (Tok.is(tok::semi))
569 ConsumeToken();
570 return 0;
571}
572
573/// ParseCXXMemberSpecification - Parse the class definition.
574///
575/// member-specification:
576/// member-declaration member-specification[opt]
577/// access-specifier ':' member-specification[opt]
578///
579void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
580 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000581 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000582 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000583 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000584
585 SourceLocation LBraceLoc = ConsumeBrace();
586
587 if (!CurScope->isCXXClassScope() && // Not about to define a nested class.
588 CurScope->isInCXXInlineMethodScope()) {
589 // We will define a local class of an inline method.
590 // Push a new LexedMethodsForTopClass for its inline methods.
591 PushTopClassStack();
592 }
593
594 // Enter a scope for the class.
595 EnterScope(Scope::CXXClassScope|Scope::DeclScope);
596
597 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc);
598
599 // C++ 11p3: Members of a class defined with the keyword class are private
600 // by default. Members of a class defined with the keywords struct or union
601 // are public by default.
602 AccessSpecifier CurAS;
603 if (TagType == DeclSpec::TST_class)
604 CurAS = AS_private;
605 else
606 CurAS = AS_public;
607
608 // While we still have something to read, read the member-declarations.
609 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
610 // Each iteration of this loop reads one member-declaration.
611
612 // Check for extraneous top-level semicolon.
613 if (Tok.is(tok::semi)) {
614 Diag(Tok, diag::ext_extra_struct_semi);
615 ConsumeToken();
616 continue;
617 }
618
619 AccessSpecifier AS = getAccessSpecifierIfPresent();
620 if (AS != AS_none) {
621 // Current token is a C++ access specifier.
622 CurAS = AS;
623 ConsumeToken();
624 ExpectAndConsume(tok::colon, diag::err_expected_colon);
625 continue;
626 }
627
628 // Parse all the comma separated declarators.
629 ParseCXXClassMemberDeclaration(CurAS);
630 }
631
632 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
633
634 AttributeList *AttrList = 0;
635 // If attributes exist after class contents, parse them.
636 if (Tok.is(tok::kw___attribute))
637 AttrList = ParseAttributes(); // FIXME: where should I put them?
638
639 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
640 LBraceLoc, RBraceLoc);
641
642 // C++ 9.2p2: Within the class member-specification, the class is regarded as
643 // complete within function bodies, default arguments,
644 // exception-specifications, and constructor ctor-initializers (including
645 // such things in nested classes).
646 //
647 // FIXME: Only function bodies are parsed correctly, fix the rest.
648 if (!CurScope->getParent()->isCXXClassScope()) {
649 // We are not inside a nested class. This class and its nested classes
650 // are complete and we can parse the lexed inline method definitions.
651 ParseLexedMethodDefs();
652
653 // For a local class of inline method, pop the LexedMethodsForTopClass that
654 // was previously pushed.
655
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000656 assert((CurScope->isInCXXInlineMethodScope() ||
657 TopClassStacks.size() == 1) &&
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000658 "MethodLexers not getting popped properly!");
659 if (CurScope->isInCXXInlineMethodScope())
660 PopTopClassStack();
661 }
662
663 // Leave the class scope.
664 ExitScope();
665
Argyrios Kyrtzidis5b7f0c82008-08-09 00:39:29 +0000666 Actions.ActOnFinishCXXClassDef(TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000667}
Douglas Gregor7ad83902008-11-05 04:29:56 +0000668
669/// ParseConstructorInitializer - Parse a C++ constructor initializer,
670/// which explicitly initializes the members or base classes of a
671/// class (C++ [class.base.init]). For example, the three initializers
672/// after the ':' in the Derived constructor below:
673///
674/// @code
675/// class Base { };
676/// class Derived : Base {
677/// int x;
678/// float f;
679/// public:
680/// Derived(float f) : Base(), x(17), f(f) { }
681/// };
682/// @endcode
683///
684/// [C++] ctor-initializer:
685/// ':' mem-initializer-list
686///
687/// [C++] mem-initializer-list:
688/// mem-initializer
689/// mem-initializer , mem-initializer-list
690void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
691 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
692
693 SourceLocation ColonLoc = ConsumeToken();
694
695 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
696
697 do {
698 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
699 if (!MemInit.isInvalid)
700 MemInitializers.push_back(MemInit.Val);
701
702 if (Tok.is(tok::comma))
703 ConsumeToken();
704 else if (Tok.is(tok::l_brace))
705 break;
706 else {
707 // Skip over garbage, until we get to '{'. Don't eat the '{'.
708 SkipUntil(tok::l_brace, true, true);
709 break;
710 }
711 } while (true);
712
713 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
714 &MemInitializers[0], MemInitializers.size());
715}
716
717/// ParseMemInitializer - Parse a C++ member initializer, which is
718/// part of a constructor initializer that explicitly initializes one
719/// member or base class (C++ [class.base.init]). See
720/// ParseConstructorInitializer for an example.
721///
722/// [C++] mem-initializer:
723/// mem-initializer-id '(' expression-list[opt] ')'
724///
725/// [C++] mem-initializer-id:
726/// '::'[opt] nested-name-specifier[opt] class-name
727/// identifier
728Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
729 // FIXME: parse '::'[opt] nested-name-specifier[opt]
730
731 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000732 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000733 return true;
734 }
735
736 // Get the identifier. This may be a member name or a class name,
737 // but we'll let the semantic analysis determine which it is.
738 IdentifierInfo *II = Tok.getIdentifierInfo();
739 SourceLocation IdLoc = ConsumeToken();
740
741 // Parse the '('.
742 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000743 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000744 return true;
745 }
746 SourceLocation LParenLoc = ConsumeParen();
747
748 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000749 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000750 CommaLocsTy CommaLocs;
751 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
752 SkipUntil(tok::r_paren);
753 return true;
754 }
755
756 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
757
Sebastian Redla55e52c2008-11-25 22:21:31 +0000758 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
759 LParenLoc, ArgExprs.take(),
760 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000761}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000762
763/// ParseExceptionSpecification - Parse a C++ exception-specification
764/// (C++ [except.spec]).
765///
Douglas Gregora4745612008-12-01 18:00:20 +0000766/// exception-specification:
767/// 'throw' '(' type-id-list [opt] ')'
768/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000769///
Douglas Gregora4745612008-12-01 18:00:20 +0000770/// type-id-list:
771/// type-id
772/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000773///
774bool Parser::ParseExceptionSpecification() {
775 assert(Tok.is(tok::kw_throw) && "expected throw");
776
777 SourceLocation ThrowLoc = ConsumeToken();
778
779 if (!Tok.is(tok::l_paren)) {
780 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
781 }
782 SourceLocation LParenLoc = ConsumeParen();
783
Douglas Gregora4745612008-12-01 18:00:20 +0000784 // Parse throw(...), a Microsoft extension that means "this function
785 // can throw anything".
786 if (Tok.is(tok::ellipsis)) {
787 SourceLocation EllipsisLoc = ConsumeToken();
788 if (!getLang().Microsoft)
789 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
790 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
791 return false;
792 }
793
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000794 // Parse the sequence of type-ids.
795 while (Tok.isNot(tok::r_paren)) {
796 ParseTypeName();
797 if (Tok.is(tok::comma))
798 ConsumeToken();
799 else
800 break;
801 }
802
803 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
804 return false;
805}