ParseDeclCXX.cpp revision 5b7f0c8f470f0b23ee95e467b5951e2bed733be1
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===// 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The LLVM Compiler Infrastructure 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// License. See LICENSE.TXT for details. 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===// 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file implements the C++ Declaration portions of the Parser interfaces. 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===// 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Parse/Parser.h" 15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "clang/Basic/Diagnostic.h" 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Parse/DeclSpec.h" 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Parse/Scope.h" 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using namespace clang; 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// ParseNamespace - We know that the current token is a namespace keyword. This 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// may either be a top level namespace or a block-level namespace alias. 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// namespace-definition: [C++ 7.3: basic.namespace] 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// named-namespace-definition 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// unnamed-namespace-definition 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// unnamed-namespace-definition: 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 'namespace' attributes[opt] '{' namespace-body '}' 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// named-namespace-definition: 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// original-namespace-definition 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// extension-namespace-definition 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// original-namespace-definition: 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 'namespace' identifier attributes[opt] '{' namespace-body '}' 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// extension-namespace-definition: 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 'namespace' original-namespace-name '{' namespace-body '}' 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// namespace-alias-definition: [C++ 7.3.2: namespace.alias] 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 'namespace' identifier '=' qualified-namespace-specifier ';' 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)Parser::DeclTy *Parser::ParseNamespace(unsigned Context) { 44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); 45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. 46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) SourceLocation IdentLoc; 48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) IdentifierInfo *Ident = 0; 49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (Tok.is(tok::identifier)) { 51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) Ident = Tok.getIdentifierInfo(); 52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) IdentLoc = ConsumeToken(); // eat the identifier. 53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 55a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Read label attributes, if present. 56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DeclTy *AttrList = 0; 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (Tok.is(tok::kw___attribute)) 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // FIXME: save these somewhere. 59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) AttrList = ParseAttributes(); 60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (Tok.is(tok::equal)) { 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // FIXME: Verify no attributes were present. 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // FIXME: parse this. 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } else if (Tok.is(tok::l_brace)) { 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) SourceLocation LBrace = ConsumeBrace(); 67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Enter a scope for the namespace. 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EnterScope(Scope::DeclScope); 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DeclTy *NamespcDecl = 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace); 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ParseExternalDeclaration(); 76 77 // Leave the namespace scope. 78 ExitScope(); 79 80 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); 81 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace); 82 83 return NamespcDecl; 84 85 } else { 86 unsigned D = Ident ? diag::err_expected_lbrace : 87 diag::err_expected_ident_lbrace; 88 Diag(Tok.getLocation(), D); 89 } 90 91 return 0; 92} 93 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) { 102 assert(Tok.is(tok::string_literal) && "Not a stringliteral!"); 103 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)) { 114 D = ParseDeclaration(Context); 115 } 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} 130 131/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or 132/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which 133/// until we reach the start of a definition or see a token that 134/// cannot start a definition. 135/// 136/// class-specifier: [C++ class] 137/// class-head '{' member-specification[opt] '}' 138/// class-head '{' member-specification[opt] '}' attributes[opt] 139/// class-head: 140/// class-key identifier[opt] base-clause[opt] 141/// class-key nested-name-specifier identifier base-clause[opt] 142/// class-key nested-name-specifier[opt] simple-template-id 143/// base-clause[opt] 144/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] 145/// [GNU] class-key attributes[opt] nested-name-specifier 146/// identifier base-clause[opt] 147/// [GNU] class-key attributes[opt] nested-name-specifier[opt] 148/// simple-template-id base-clause[opt] 149/// class-key: 150/// 'class' 151/// 'struct' 152/// 'union' 153/// 154/// elaborated-type-specifier: [C++ dcl.type.elab] 155/// class-key ::[opt] nested-name-specifier[opt] identifier 156/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] 157/// simple-template-id 158/// 159/// Note that the C++ class-specifier and elaborated-type-specifier, 160/// together, subsume the C99 struct-or-union-specifier: 161/// 162/// struct-or-union-specifier: [C99 6.7.2.1] 163/// struct-or-union identifier[opt] '{' struct-contents '}' 164/// struct-or-union identifier 165/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents 166/// '}' attributes[opt] 167/// [GNU] struct-or-union attributes[opt] identifier 168/// struct-or-union: 169/// 'struct' 170/// 'union' 171void Parser::ParseClassSpecifier(DeclSpec &DS) { 172 assert((Tok.is(tok::kw_class) || 173 Tok.is(tok::kw_struct) || 174 Tok.is(tok::kw_union)) && 175 "Not a class specifier"); 176 DeclSpec::TST TagType = 177 Tok.is(tok::kw_class) ? DeclSpec::TST_class : 178 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct : 179 DeclSpec::TST_union; 180 181 SourceLocation StartLoc = ConsumeToken(); 182 183 AttributeList *Attr = 0; 184 // If attributes exist after tag, parse them. 185 if (Tok.is(tok::kw___attribute)) 186 Attr = ParseAttributes(); 187 188 // FIXME: Parse the (optional) nested-name-specifier. 189 190 // Parse the (optional) class name. 191 // FIXME: Alternatively, parse a simple-template-id. 192 IdentifierInfo *Name = 0; 193 SourceLocation NameLoc; 194 if (Tok.is(tok::identifier)) { 195 Name = Tok.getIdentifierInfo(); 196 NameLoc = ConsumeToken(); 197 } 198 199 // There are three options here. If we have 'struct foo;', then 200 // this is a forward declaration. If we have 'struct foo {...' or 201 // 'struct fo :...' then this is a definition. Otherwise we have 202 // something like 'struct foo xyz', a reference. 203 Action::TagKind TK; 204 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) 205 TK = Action::TK_Definition; 206 else if (Tok.is(tok::semi)) 207 TK = Action::TK_Declaration; 208 else 209 TK = Action::TK_Reference; 210 211 if (!Name && TK != Action::TK_Definition) { 212 // We have a declaration or reference to an anonymous class. 213 Diag(StartLoc, diag::err_anon_type_definition, 214 DeclSpec::getSpecifierName(TagType)); 215 216 // Skip the rest of this declarator, up until the comma or semicolon. 217 SkipUntil(tok::comma, true); 218 return; 219 } 220 221 // Parse the tag portion of this. 222 DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, 223 NameLoc, Attr); 224 225 // Parse the optional base clause (C++ only). 226 if (getLang().CPlusPlus && Tok.is(tok::colon)) { 227 ParseBaseClause(TagDecl); 228 } 229 230 // If there is a body, parse it and inform the actions module. 231 if (Tok.is(tok::l_brace)) 232 if (getLang().CPlusPlus) 233 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl); 234 else 235 ParseStructUnionBody(StartLoc, TagType, TagDecl); 236 else if (TK == Action::TK_Definition) { 237 // FIXME: Complain that we have a base-specifier list but no 238 // definition. 239 Diag(Tok.getLocation(), diag::err_expected_lbrace); 240 } 241 242 const char *PrevSpec = 0; 243 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl)) 244 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); 245} 246 247/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. 248/// 249/// base-clause : [C++ class.derived] 250/// ':' base-specifier-list 251/// base-specifier-list: 252/// base-specifier '...'[opt] 253/// base-specifier-list ',' base-specifier '...'[opt] 254void Parser::ParseBaseClause(DeclTy *ClassDecl) 255{ 256 assert(Tok.is(tok::colon) && "Not a base clause"); 257 ConsumeToken(); 258 259 while (true) { 260 // Parse a base-specifier. 261 if (ParseBaseSpecifier(ClassDecl)) { 262 // Skip the rest of this base specifier, up until the comma or 263 // opening brace. 264 SkipUntil(tok::comma, tok::l_brace); 265 } 266 267 // If the next token is a comma, consume it and keep reading 268 // base-specifiers. 269 if (Tok.isNot(tok::comma)) break; 270 271 // Consume the comma. 272 ConsumeToken(); 273 } 274} 275 276/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is 277/// one entry in the base class list of a class specifier, for example: 278/// class foo : public bar, virtual private baz { 279/// 'public bar' and 'virtual private baz' are each base-specifiers. 280/// 281/// base-specifier: [C++ class.derived] 282/// ::[opt] nested-name-specifier[opt] class-name 283/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] 284/// class-name 285/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] 286/// class-name 287bool Parser::ParseBaseSpecifier(DeclTy *ClassDecl) 288{ 289 bool IsVirtual = false; 290 SourceLocation StartLoc = Tok.getLocation(); 291 292 // Parse the 'virtual' keyword. 293 if (Tok.is(tok::kw_virtual)) { 294 ConsumeToken(); 295 IsVirtual = true; 296 } 297 298 // Parse an (optional) access specifier. 299 AccessSpecifier Access = getAccessSpecifierIfPresent(); 300 if (Access) 301 ConsumeToken(); 302 303 // Parse the 'virtual' keyword (again!), in case it came after the 304 // access specifier. 305 if (Tok.is(tok::kw_virtual)) { 306 SourceLocation VirtualLoc = ConsumeToken(); 307 if (IsVirtual) { 308 // Complain about duplicate 'virtual' 309 Diag(VirtualLoc, diag::err_dup_virtual); 310 } 311 312 IsVirtual = true; 313 } 314 315 // FIXME: Parse optional '::' and optional nested-name-specifier. 316 317 // Parse the class-name. 318 // FIXME: Alternatively, parse a simple-template-id. 319 if (Tok.isNot(tok::identifier)) { 320 Diag(Tok.getLocation(), diag::err_expected_class_name); 321 return true; 322 } 323 324 // We have an identifier; check whether it is actually a type. 325 TypeTy *BaseType = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope); 326 if (!BaseType) { 327 Diag(Tok.getLocation(), diag::err_expected_class_name); 328 return true; 329 } 330 331 // The location of the base class itself. 332 SourceLocation BaseLoc = Tok.getLocation(); 333 334 // Find the complete source range for the base-specifier. 335 SourceRange Range(StartLoc, BaseLoc); 336 337 // Consume the identifier token (finally!). 338 ConsumeToken(); 339 340 // Notify semantic analysis that we have parsed a complete 341 // base-specifier. 342 Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, BaseType, 343 BaseLoc); 344 return false; 345} 346 347/// getAccessSpecifierIfPresent - Determine whether the next token is 348/// a C++ access-specifier. 349/// 350/// access-specifier: [C++ class.derived] 351/// 'private' 352/// 'protected' 353/// 'public' 354AccessSpecifier Parser::getAccessSpecifierIfPresent() const 355{ 356 switch (Tok.getKind()) { 357 default: return AS_none; 358 case tok::kw_private: return AS_private; 359 case tok::kw_protected: return AS_protected; 360 case tok::kw_public: return AS_public; 361 } 362} 363 364/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. 365/// 366/// member-declaration: 367/// decl-specifier-seq[opt] member-declarator-list[opt] ';' 368/// function-definition ';'[opt] 369/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] 370/// using-declaration [TODO] 371/// [C++0x] static_assert-declaration [TODO] 372/// template-declaration [TODO] 373/// 374/// member-declarator-list: 375/// member-declarator 376/// member-declarator-list ',' member-declarator 377/// 378/// member-declarator: 379/// declarator pure-specifier[opt] 380/// declarator constant-initializer[opt] 381/// identifier[opt] ':' constant-expression 382/// 383/// pure-specifier: [TODO] 384/// '= 0' 385/// 386/// constant-initializer: 387/// '=' constant-expression 388/// 389Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) { 390 SourceLocation DSStart = Tok.getLocation(); 391 // decl-specifier-seq: 392 // Parse the common declaration-specifiers piece. 393 DeclSpec DS; 394 ParseDeclarationSpecifiers(DS); 395 396 if (Tok.is(tok::semi)) { 397 ConsumeToken(); 398 // C++ 9.2p7: The member-declarator-list can be omitted only after a 399 // class-specifier or an enum-specifier or in a friend declaration. 400 // FIXME: Friend declarations. 401 switch (DS.getTypeSpecType()) { 402 case DeclSpec::TST_struct: 403 case DeclSpec::TST_union: 404 case DeclSpec::TST_class: 405 case DeclSpec::TST_enum: 406 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); 407 default: 408 Diag(DSStart, diag::err_no_declarators); 409 return 0; 410 } 411 } 412 413 Declarator DeclaratorInfo(DS, Declarator::MemberContext); 414 415 if (Tok.isNot(tok::colon)) { 416 // Parse the first declarator. 417 ParseDeclarator(DeclaratorInfo); 418 // Error parsing the declarator? 419 if (DeclaratorInfo.getIdentifier() == 0) { 420 // If so, skip until the semi-colon or a }. 421 SkipUntil(tok::r_brace, true); 422 if (Tok.is(tok::semi)) 423 ConsumeToken(); 424 return 0; 425 } 426 427 // function-definition: 428 if (Tok.is(tok::l_brace)) { 429 if (!DeclaratorInfo.isFunctionDeclarator()) { 430 Diag(Tok, diag::err_func_def_no_params); 431 ConsumeBrace(); 432 SkipUntil(tok::r_brace, true); 433 return 0; 434 } 435 436 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 437 Diag(Tok, diag::err_function_declared_typedef); 438 // This recovery skips the entire function body. It would be nice 439 // to simply call ParseCXXInlineMethodDef() below, however Sema 440 // assumes the declarator represents a function, not a typedef. 441 ConsumeBrace(); 442 SkipUntil(tok::r_brace, true); 443 return 0; 444 } 445 446 return ParseCXXInlineMethodDef(AS, DeclaratorInfo); 447 } 448 } 449 450 // member-declarator-list: 451 // member-declarator 452 // member-declarator-list ',' member-declarator 453 454 DeclTy *LastDeclInGroup = 0; 455 ExprTy *BitfieldSize = 0; 456 ExprTy *Init = 0; 457 458 while (1) { 459 460 // member-declarator: 461 // declarator pure-specifier[opt] 462 // declarator constant-initializer[opt] 463 // identifier[opt] ':' constant-expression 464 465 if (Tok.is(tok::colon)) { 466 ConsumeToken(); 467 ExprResult Res = ParseConstantExpression(); 468 if (Res.isInvalid) 469 SkipUntil(tok::comma, true, true); 470 else 471 BitfieldSize = Res.Val; 472 } 473 474 // pure-specifier: 475 // '= 0' 476 // 477 // constant-initializer: 478 // '=' constant-expression 479 480 if (Tok.is(tok::equal)) { 481 ConsumeToken(); 482 ExprResult Res = ParseInitializer(); 483 if (Res.isInvalid) 484 SkipUntil(tok::comma, true, true); 485 else 486 Init = Res.Val; 487 } 488 489 // If attributes exist after the declarator, parse them. 490 if (Tok.is(tok::kw___attribute)) 491 DeclaratorInfo.AddAttributes(ParseAttributes()); 492 493 // NOTE: If Sema is the Action module and declarator is an instance field, 494 // this call will *not* return the created decl; LastDeclInGroup will be 495 // returned instead. 496 // See Sema::ActOnCXXMemberDeclarator for details. 497 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS, 498 DeclaratorInfo, 499 BitfieldSize, Init, 500 LastDeclInGroup); 501 502 // If we don't have a comma, it is either the end of the list (a ';') 503 // or an error, bail out. 504 if (Tok.isNot(tok::comma)) 505 break; 506 507 // Consume the comma. 508 ConsumeToken(); 509 510 // Parse the next declarator. 511 DeclaratorInfo.clear(); 512 BitfieldSize = Init = 0; 513 514 // Attributes are only allowed on the second declarator. 515 if (Tok.is(tok::kw___attribute)) 516 DeclaratorInfo.AddAttributes(ParseAttributes()); 517 518 if (Tok.isNot(tok::colon)) 519 ParseDeclarator(DeclaratorInfo); 520 } 521 522 if (Tok.is(tok::semi)) { 523 ConsumeToken(); 524 // Reverse the chain list. 525 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); 526 } 527 528 Diag(Tok, diag::err_expected_semi_decl_list); 529 // Skip to end of block or statement 530 SkipUntil(tok::r_brace, true, true); 531 if (Tok.is(tok::semi)) 532 ConsumeToken(); 533 return 0; 534} 535 536/// ParseCXXMemberSpecification - Parse the class definition. 537/// 538/// member-specification: 539/// member-declaration member-specification[opt] 540/// access-specifier ':' member-specification[opt] 541/// 542void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, 543 unsigned TagType, DeclTy *TagDecl) { 544 assert(TagType == DeclSpec::TST_struct || 545 TagType == DeclSpec::TST_union || 546 TagType == DeclSpec::TST_class && "Invalid TagType!"); 547 548 SourceLocation LBraceLoc = ConsumeBrace(); 549 550 if (!CurScope->isCXXClassScope() && // Not about to define a nested class. 551 CurScope->isInCXXInlineMethodScope()) { 552 // We will define a local class of an inline method. 553 // Push a new LexedMethodsForTopClass for its inline methods. 554 PushTopClassStack(); 555 } 556 557 // Enter a scope for the class. 558 EnterScope(Scope::CXXClassScope|Scope::DeclScope); 559 560 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc); 561 562 // C++ 11p3: Members of a class defined with the keyword class are private 563 // by default. Members of a class defined with the keywords struct or union 564 // are public by default. 565 AccessSpecifier CurAS; 566 if (TagType == DeclSpec::TST_class) 567 CurAS = AS_private; 568 else 569 CurAS = AS_public; 570 571 // While we still have something to read, read the member-declarations. 572 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 573 // Each iteration of this loop reads one member-declaration. 574 575 // Check for extraneous top-level semicolon. 576 if (Tok.is(tok::semi)) { 577 Diag(Tok, diag::ext_extra_struct_semi); 578 ConsumeToken(); 579 continue; 580 } 581 582 AccessSpecifier AS = getAccessSpecifierIfPresent(); 583 if (AS != AS_none) { 584 // Current token is a C++ access specifier. 585 CurAS = AS; 586 ConsumeToken(); 587 ExpectAndConsume(tok::colon, diag::err_expected_colon); 588 continue; 589 } 590 591 // Parse all the comma separated declarators. 592 ParseCXXClassMemberDeclaration(CurAS); 593 } 594 595 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); 596 597 AttributeList *AttrList = 0; 598 // If attributes exist after class contents, parse them. 599 if (Tok.is(tok::kw___attribute)) 600 AttrList = ParseAttributes(); // FIXME: where should I put them? 601 602 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl, 603 LBraceLoc, RBraceLoc); 604 605 // C++ 9.2p2: Within the class member-specification, the class is regarded as 606 // complete within function bodies, default arguments, 607 // exception-specifications, and constructor ctor-initializers (including 608 // such things in nested classes). 609 // 610 // FIXME: Only function bodies are parsed correctly, fix the rest. 611 if (!CurScope->getParent()->isCXXClassScope()) { 612 // We are not inside a nested class. This class and its nested classes 613 // are complete and we can parse the lexed inline method definitions. 614 ParseLexedMethodDefs(); 615 616 // For a local class of inline method, pop the LexedMethodsForTopClass that 617 // was previously pushed. 618 619 assert(CurScope->isInCXXInlineMethodScope() || 620 TopClassStacks.size() == 1 && 621 "MethodLexers not getting popped properly!"); 622 if (CurScope->isInCXXInlineMethodScope()) 623 PopTopClassStack(); 624 } 625 626 // Leave the class scope. 627 ExitScope(); 628 629 Actions.ActOnFinishCXXClassDef(TagDecl); 630} 631