ParseDeclCXX.cpp revision 45ab4b5f8961dadcef6545ed6956da5daf95c6cb
18f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===// 28f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner// 38f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner// The LLVM Compiler Infrastructure 48f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 78f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner// 88f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//===----------------------------------------------------------------------===// 98f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner// 108f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner// This file implements the C++ Declaration portions of the Parser interfaces. 118f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner// 128f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner//===----------------------------------------------------------------------===// 138f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner 140c6139d0c2497c4e8780340e0dc097de041f248eAnders Carlsson#include "clang/Basic/OperatorKinds.h" 151b7f89846f10681ce3cbe89ef5d60691d55bd918Douglas Gregor#include "clang/Parse/Parser.h" 16500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h" 1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h" 1819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/Scope.h" 1919510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h" 20f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h" 21d167ca0d26e43292b8b9e8d5300d92784ae0e27dChris Lattner#include "RAIIObjectsForParser.h" 228f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattnerusing namespace clang; 238f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner 248f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// ParseNamespace - We know that the current token is a namespace keyword. This 25d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl/// may either be a top level namespace or a block-level namespace alias. If 26d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl/// there was an inline keyword, it has already been parsed. 278f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// 288f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// namespace-definition: [C++ 7.3: basic.namespace] 298f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// named-namespace-definition 308f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// unnamed-namespace-definition 318f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// 328f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// unnamed-namespace-definition: 33d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}' 348f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// 358f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// named-namespace-definition: 368f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// original-namespace-definition 378f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// extension-namespace-definition 388f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// 398f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// original-namespace-definition: 40d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl/// 'inline'[opt] 'namespace' identifier attributes[opt] 41d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl/// '{' namespace-body '}' 428f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// 438f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// extension-namespace-definition: 44d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl/// 'inline'[opt] 'namespace' original-namespace-name 45d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl/// '{' namespace-body '}' 461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 478f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// namespace-alias-definition: [C++ 7.3.2: namespace.alias] 488f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// 'namespace' identifier '=' qualified-namespace-specifier ';' 498f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner/// 50d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseNamespace(unsigned Context, 51d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl SourceLocation &DeclEnd, 52d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl SourceLocation InlineLoc) { 5304d6666eee19bbf25bdfcb8e79eb8b00ace03f0cChris Lattner assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); 548f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. 551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5649f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor if (Tok.is(tok::code_completion)) { 5723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CodeCompleteNamespaceDecl(getCurScope()); 58dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor ConsumeCodeCompletionToken(); 5949f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor } 60193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 618f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner SourceLocation IdentLoc; 628f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner IdentifierInfo *Ident = 0; 636a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor 646a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor Token attrTok; 651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6604d6666eee19bbf25bdfcb8e79eb8b00ace03f0cChris Lattner if (Tok.is(tok::identifier)) { 678f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner Ident = Tok.getIdentifierInfo(); 688f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner IdentLoc = ConsumeToken(); // eat the identifier. 698f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner } 701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 718f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner // Read label attributes, if present. 728113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek AttributeList *AttrList = 0; 736a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor if (Tok.is(tok::kw___attribute)) { 746a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor attrTok = Tok; 756a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor 768f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner // FIXME: save these somewhere. 778113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek AttrList = ParseGNUAttributes(); 786a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor } 791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 806a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor if (Tok.is(tok::equal)) { 816a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor if (AttrList) 826a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor Diag(attrTok, diag::err_unexpected_namespace_attributes_alias); 83d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl if (InlineLoc.isValid()) 84d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl Diag(InlineLoc, diag::err_inline_namespace_alias) 85d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl << FixItHint::CreateRemoval(InlineLoc); 866a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor 8797144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); 886a588dd230c14a364d222d6057bbcf11afbd9ffdDouglas Gregor } 891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 905144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner if (Tok.isNot(tok::l_brace)) { 911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(Tok, Ident ? diag::err_expected_lbrace : 925144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner diag::err_expected_ident_lbrace); 93d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 945144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner } 951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 965144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner SourceLocation LBrace = ConsumeBrace(); 972d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis 9823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() || 9923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() || 10023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor getCurScope()->getFnParent()) { 10195f1b15ce1b281c8517d77792abe540753fbcc12Douglas Gregor Diag(LBrace, diag::err_namespace_nonnamespace_scope); 10295f1b15ce1b281c8517d77792abe540753fbcc12Douglas Gregor SkipUntil(tok::r_brace, false); 103d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 10495f1b15ce1b281c8517d77792abe540753fbcc12Douglas Gregor } 10595f1b15ce1b281c8517d77792abe540753fbcc12Douglas Gregor 10688e64ca96d6c00c6f3bd43772cd325bede795d2aSebastian Redl // If we're still good, complain about inline namespaces in non-C++0x now. 10788e64ca96d6c00c6f3bd43772cd325bede795d2aSebastian Redl if (!getLang().CPlusPlus0x && InlineLoc.isValid()) 10888e64ca96d6c00c6f3bd43772cd325bede795d2aSebastian Redl Diag(InlineLoc, diag::ext_inline_namespace); 10988e64ca96d6c00c6f3bd43772cd325bede795d2aSebastian Redl 1105144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner // Enter a scope for the namespace. 1115144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner ParseScope NamespaceScope(this, Scope::DeclScope); 1122d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis 113d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *NamespcDecl = 114d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, IdentLoc, Ident, 1158113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek LBrace, AttrList); 1162d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis 117f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc, 118f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall "parsing namespace"); 1191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 120bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 121bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt CXX0XAttributeList Attr; 122bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 123bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Attr = ParseCXX0XAttributes(); 124334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet if (getLang().Microsoft && Tok.is(tok::l_square)) 125334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet ParseMicrosoftAttributes(); 126bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ParseExternalDeclaration(Attr); 127bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 1281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1295144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner // Leave the namespace scope. 1305144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner NamespaceScope.Exit(); 1318ba5d792032f475eb653ca6340eb51068df0fa90Argyrios Kyrtzidis 13297144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace); 13397144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc); 1342d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis 13597144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner DeclEnd = RBraceLoc; 1365144832ae62cf97543b274d4bb88d5f74d0f7a20Chris Lattner return NamespcDecl; 1378f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner} 138c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 139f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson/// ParseNamespaceAlias - Parse the part after the '=' in a namespace 140f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson/// alias definition. 141f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson/// 142d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, 1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SourceLocation AliasLoc, 14497144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner IdentifierInfo *Alias, 14597144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner SourceLocation &DeclEnd) { 146f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson assert(Tok.is(tok::equal) && "Not equal token"); 1471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 148f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson ConsumeToken(); // eat the '='. 1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15049f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor if (Tok.is(tok::code_completion)) { 15123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CodeCompleteNamespaceAliasDecl(getCurScope()); 152dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor ConsumeCodeCompletionToken(); 15349f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor } 154193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 155f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson CXXScopeSpec SS; 156f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson // Parse (optional) nested-name-specifier. 157b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); 158f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson 159f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson if (SS.isInvalid() || Tok.isNot(tok::identifier)) { 160f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson Diag(Tok, diag::err_expected_namespace_name); 161f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson // Skip to end of the definition and eat the ';'. 162f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson SkipUntil(tok::semi); 163d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 164f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson } 165f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson 166f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson // Parse identifier. 16703bd5a1e9a54b62b10ae8aeb6eb5245e2031d98bAnders Carlsson IdentifierInfo *Ident = Tok.getIdentifierInfo(); 16803bd5a1e9a54b62b10ae8aeb6eb5245e2031d98bAnders Carlsson SourceLocation IdentLoc = ConsumeToken(); 1691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 170f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson // Eat the ';'. 17197144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner DeclEnd = Tok.getLocation(); 1726869d8ea1bb7d191b5b290f5a55ee74f2174829aChris Lattner ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name, 1736869d8ea1bb7d191b5b290f5a55ee74f2174829aChris Lattner "", tok::semi); 1741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias, 17603bd5a1e9a54b62b10ae8aeb6eb5245e2031d98bAnders Carlsson SS, IdentLoc, Ident); 177f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson} 178f67606ae2febe3bb0718f05040c6c4bc2c2c3276Anders Carlsson 179c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// ParseLinkage - We know that the current token is a string_literal 180c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// and just before that, that extern was seen. 181c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// 182c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// linkage-specification: [C++ 7.5p2: dcl.link] 183c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// 'extern' string-literal '{' declaration-seq[opt] '}' 184c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// 'extern' string-literal declaration 185c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// 1867d64271b162eaf5cae264ff64465b28af623dc17Chris LattnerDecl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) { 187c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor assert(Tok.is(tok::string_literal) && "Not a string literal!"); 188193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam llvm::SmallString<8> LangBuffer; 189453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor bool Invalid = false; 190453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid); 191453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor if (Invalid) 192d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 193c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 194c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner SourceLocation Loc = ConsumeStringToken(); 195c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 196074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor ParseScope LinkageScope(this, Scope::DeclScope); 197d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *LinkageSpec 19823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor = Actions.ActOnStartLinkageSpecification(getCurScope(), 199074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor /*FIXME: */SourceLocation(), 200d56638180014e60538cd666cd11fde6d4698e051Benjamin Kramer Loc, Lang, 2011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Tok.is(tok::l_brace)? Tok.getLocation() 202074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor : SourceLocation()); 203074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor 204bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt CXX0XAttributeList Attr; 2057d64271b162eaf5cae264ff64465b28af623dc17Chris Lattner if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 206bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Attr = ParseCXX0XAttributes(); 2077d64271b162eaf5cae264ff64465b28af623dc17Chris Lattner 208334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet if (getLang().Microsoft && Tok.is(tok::l_square)) 209334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet ParseMicrosoftAttributes(); 210193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 211074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor if (Tok.isNot(tok::l_brace)) { 21235f9a196ef897b9559de25aaecd957208f0b4f59Abramo Bagnara DS.setExternInLinkageSpec(true); 21309a63c97b95eb4dc6fd6b2323929e8cf12af03ffDouglas Gregor ParseExternalDeclaration(Attr, &DS); 21423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, 215074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor SourceLocation()); 2161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 217f44515a49b549171dc3ee9faa6281b72609da563Douglas Gregor 21863a011378d4b9483ce24400c163cb8d65ea096a5Douglas Gregor DS.abort(); 21963a011378d4b9483ce24400c163cb8d65ea096a5Douglas Gregor 220bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (Attr.HasAttr) 221bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) 222bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt << Attr.Range; 223bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 224f44515a49b549171dc3ee9faa6281b72609da563Douglas Gregor SourceLocation LBrace = ConsumeBrace(); 225f44515a49b549171dc3ee9faa6281b72609da563Douglas Gregor while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 226bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt CXX0XAttributeList Attr; 227bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 228bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Attr = ParseCXX0XAttributes(); 229334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet if (getLang().Microsoft && Tok.is(tok::l_square)) 230334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet ParseMicrosoftAttributes(); 231bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ParseExternalDeclaration(Attr); 232f44515a49b549171dc3ee9faa6281b72609da563Douglas Gregor } 233c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 234f44515a49b549171dc3ee9faa6281b72609da563Douglas Gregor SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); 2357d64271b162eaf5cae264ff64465b28af623dc17Chris Lattner return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, 2367d64271b162eaf5cae264ff64465b28af623dc17Chris Lattner RBrace); 237c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 238e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 239f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or 240f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// using-directive. Assumes that current token is 'using'. 241d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context, 24278b810559d89e996e00684335407443936ce34a1John McCall const ParsedTemplateInfo &TemplateInfo, 24378b810559d89e996e00684335407443936ce34a1John McCall SourceLocation &DeclEnd, 24478b810559d89e996e00684335407443936ce34a1John McCall CXX0XAttributeList Attr) { 245f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor assert(Tok.is(tok::kw_using) && "Not using token"); 246f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor 247f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor // Eat 'using'. 248f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor SourceLocation UsingLoc = ConsumeToken(); 249f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor 25049f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor if (Tok.is(tok::code_completion)) { 25123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CodeCompleteUsing(getCurScope()); 252dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor ConsumeCodeCompletionToken(); 25349f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor } 254193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 25578b810559d89e996e00684335407443936ce34a1John McCall // 'using namespace' means this is a using-directive. 25678b810559d89e996e00684335407443936ce34a1John McCall if (Tok.is(tok::kw_namespace)) { 25778b810559d89e996e00684335407443936ce34a1John McCall // Template parameters are always an error here. 25878b810559d89e996e00684335407443936ce34a1John McCall if (TemplateInfo.Kind) { 25978b810559d89e996e00684335407443936ce34a1John McCall SourceRange R = TemplateInfo.getSourceRange(); 26078b810559d89e996e00684335407443936ce34a1John McCall Diag(UsingLoc, diag::err_templated_using_directive) 26178b810559d89e996e00684335407443936ce34a1John McCall << R << FixItHint::CreateRemoval(R); 26278b810559d89e996e00684335407443936ce34a1John McCall } 26378b810559d89e996e00684335407443936ce34a1John McCall 264bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList); 26578b810559d89e996e00684335407443936ce34a1John McCall } 266bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 26778b810559d89e996e00684335407443936ce34a1John McCall // Otherwise, it must be a using-declaration. 26878b810559d89e996e00684335407443936ce34a1John McCall 26978b810559d89e996e00684335407443936ce34a1John McCall // Using declarations can't have attributes. 270bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (Attr.HasAttr) 271bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) 272bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt << Attr.Range; 2732f27477a29b6f5365ee545c1cac666cc8b95f518Chris Lattner 27478b810559d89e996e00684335407443936ce34a1John McCall return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd); 275f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor} 276f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor 277f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// ParseUsingDirective - Parse C++ using-directive, assumes 278f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// that current token is 'namespace' and 'using' was already parsed. 279f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// 280f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// using-directive: [C++ 7.3.p4: namespace.udir] 281f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// 'using' 'namespace' ::[opt] nested-name-specifier[opt] 282f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// namespace-name ; 283f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// [GNU] using-directive: 284f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// 'using' 'namespace' ::[opt] nested-name-specifier[opt] 285f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// namespace-name attributes[opt] ; 286f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// 287d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseUsingDirective(unsigned Context, 28878b810559d89e996e00684335407443936ce34a1John McCall SourceLocation UsingLoc, 28978b810559d89e996e00684335407443936ce34a1John McCall SourceLocation &DeclEnd, 29078b810559d89e996e00684335407443936ce34a1John McCall AttributeList *Attr) { 291f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); 292f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor 293f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor // Eat 'namespace'. 294f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor SourceLocation NamespcLoc = ConsumeToken(); 295f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor 29649f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor if (Tok.is(tok::code_completion)) { 29723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CodeCompleteUsingDirective(getCurScope()); 298dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor ConsumeCodeCompletionToken(); 29949f40bd0c9c9de5e74727774fec429b47d36303aDouglas Gregor } 300193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 301f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor CXXScopeSpec SS; 302f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor // Parse (optional) nested-name-specifier. 303b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); 304f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor 305f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor IdentifierInfo *NamespcName = 0; 306f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor SourceLocation IdentLoc = SourceLocation(); 307f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor 308f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor // Parse namespace-name. 309823c44e6d73141f642e207980b4021ddcf09897bChris Lattner if (SS.isInvalid() || Tok.isNot(tok::identifier)) { 310f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor Diag(Tok, diag::err_expected_namespace_name); 311f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor // If there was invalid namespace name, skip to end of decl, and eat ';'. 312f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor SkipUntil(tok::semi); 313f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor // FIXME: Are there cases, when we would like to call ActOnUsingDirective? 314d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 315f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor } 3161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 317823c44e6d73141f642e207980b4021ddcf09897bChris Lattner // Parse identifier. 318823c44e6d73141f642e207980b4021ddcf09897bChris Lattner NamespcName = Tok.getIdentifierInfo(); 319823c44e6d73141f642e207980b4021ddcf09897bChris Lattner IdentLoc = ConsumeToken(); 3201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 321823c44e6d73141f642e207980b4021ddcf09897bChris Lattner // Parse (optional) attributes (most likely GNU strong-using extension). 322bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt bool GNUAttr = false; 323bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (Tok.is(tok::kw___attribute)) { 324bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt GNUAttr = true; 325bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Attr = addAttributeLists(Attr, ParseGNUAttributes()); 326bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 3271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 328823c44e6d73141f642e207980b4021ddcf09897bChris Lattner // Eat ';'. 32997144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner DeclEnd = Tok.getLocation(); 3306869d8ea1bb7d191b5b290f5a55ee74f2174829aChris Lattner ExpectAndConsume(tok::semi, 3319ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor GNUAttr ? diag::err_expected_semi_after_attribute_list 3329ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor : diag::err_expected_semi_after_namespace_name, 3339ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor "", tok::semi); 334f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor 33523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, 336bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt IdentLoc, NamespcName, Attr); 337f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor} 338f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor 339f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that 340f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// 'using' was already seen. 341f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// 342f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// using-declaration: [C++ 7.3.p3: namespace.udecl] 343f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// 'using' 'typename'[opt] ::[opt] nested-name-specifier 3449cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor/// unqualified-id 3459cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor/// 'using' :: unqualified-id 346f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor/// 347d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseUsingDeclaration(unsigned Context, 34878b810559d89e996e00684335407443936ce34a1John McCall const ParsedTemplateInfo &TemplateInfo, 34978b810559d89e996e00684335407443936ce34a1John McCall SourceLocation UsingLoc, 35078b810559d89e996e00684335407443936ce34a1John McCall SourceLocation &DeclEnd, 35178b810559d89e996e00684335407443936ce34a1John McCall AccessSpecifier AS) { 3529cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor CXXScopeSpec SS; 3537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall SourceLocation TypenameLoc; 3549cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor bool IsTypeName; 3559cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor 35678b810559d89e996e00684335407443936ce34a1John McCall // TODO: in C++0x, if we have template parameters this must be a 35778b810559d89e996e00684335407443936ce34a1John McCall // template alias: 35878b810559d89e996e00684335407443936ce34a1John McCall // template <...> using id = type; 35978b810559d89e996e00684335407443936ce34a1John McCall 3609cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // Ignore optional 'typename'. 36112c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor // FIXME: This is wrong; we should parse this as a typename-specifier. 3629cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor if (Tok.is(tok::kw_typename)) { 3637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall TypenameLoc = Tok.getLocation(); 3649cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor ConsumeToken(); 3659cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor IsTypeName = true; 3669cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor } 3679cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor else 3689cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor IsTypeName = false; 3699cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor 3709cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // Parse nested-name-specifier. 371b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); 3729cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor 3739cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // Check nested-name specifier. 3749cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor if (SS.isInvalid()) { 3759cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor SkipUntil(tok::semi); 376d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 3779cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor } 3781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 379193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam // Parse the unqualified-id. We allow parsing of both constructor and 38012c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor // destructor names and allow the action module to diagnose any semantic 38112c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor // errors. 38212c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor UnqualifiedId Name; 383193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam if (ParseUnqualifiedId(SS, 38412c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor /*EnteringContext=*/false, 38512c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor /*AllowDestructorName=*/true, 386193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam /*AllowConstructorName=*/true, 387b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType(), 38812c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor Name)) { 3899cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor SkipUntil(tok::semi); 390d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 3919cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor } 392193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 3939cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // Parse (optional) attributes (most likely GNU strong-using extension). 3948113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek AttributeList *AttrList = 0; 3959cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor if (Tok.is(tok::kw___attribute)) 3968113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek AttrList = ParseGNUAttributes(); 3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3989cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // Eat ';'. 3999cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor DeclEnd = Tok.getLocation(); 4009cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 401193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam AttrList ? "attributes list" : "using declaration", 40212c118a8ff9f61a4d63146fe1a5c0d60987f99bbDouglas Gregor tok::semi); 4039cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor 40478b810559d89e996e00684335407443936ce34a1John McCall // Diagnose an attempt to declare a templated using-declaration. 40578b810559d89e996e00684335407443936ce34a1John McCall if (TemplateInfo.Kind) { 40678b810559d89e996e00684335407443936ce34a1John McCall SourceRange R = TemplateInfo.getSourceRange(); 40778b810559d89e996e00684335407443936ce34a1John McCall Diag(UsingLoc, diag::err_templated_using_declaration) 40878b810559d89e996e00684335407443936ce34a1John McCall << R << FixItHint::CreateRemoval(R); 40978b810559d89e996e00684335407443936ce34a1John McCall 41078b810559d89e996e00684335407443936ce34a1John McCall // Unfortunately, we have to bail out instead of recovering by 41178b810559d89e996e00684335407443936ce34a1John McCall // ignoring the parameters, just in case the nested name specifier 41278b810559d89e996e00684335407443936ce34a1John McCall // depends on the parameters. 41378b810559d89e996e00684335407443936ce34a1John McCall return 0; 41478b810559d89e996e00684335407443936ce34a1John McCall } 41578b810559d89e996e00684335407443936ce34a1John McCall 4168113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, 4178113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek Name, AttrList, IsTypeName, TypenameLoc); 418f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor} 419f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor 420511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion. 421511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson/// 422511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson/// static_assert-declaration: 423511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson/// static_assert ( constant-expression , string-literal ) ; 424511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson/// 425d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ 426511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration"); 427511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson SourceLocation StaticAssertLoc = ConsumeToken(); 4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 429511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson if (Tok.isNot(tok::l_paren)) { 430511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson Diag(Tok, diag::err_expected_lparen); 431d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 432511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson } 4331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 434511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson SourceLocation LParenLoc = ConsumeParen(); 435e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor 43660d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult AssertExpr(ParseConstantExpression()); 437511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson if (AssertExpr.isInvalid()) { 438511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson SkipUntil(tok::semi); 439d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 440511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson } 4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 442ad5f960f9e42568a87bf5e03dce7ad878f9ba6daAnders Carlsson if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) 443d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 444ad5f960f9e42568a87bf5e03dce7ad878f9ba6daAnders Carlsson 445511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson if (Tok.isNot(tok::string_literal)) { 446511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson Diag(Tok, diag::err_expected_string_literal); 447511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson SkipUntil(tok::semi); 448d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 449511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson } 4501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 45160d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult AssertMessage(ParseStringLiteralExpression()); 4521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (AssertMessage.isInvalid()) 453d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 454511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson 45594b15fbc3a10cdfb1639528a8a773b66a1e7cd9eAnders Carlsson MatchRHSPunctuation(tok::r_paren, LParenLoc); 4561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 45797144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner DeclEnd = Tok.getLocation(); 4589ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert); 459511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson 4609ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, 4619ae2f076ca5ab1feb3ba95629099ec2319833701John McCall AssertExpr.take(), 4629ae2f076ca5ab1feb3ba95629099ec2319833701John McCall AssertMessage.take()); 463511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson} 464511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson 4656fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier. 4666fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson/// 4676fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson/// 'decltype' ( expression ) 4686fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson/// 4696fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlssonvoid Parser::ParseDecltypeSpecifier(DeclSpec &DS) { 4706fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier"); 4716fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson 4726fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson SourceLocation StartLoc = ConsumeToken(); 4736fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson SourceLocation LParenLoc = Tok.getLocation(); 4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, 4766fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson "decltype")) { 4776fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson SkipUntil(tok::r_paren); 4786fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson return; 4796fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson } 4801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4816fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson // Parse the expression 4821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4836fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson // C++0x [dcl.type.simple]p4: 4846fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson // The operand of the decltype specifier is an unevaluated operand. 4856fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson EnterExpressionEvaluationContext Unevaluated(Actions, 486f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall Sema::Unevaluated); 48760d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult Result = ParseExpression(); 4886fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson if (Result.isInvalid()) { 4896fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson SkipUntil(tok::r_paren); 4906fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson return; 4916fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson } 4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4936fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson // Match the ')' 4946fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson SourceLocation RParenLoc; 4956fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson if (Tok.is(tok::r_paren)) 4966fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson RParenLoc = ConsumeParen(); 4976fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson else 4986fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson MatchRHSPunctuation(tok::r_paren, LParenLoc); 4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5006fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson if (RParenLoc.isInvalid()) 5016fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson return; 5026fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson 5036fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson const char *PrevSpec = 0; 504fec54013fcd0eb72642741584ca04c1bc292bef8John McCall unsigned DiagID; 5056fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson // Check for duplicate type specifiers (e.g. "int decltype(a)"). 5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, 507fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DiagID, Result.release())) 508fec54013fcd0eb72642741584ca04c1bc292bef8John McCall Diag(StartLoc, DiagID) << PrevSpec; 5096fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson} 5106fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson 51142a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor/// ParseClassName - Parse a C++ class-name, which names a class. Note 51242a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor/// that we only check that the result names a type; semantic analysis 51342a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor/// will need to verify that the type names a class. The result is 5147f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor/// either a type or NULL, depending on whether a type name was 51542a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor/// found. 51642a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor/// 51742a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor/// class-name: [C++ 9.1] 51842a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor/// identifier 5197f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor/// simple-template-id 5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 52131a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas GregorParser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation, 5229ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskin CXXScopeSpec *SS) { 5237f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor // Check whether we have a template-id that names a type. 5247f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor if (Tok.is(tok::annot_template_id)) { 5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateIdAnnotation *TemplateId 5267f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 527d9b600c1a589200be905c53e2e10fceb57efa18dDouglas Gregor if (TemplateId->Kind == TNK_Type_template || 528d9b600c1a589200be905c53e2e10fceb57efa18dDouglas Gregor TemplateId->Kind == TNK_Dependent_template_name) { 52931a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor AnnotateTemplateIdTokenAsType(SS); 5307f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor 5317f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); 532b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType Type = getTypeAnnotation(Tok); 5337f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor EndLocation = Tok.getAnnotationEndLoc(); 5347f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor ConsumeToken(); 53531a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor 53631a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor if (Type) 53731a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor return Type; 53831a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor return true; 5397f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor } 5407f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor 5417f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor // Fall through to produce an error below. 5427f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor } 5437f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor 54442a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor if (Tok.isNot(tok::identifier)) { 5451ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner Diag(Tok, diag::err_expected_class_name); 54631a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor return true; 54742a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor } 54842a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor 54984d0a19828599e8623223632d59447fd498999cfDouglas Gregor IdentifierInfo *Id = Tok.getIdentifierInfo(); 55084d0a19828599e8623223632d59447fd498999cfDouglas Gregor SourceLocation IdLoc = ConsumeToken(); 55184d0a19828599e8623223632d59447fd498999cfDouglas Gregor 55284d0a19828599e8623223632d59447fd498999cfDouglas Gregor if (Tok.is(tok::less)) { 55384d0a19828599e8623223632d59447fd498999cfDouglas Gregor // It looks the user intended to write a template-id here, but the 55484d0a19828599e8623223632d59447fd498999cfDouglas Gregor // template-name was wrong. Try to fix that. 55584d0a19828599e8623223632d59447fd498999cfDouglas Gregor TemplateNameKind TNK = TNK_Type_template; 55684d0a19828599e8623223632d59447fd498999cfDouglas Gregor TemplateTy Template; 55723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), 55884d0a19828599e8623223632d59447fd498999cfDouglas Gregor SS, Template, TNK)) { 55984d0a19828599e8623223632d59447fd498999cfDouglas Gregor Diag(IdLoc, diag::err_unknown_template_name) 56084d0a19828599e8623223632d59447fd498999cfDouglas Gregor << Id; 56184d0a19828599e8623223632d59447fd498999cfDouglas Gregor } 562193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 56384d0a19828599e8623223632d59447fd498999cfDouglas Gregor if (!Template) 56484d0a19828599e8623223632d59447fd498999cfDouglas Gregor return true; 56584d0a19828599e8623223632d59447fd498999cfDouglas Gregor 566193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam // Form the template name 56784d0a19828599e8623223632d59447fd498999cfDouglas Gregor UnqualifiedId TemplateName; 56884d0a19828599e8623223632d59447fd498999cfDouglas Gregor TemplateName.setIdentifier(Id, IdLoc); 569193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 57084d0a19828599e8623223632d59447fd498999cfDouglas Gregor // Parse the full template-id, then turn it into a type. 57184d0a19828599e8623223632d59447fd498999cfDouglas Gregor if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName, 57284d0a19828599e8623223632d59447fd498999cfDouglas Gregor SourceLocation(), true)) 57384d0a19828599e8623223632d59447fd498999cfDouglas Gregor return true; 57484d0a19828599e8623223632d59447fd498999cfDouglas Gregor if (TNK == TNK_Dependent_template_name) 57584d0a19828599e8623223632d59447fd498999cfDouglas Gregor AnnotateTemplateIdTokenAsType(SS); 576193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 57784d0a19828599e8623223632d59447fd498999cfDouglas Gregor // If we didn't end up with a typename token, there's nothing more we 57884d0a19828599e8623223632d59447fd498999cfDouglas Gregor // can do. 57984d0a19828599e8623223632d59447fd498999cfDouglas Gregor if (Tok.isNot(tok::annot_typename)) 58084d0a19828599e8623223632d59447fd498999cfDouglas Gregor return true; 581193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 58284d0a19828599e8623223632d59447fd498999cfDouglas Gregor // Retrieve the type from the annotation token, consume that token, and 58384d0a19828599e8623223632d59447fd498999cfDouglas Gregor // return. 58484d0a19828599e8623223632d59447fd498999cfDouglas Gregor EndLocation = Tok.getAnnotationEndLoc(); 585b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType Type = getTypeAnnotation(Tok); 58684d0a19828599e8623223632d59447fd498999cfDouglas Gregor ConsumeToken(); 58784d0a19828599e8623223632d59447fd498999cfDouglas Gregor return Type; 58884d0a19828599e8623223632d59447fd498999cfDouglas Gregor } 58984d0a19828599e8623223632d59447fd498999cfDouglas Gregor 59042a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor // We have an identifier; check whether it is actually a type. 591b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true); 592193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam if (!Type) { 593124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor Diag(IdLoc, diag::err_expected_class_name); 59431a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor return true; 59542a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor } 59642a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor 59742a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor // Consume the identifier. 59884d0a19828599e8623223632d59447fd498999cfDouglas Gregor EndLocation = IdLoc; 5995606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky 6005606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky // Fake up a Declarator to use with ActOnTypeName. 6015606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky DeclSpec DS; 6025606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky DS.SetRangeStart(IdLoc); 6035606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky DS.SetRangeEnd(EndLocation); 6045606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky DS.getTypeSpecScope() = *SS; 6055606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky 6065606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky const char *PrevSpec = 0; 6075606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky unsigned DiagID; 6085606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type); 6095606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky 6105606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 6115606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 61242a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor} 61342a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor 614e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or 615e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which 616e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// until we reach the start of a definition or see a token that 617d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl/// cannot start a definition. If SuppressDeclarations is true, we do know. 618e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 619e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class-specifier: [C++ class] 620e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class-head '{' member-specification[opt] '}' 621e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class-head '{' member-specification[opt] '}' attributes[opt] 622e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class-head: 623e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class-key identifier[opt] base-clause[opt] 624e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class-key nested-name-specifier identifier base-clause[opt] 625e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class-key nested-name-specifier[opt] simple-template-id 626e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// base-clause[opt] 627e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] 6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// [GNU] class-key attributes[opt] nested-name-specifier 629e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// identifier base-clause[opt] 6301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// [GNU] class-key attributes[opt] nested-name-specifier[opt] 631e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// simple-template-id base-clause[opt] 632e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class-key: 633e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'class' 634e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'struct' 635e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'union' 636e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 637e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// elaborated-type-specifier: [C++ dcl.type.elab] 6381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// class-key ::[opt] nested-name-specifier[opt] identifier 6391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] 6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// simple-template-id 641e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 642e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// Note that the C++ class-specifier and elaborated-type-specifier, 643e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// together, subsume the C99 struct-or-union-specifier: 644e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 645e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// struct-or-union-specifier: [C99 6.7.2.1] 646e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// struct-or-union identifier[opt] '{' struct-contents '}' 647e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// struct-or-union identifier 648e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents 649e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// '}' attributes[opt] 650e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// [GNU] struct-or-union attributes[opt] identifier 651e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// struct-or-union: 652e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'struct' 653e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'union' 6544c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattnervoid Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, 6554c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner SourceLocation StartLoc, DeclSpec &DS, 6564d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor const ParsedTemplateInfo &TemplateInfo, 657d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl AccessSpecifier AS, bool SuppressDeclarations){ 6584c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner DeclSpec::TST TagType; 6594c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner if (TagTokKind == tok::kw_struct) 6604c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner TagType = DeclSpec::TST_struct; 6614c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner else if (TagTokKind == tok::kw_class) 6624c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner TagType = DeclSpec::TST_class; 6634c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner else { 6644c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner assert(TagTokKind == tok::kw_union && "Not a class specifier"); 6654c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner TagType = DeclSpec::TST_union; 6664c97d762d8c5a84f6554e5bfb31d28c90df64158Chris Lattner } 667e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 668374929f7e88f6c7a96382b3eb4201b721c418372Douglas Gregor if (Tok.is(tok::code_completion)) { 669374929f7e88f6c7a96382b3eb4201b721c418372Douglas Gregor // Code completion for a struct, class, or union name. 67023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CodeCompleteTag(getCurScope(), TagType); 671dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor ConsumeCodeCompletionToken(); 672374929f7e88f6c7a96382b3eb4201b721c418372Douglas Gregor } 673193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 674926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth // C++03 [temp.explicit] 14.7.2/8: 675926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth // The usual access checking rules do not apply to names used to specify 676926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth // explicit instantiations. 677926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth // 678926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth // As an extension we do not perform access checking on the names used to 679926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth // specify explicit specializations either. This is important to allow 680926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth // specializing traits classes for private types. 681926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth bool SuppressingAccessChecks = false; 682926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || 683926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) { 684926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth Actions.ActOnStartSuppressingAccessChecks(); 685926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth SuppressingAccessChecks = true; 686926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth } 687926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth 688bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttributeList *AttrList = 0; 689e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // If attributes exist after tag, parse them. 690e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor if (Tok.is(tok::kw___attribute)) 691bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttrList = ParseGNUAttributes(); 692e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 693f59e17ecf06ac60065e2d02058bd6f21f5d216ccSteve Naroff // If declspecs exist after tag, parse them. 694b1d397c23e1f8fd8b404d5731d531e7500f7140dJohn McCall while (Tok.is(tok::kw___declspec)) 695bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttrList = ParseMicrosoftDeclSpec(AttrList); 696193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 697bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // If C++0x attributes exist here, parse them. 698bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // FIXME: Are we consistent with the ordering of parsing of different 699bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // styles of attributes? 700bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (isCXX0XAttributeSpecifier()) 701bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList); 7021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 703b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) { 704b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but 705b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the 7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // token sequence "struct __is_pod", make __is_pod into a normal 707b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor // identifier rather than a keyword, to allow libstdc++ 4.2 to work 708b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor // properly. 709646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); 710b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor Tok.setKind(tok::identifier); 711b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor } 712b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor 713b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) { 714b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but 715b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the 7161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // token sequence "struct __is_empty", make __is_empty into a normal 717b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor // identifier rather than a keyword, to allow libstdc++ 4.2 to work 718b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor // properly. 719646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); 720b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor Tok.setKind(tok::identifier); 721b117a60f7684261ddc8c8f14e8ef8a827e6af814Douglas Gregor } 7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 723eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis // Parse the (optional) nested-name-specifier. 724aa87d33309f505b68c3bfc17486c93e4d224b24fJohn McCall CXXScopeSpec &SS = DS.getTypeSpecScope(); 72508d92ecf6e5b1fd23177a08c2312b58d63d863dbChris Lattner if (getLang().CPlusPlus) { 72608d92ecf6e5b1fd23177a08c2312b58d63d863dbChris Lattner // "FOO : BAR" is not a potential typo for "FOO::BAR". 72708d92ecf6e5b1fd23177a08c2312b58d63d863dbChris Lattner ColonProtectionRAIIObject X(*this); 728193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 729b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) 730207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall DS.SetTypeSpecError(); 7319ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall if (SS.isSet()) 73208d92ecf6e5b1fd23177a08c2312b58d63d863dbChris Lattner if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) 73308d92ecf6e5b1fd23177a08c2312b58d63d863dbChris Lattner Diag(Tok, diag::err_expected_ident); 73408d92ecf6e5b1fd23177a08c2312b58d63d863dbChris Lattner } 735cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor 7362cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; 7372cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor 738cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor // Parse the (optional) class name or simple-template-id. 739e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor IdentifierInfo *Name = 0; 740e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor SourceLocation NameLoc; 74139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor TemplateIdAnnotation *TemplateId = 0; 742e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor if (Tok.is(tok::identifier)) { 743e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor Name = Tok.getIdentifierInfo(); 744e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor NameLoc = ConsumeToken(); 745193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 7465ee3734c0b5222a7c445591a1f14102c1b3a289bDouglas Gregor if (Tok.is(tok::less) && getLang().CPlusPlus) { 747193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam // The name was supposed to refer to a template, but didn't. 7482cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor // Eat the template argument list and try to continue parsing this as 7492cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor // a class (or template thereof). 7502cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor TemplateArgList TemplateArgs; 7512cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor SourceLocation LAngleLoc, RAngleLoc; 752193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS, 7532cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor true, LAngleLoc, 754314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor TemplateArgs, RAngleLoc)) { 7552cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor // We couldn't parse the template argument list at all, so don't 7562cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor // try to give any location information for the list. 7572cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor LAngleLoc = RAngleLoc = SourceLocation(); 7582cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor } 759193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 7602cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor Diag(NameLoc, diag::err_explicit_spec_non_template) 761c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) 7622cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor << (TagType == DeclSpec::TST_class? 0 7632cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor : TagType == DeclSpec::TST_struct? 1 7642cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor : 2) 7652cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor << Name 7662cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor << SourceRange(LAngleLoc, RAngleLoc); 767193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 768193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam // Strip off the last template parameter list if it was empty, since 769c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor // we've removed its template argument list. 770c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { 771c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor if (TemplateParams && TemplateParams->size() > 1) { 772c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor TemplateParams->pop_back(); 773c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor } else { 774c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor TemplateParams = 0; 775193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind 776c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor = ParsedTemplateInfo::NonTemplate; 777c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor } 778c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor } else if (TemplateInfo.Kind 779c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor == ParsedTemplateInfo::ExplicitInstantiation) { 780c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor // Pretend this is just a forward declaration. 7812cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor TemplateParams = 0; 782193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind 7832cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor = ParsedTemplateInfo::NonTemplate; 784193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc 785c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor = SourceLocation(); 786c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc 787c78c06d81f9838aea4198e4965cc1b26bb0bf838Douglas Gregor = SourceLocation(); 7882cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor } 7892cc782f7932f1069d9fa8bb5c518165802aad68dDouglas Gregor } 79039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor } else if (Tok.is(tok::annot_template_id)) { 79139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 79239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor NameLoc = ConsumeToken(); 793cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor 794c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor if (TemplateId->Kind != TNK_Type_template) { 79539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // The template-name in the simple-template-id refers to 79639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // something other than a class template. Give an appropriate 79739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // error message and skip to the ';'. 79839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor SourceRange Range(NameLoc); 79939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor if (SS.isNotEmpty()) 80039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor Range.setBegin(SS.getBeginLoc()); 80139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor 80239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) 80339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor << Name << static_cast<int>(TemplateId->Kind) << Range; 8041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 80539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor DS.SetTypeSpecError(); 80639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor SkipUntil(tok::semi, false, true); 80739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor TemplateId->Destroy(); 808926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth if (SuppressingAccessChecks) 809926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth Actions.ActOnStopSuppressingAccessChecks(); 810926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth 81139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor return; 812cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor } 813e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor } 814e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 815926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth // As soon as we're finished parsing the class's template-id, turn access 816926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth // checking back on. 817926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth if (SuppressingAccessChecks) 818926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth Actions.ActOnStopSuppressingAccessChecks(); 819926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth 82067d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall // There are four options here. If we have 'struct foo;', then this 82167d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall // is either a forward declaration or a friend declaration, which 82267d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall // have to be treated differently. If we have 'struct foo {...' or 82339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // 'struct foo :...' then this is a definition. Otherwise we have 824e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // something like 'struct foo xyz', a reference. 825d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl // However, in some contexts, things look like declarations but are just 826d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl // references, e.g. 827d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl // new struct s; 828d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl // or 829d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl // &T::operator struct s; 830d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl // For these, SuppressDeclarations is true. 831f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall Sema::TagUseKind TUK; 832d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl if (SuppressDeclarations) 833f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TUK = Sema::TUK_Reference; 834d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){ 835d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor if (DS.isFriendSpecified()) { 836d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor // C++ [class.friend]p2: 837d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor // A class shall not be defined in a friend declaration. 838d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor Diag(Tok.getLocation(), diag::err_friend_decl_defines_class) 839d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor << SourceRange(DS.getFriendSpecLoc()); 840d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor 841d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor // Skip everything up to the semicolon, so that this looks like a proper 842d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor // friend class (or template thereof) declaration. 843d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor SkipUntil(tok::semi, true, true); 844f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TUK = Sema::TUK_Friend; 845d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor } else { 846d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor // Okay, this is a class definition. 847f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TUK = Sema::TUK_Definition; 848d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor } 849d85bea2affdd59d83d1be7d24b97f436484c3625Douglas Gregor } else if (Tok.is(tok::semi)) 850f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; 851e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor else 852f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TUK = Sema::TUK_Reference; 853e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 854207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error || 855f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TUK != Sema::TUK_Definition)) { 856207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall if (DS.getTypeSpecType() != DeclSpec::TST_error) { 857207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall // We have a declaration or reference to an anonymous class. 858207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall Diag(StartLoc, diag::err_anon_type_definition) 859207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall << DeclSpec::getSpecifierName(TagType); 860207014eb2b372aa33721e86d90a8a586ba8dc8aeJohn McCall } 861e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 862e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor SkipUntil(tok::comma, true); 86339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor 86439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor if (TemplateId) 86539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor TemplateId->Destroy(); 866e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor return; 867e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor } 868e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 869ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor // Create the tag portion of the class or class template. 870d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall DeclResult TagOrTempResult = true; // invalid 871d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall TypeResult TypeResult = true; // invalid 8724d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor 873402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor bool Owned = false; 874f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall if (TemplateId) { 8754d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // Explicit specialization, class template partial specialization, 8764d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // or explicit instantiation. 8771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ASTTemplateArgsPtr TemplateArgsPtr(Actions, 87839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor TemplateId->getTemplateArgs(), 87939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor TemplateId->NumArgs); 8804d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && 881f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TUK == Sema::TUK_Declaration) { 8824d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // This is an explicit instantiation of a class template. 8834d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor TagOrTempResult 88423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor = Actions.ActOnExplicitInstantiation(getCurScope(), 88545f965581935791a018df829a14dff53c1dd8f47Douglas Gregor TemplateInfo.ExternLoc, 8861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateInfo.TemplateLoc, 8874d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor TagType, 8881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump StartLoc, 8894d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor SS, 8902b5289b6fd7e3d9899868410a498c081c9595662John McCall TemplateId->Template, 8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateId->TemplateNameLoc, 8921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateId->LAngleLoc, 8934d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor TemplateArgsPtr, 8941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateId->RAngleLoc, 895bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttrList); 89674256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall 89774256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall // Friend template-ids are treated as references unless 89874256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall // they have template headers, in which case they're ill-formed 89974256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall // (FIXME: "template <class T> friend class A<T>::B<int>;"). 90074256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall // We diagnose this error in ActOnClassTemplateSpecialization. 901f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall } else if (TUK == Sema::TUK_Reference || 902f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall (TUK == Sema::TUK_Friend && 90374256f5ea6950c9fd34595aa124eb4740372f15cJohn McCall TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) { 904c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall TypeResult 9052b5289b6fd7e3d9899868410a498c081c9595662John McCall = Actions.ActOnTemplateIdType(TemplateId->Template, 9066b2becfc434b0bdced8560802c4d0e03148c61b8John McCall TemplateId->TemplateNameLoc, 9076b2becfc434b0bdced8560802c4d0e03148c61b8John McCall TemplateId->LAngleLoc, 9086b2becfc434b0bdced8560802c4d0e03148c61b8John McCall TemplateArgsPtr, 9096b2becfc434b0bdced8560802c4d0e03148c61b8John McCall TemplateId->RAngleLoc); 9106b2becfc434b0bdced8560802c4d0e03148c61b8John McCall 91145ab4b5f8961dadcef6545ed6956da5daf95c6cbCraig Silverstein TypeResult = Actions.ActOnTagTemplateIdType(SS, TypeResult, TUK, 912c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall TagType, StartLoc); 9134d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor } else { 9144d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // This is an explicit specialization or a class template 9154d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // partial specialization. 9164d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor TemplateParameterLists FakedParamLists; 9174d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor 9184d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { 9194d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // This looks like an explicit instantiation, because we have 9204d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // something like 9214d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // 9224d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // template class Foo<X> 9234d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // 9243f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor // but it actually has a definition. Most likely, this was 9254d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // meant to be an explicit specialization, but the user forgot 9264d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // the '<>' after 'template'. 927f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall assert(TUK == Sema::TUK_Definition && "Expected a definition here"); 9284d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor 9291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SourceLocation LAngleLoc 9304d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); 9311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(TemplateId->TemplateNameLoc, 9324d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor diag::err_explicit_instantiation_with_definition) 9334d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor << SourceRange(TemplateInfo.TemplateLoc) 934849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor << FixItHint::CreateInsertion(LAngleLoc, "<>"); 9354d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor 9364d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // Create a fake template parameter list that contains only 9374d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // "template<>", so that we treat this construct as a class 9384d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // template specialization. 9394d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor FakedParamLists.push_back( 9401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Actions.ActOnTemplateParameterList(0, SourceLocation(), 9414d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor TemplateInfo.TemplateLoc, 9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump LAngleLoc, 9431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 0, 0, 9444d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor LAngleLoc)); 9454d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor TemplateParams = &FakedParamLists; 9464d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor } 9474d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor 9484d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor // Build the class template specialization. 9494d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor TagOrTempResult 95023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK, 95139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor StartLoc, SS, 9522b5289b6fd7e3d9899868410a498c081c9595662John McCall TemplateId->Template, 9531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateId->TemplateNameLoc, 9541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateId->LAngleLoc, 95539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor TemplateArgsPtr, 9561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateId->RAngleLoc, 957bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttrList, 958f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall MultiTemplateParamsArg(Actions, 959cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor TemplateParams? &(*TemplateParams)[0] : 0, 960cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor TemplateParams? TemplateParams->size() : 0)); 9614d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor } 96239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor TemplateId->Destroy(); 9633f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && 964f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TUK == Sema::TUK_Declaration) { 9653f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor // Explicit instantiation of a member of a class template 9663f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor // specialization, e.g., 9673f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor // 9683f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor // template struct Outer<int>::Inner; 9693f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor // 9703f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor TagOrTempResult 97123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor = Actions.ActOnExplicitInstantiation(getCurScope(), 97245f965581935791a018df829a14dff53c1dd8f47Douglas Gregor TemplateInfo.ExternLoc, 9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateInfo.TemplateLoc, 9741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TagType, StartLoc, SS, Name, 975bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt NameLoc, AttrList); 9769a34edb710917798aa30263374f624f13b594605John McCall } else if (TUK == Sema::TUK_Friend && 9779a34edb710917798aa30263374f624f13b594605John McCall TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { 9789a34edb710917798aa30263374f624f13b594605John McCall TagOrTempResult = 9799a34edb710917798aa30263374f624f13b594605John McCall Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(), 9809a34edb710917798aa30263374f624f13b594605John McCall TagType, StartLoc, SS, 9819a34edb710917798aa30263374f624f13b594605John McCall Name, NameLoc, AttrList, 9829a34edb710917798aa30263374f624f13b594605John McCall MultiTemplateParamsArg(Actions, 9839a34edb710917798aa30263374f624f13b594605John McCall TemplateParams? &(*TemplateParams)[0] : 0, 9849a34edb710917798aa30263374f624f13b594605John McCall TemplateParams? TemplateParams->size() : 0)); 9853f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor } else { 9863f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && 987f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TUK == Sema::TUK_Definition) { 9883f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor // FIXME: Diagnose this particular error. 9893f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor } 9903f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor 991c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall bool IsDependent = false; 992c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall 993a25c4080a490ea2bab6f54094dd75b19eae83770John McCall // Don't pass down template parameter lists if this is just a tag 994a25c4080a490ea2bab6f54094dd75b19eae83770John McCall // reference. For example, we don't need the template parameters here: 995a25c4080a490ea2bab6f54094dd75b19eae83770John McCall // template <class T> class A *makeA(T t); 996a25c4080a490ea2bab6f54094dd75b19eae83770John McCall MultiTemplateParamsArg TParams; 997a25c4080a490ea2bab6f54094dd75b19eae83770John McCall if (TUK != Sema::TUK_Reference && TemplateParams) 998a25c4080a490ea2bab6f54094dd75b19eae83770John McCall TParams = 999a25c4080a490ea2bab6f54094dd75b19eae83770John McCall MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); 1000a25c4080a490ea2bab6f54094dd75b19eae83770John McCall 10013f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor // Declaration or definition of a class type 10029a34edb710917798aa30263374f624f13b594605John McCall TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, 10039a34edb710917798aa30263374f624f13b594605John McCall SS, Name, NameLoc, AttrList, AS, 1004a25c4080a490ea2bab6f54094dd75b19eae83770John McCall TParams, Owned, IsDependent, false, 10051274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor clang::TypeResult()); 1006c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall 1007c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall // If ActOnTag said the type was dependent, try again with the 1008c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall // less common call. 10099a34edb710917798aa30263374f624f13b594605John McCall if (IsDependent) { 10109a34edb710917798aa30263374f624f13b594605John McCall assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); 101123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, 1012193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam SS, Name, StartLoc, NameLoc); 10139a34edb710917798aa30263374f624f13b594605John McCall } 10143f5b61c394f4f205bcb4d316eb2a7a0a68b8af86Douglas Gregor } 1015e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1016e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // If there is a body, parse it and inform the actions module. 1017f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall if (TUK == Sema::TUK_Definition) { 1018bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall assert(Tok.is(tok::l_brace) || 1019bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall (getLang().CPlusPlus && Tok.is(tok::colon))); 102007952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis if (getLang().CPlusPlus) 1021212e81cc5151b3c42346e43cfd42499a53ffd39aDouglas Gregor ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get()); 102207952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis else 1023212e81cc5151b3c42346e43cfd42499a53ffd39aDouglas Gregor ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get()); 1024e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor } 1025e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1026b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall // FIXME: The DeclSpec should keep the locations of both the keyword and the 1027b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall // name (if there is one). 1028b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc; 1029b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall 1030b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall const char *PrevSpec = 0; 1031b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall unsigned DiagID; 1032b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall bool Result; 1033c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall if (!TypeResult.isInvalid()) { 1034b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall Result = DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, 1035b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall PrevSpec, DiagID, TypeResult.get()); 1036c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall } else if (!TagOrTempResult.isInvalid()) { 1037b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall Result = DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID, 1038b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall TagOrTempResult.get(), Owned); 1039c4e7019d5c9034a2d84ee4695f8e98dc025ac131John McCall } else { 1040ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor DS.SetTypeSpecError(); 104166e9977ddd6b197317d149213b76a9af0d3df4deAnders Carlsson return; 104266e9977ddd6b197317d149213b76a9af0d3df4deAnders Carlsson } 10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1044b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall if (Result) 1045fec54013fcd0eb72642741584ca04c1bc292bef8John McCall Diag(StartLoc, DiagID) << PrevSpec; 1046193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 10474ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // At this point, we've successfully parsed a class-specifier in 'definition' 10484ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // form (e.g. "struct foo { int x; }". While we could just return here, we're 10494ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // going to look at what comes after it to improve error recovery. If an 10504ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // impossible token occurs next, we assume that the programmer forgot a ; at 10514ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // the end of the declaration and recover that way. 10524ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // 10534ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // This switch enumerates the valid "follow" set for definition. 1054f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall if (TUK == Sema::TUK_Definition) { 1055b3a4e432c90be98c6d918087750397e86d030368Chris Lattner bool ExpectedSemi = true; 10564ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner switch (Tok.getKind()) { 1057b3a4e432c90be98c6d918087750397e86d030368Chris Lattner default: break; 10584ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner case tok::semi: // struct foo {...} ; 105999c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::star: // struct foo {...} * P; 106099c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::amp: // struct foo {...} & R = ... 106199c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::identifier: // struct foo {...} V ; 106299c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::r_paren: //(struct foo {...} ) {4} 106399c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::annot_cxxscope: // struct foo {...} a:: b; 106499c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::annot_typename: // struct foo {...} a ::b; 106599c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::annot_template_id: // struct foo {...} a<int> ::b; 1066c2e1c1af8ffe750b827284f207b9207112c7cc4eChris Lattner case tok::l_paren: // struct foo {...} ( x); 106716acfee729e00536af827935ccfcf69be721e462Chris Lattner case tok::comma: // __builtin_offsetof(struct foo{...} , 1068b3a4e432c90be98c6d918087750397e86d030368Chris Lattner ExpectedSemi = false; 1069b3a4e432c90be98c6d918087750397e86d030368Chris Lattner break; 1070b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // Type qualifiers 1071b3a4e432c90be98c6d918087750397e86d030368Chris Lattner case tok::kw_const: // struct foo {...} const x; 1072b3a4e432c90be98c6d918087750397e86d030368Chris Lattner case tok::kw_volatile: // struct foo {...} volatile x; 1073b3a4e432c90be98c6d918087750397e86d030368Chris Lattner case tok::kw_restrict: // struct foo {...} restrict x; 1074b3a4e432c90be98c6d918087750397e86d030368Chris Lattner case tok::kw_inline: // struct foo {...} inline foo() {}; 107599c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner // Storage-class specifiers 107699c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::kw_static: // struct foo {...} static x; 107799c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::kw_extern: // struct foo {...} extern x; 107899c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::kw_typedef: // struct foo {...} typedef x; 107999c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::kw_register: // struct foo {...} register x; 108099c952046600f6bfccf315aa7ad5b1be2d242cc3Chris Lattner case tok::kw_auto: // struct foo {...} auto x; 108133f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor case tok::kw_mutable: // struct foo {...} mutable x; 1082b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // As shown above, type qualifiers and storage class specifiers absolutely 1083b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // can occur after class specifiers according to the grammar. However, 1084b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // almost noone actually writes code like this. If we see one of these, 1085b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // it is much more likely that someone missed a semi colon and the 1086b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // type/storage class specifier we're seeing is part of the *next* 1087b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // intended declaration, as in: 1088b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // 1089b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // struct foo { ... } 1090b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // typedef int X; 1091b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // 1092b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // We'd really like to emit a missing semicolon error instead of emitting 1093b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // an error on the 'int' saying that you can't have two type specifiers in 1094b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // the same declaration of X. Because of this, we look ahead past this 1095b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // token to see if it's a type specifier. If so, we know the code is 1096b3a4e432c90be98c6d918087750397e86d030368Chris Lattner // otherwise invalid, so we can produce the expected semi error. 1097b3a4e432c90be98c6d918087750397e86d030368Chris Lattner if (!isKnownToBeTypeSpecifier(NextToken())) 1098b3a4e432c90be98c6d918087750397e86d030368Chris Lattner ExpectedSemi = false; 10994ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner break; 1100193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 1101193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam case tok::r_brace: // struct bar { struct foo {...} } 11024ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // Missing ';' at end of struct is accepted as an extension in C mode. 1103b3a4e432c90be98c6d918087750397e86d030368Chris Lattner if (!getLang().CPlusPlus) 1104b3a4e432c90be98c6d918087750397e86d030368Chris Lattner ExpectedSemi = false; 1105b3a4e432c90be98c6d918087750397e86d030368Chris Lattner break; 1106b3a4e432c90be98c6d918087750397e86d030368Chris Lattner } 1107193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 1108b3a4e432c90be98c6d918087750397e86d030368Chris Lattner if (ExpectedSemi) { 11094ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, 11104ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner TagType == DeclSpec::TST_class ? "class" 11114ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner : TagType == DeclSpec::TST_struct? "struct" : "union"); 11124ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // Push this token back into the preprocessor and change our current token 11134ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // to ';' so that the rest of the code recovers as though there were an 11144ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // ';' after the definition. 11154ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner PP.EnterToken(Tok); 1116193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam Tok.setKind(tok::semi); 11174ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner } 11184ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner } 1119e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor} 1120e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 11211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. 1122e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 1123e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// base-clause : [C++ class.derived] 1124e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// ':' base-specifier-list 1125e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// base-specifier-list: 1126e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// base-specifier '...'[opt] 1127e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// base-specifier-list ',' base-specifier '...'[opt] 1128d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseBaseClause(Decl *ClassDecl) { 1129e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor assert(Tok.is(tok::colon) && "Not a base clause"); 1130e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor ConsumeToken(); 1131e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1132f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor // Build up an array of parsed base specifiers. 1133ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo; 1134f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor 1135e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor while (true) { 1136e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // Parse a base-specifier. 1137f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor BaseResult Result = ParseBaseSpecifier(ClassDecl); 11385ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor if (Result.isInvalid()) { 1139e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // Skip the rest of this base specifier, up until the comma or 1140e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // opening brace. 1141f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor SkipUntil(tok::comma, tok::l_brace, true, true); 1142f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor } else { 1143f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor // Add this to our array of base specifiers. 11445ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor BaseInfo.push_back(Result.get()); 1145e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor } 1146e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1147e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // If the next token is a comma, consume it and keep reading 1148e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // base-specifiers. 1149e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor if (Tok.isNot(tok::comma)) break; 11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1151e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // Consume the comma. 1152e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor ConsumeToken(); 1153e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor } 1154f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor 1155f8268ae3196002bbab6adb830302e79b0f368f13Douglas Gregor // Attach the base specifiers 1156beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size()); 1157e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor} 1158e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1159e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is 1160e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// one entry in the base class list of a class specifier, for example: 1161e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class foo : public bar, virtual private baz { 1162e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'public bar' and 'virtual private baz' are each base-specifiers. 1163e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 1164e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// base-specifier: [C++ class.derived] 1165e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// ::[opt] nested-name-specifier[opt] class-name 1166e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] 1167e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class-name 1168e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] 1169e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// class-name 1170d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallParser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { 1171e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor bool IsVirtual = false; 1172e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor SourceLocation StartLoc = Tok.getLocation(); 1173e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1174e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // Parse the 'virtual' keyword. 1175e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor if (Tok.is(tok::kw_virtual)) { 1176e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor ConsumeToken(); 1177e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor IsVirtual = true; 1178e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor } 1179e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1180e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // Parse an (optional) access specifier. 1181e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor AccessSpecifier Access = getAccessSpecifierIfPresent(); 118292f883177b162928a8e632e4e3b93fafd2b26072John McCall if (Access != AS_none) 1183e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor ConsumeToken(); 11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1185e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // Parse the 'virtual' keyword (again!), in case it came after the 1186e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // access specifier. 1187e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor if (Tok.is(tok::kw_virtual)) { 1188e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor SourceLocation VirtualLoc = ConsumeToken(); 1189e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor if (IsVirtual) { 1190e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // Complain about duplicate 'virtual' 11911ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner Diag(VirtualLoc, diag::err_dup_virtual) 1192849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor << FixItHint::CreateRemoval(VirtualLoc); 1193e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor } 1194e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1195e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor IsVirtual = true; 1196e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor } 1197e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1198eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis // Parse optional '::' and optional nested-name-specifier. 1199eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis CXXScopeSpec SS; 1200b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); 1201e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1202e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // The location of the base class itself. 1203e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor SourceLocation BaseLoc = Tok.getLocation(); 120442a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor 120542a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor // Parse the class-name. 12067f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor SourceLocation EndLocation; 120731a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor TypeResult BaseType = ParseClassName(EndLocation, &SS); 120831a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor if (BaseType.isInvalid()) 120942a552f8200ba5948661aee0106fce0c04e39818Douglas Gregor return true; 12101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Find the complete source range for the base-specifier. 12127f43d6764797ab21216421aeb00f4ec314d503d1Douglas Gregor SourceRange Range(StartLoc, EndLocation); 12131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1214e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // Notify semantic analysis that we have parsed a complete 1215e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor // base-specifier. 1216a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, 121731a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor BaseType.get(), BaseLoc); 1218e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor} 1219e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor 1220e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// getAccessSpecifierIfPresent - Determine whether the next token is 1221e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// a C++ access-specifier. 1222e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 1223e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// access-specifier: [C++ class.derived] 1224e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'private' 1225e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'protected' 1226e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor/// 'public' 12271eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpAccessSpecifier Parser::getAccessSpecifierIfPresent() const { 1228e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor switch (Tok.getKind()) { 1229e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor default: return AS_none; 1230e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor case tok::kw_private: return AS_private; 1231e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor case tok::kw_protected: return AS_protected; 1232e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor case tok::kw_public: return AS_public; 1233e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor } 1234e37ac4ff1620ed2d7026f52baccbfa022d79ced1Douglas Gregor} 12354cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 1236d33133cdc1af466f9c276249b2621be03867888bEli Friedmanvoid Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo, 1237d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *ThisDecl) { 1238d33133cdc1af466f9c276249b2621be03867888bEli Friedman // We just declared a member function. If this member function 1239d33133cdc1af466f9c276249b2621be03867888bEli Friedman // has any default arguments, we'll need to parse them later. 1240d33133cdc1af466f9c276249b2621be03867888bEli Friedman LateParsedMethodDeclaration *LateMethod = 0; 12411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump DeclaratorChunk::FunctionTypeInfo &FTI 1242d33133cdc1af466f9c276249b2621be03867888bEli Friedman = DeclaratorInfo.getTypeObject(0).Fun; 1243d33133cdc1af466f9c276249b2621be03867888bEli Friedman for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { 1244d33133cdc1af466f9c276249b2621be03867888bEli Friedman if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { 1245d33133cdc1af466f9c276249b2621be03867888bEli Friedman if (!LateMethod) { 1246d33133cdc1af466f9c276249b2621be03867888bEli Friedman // Push this method onto the stack of late-parsed method 1247d33133cdc1af466f9c276249b2621be03867888bEli Friedman // declarations. 1248d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); 1249d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor getCurrentClass().LateParsedDeclarations.push_back(LateMethod); 125023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor LateMethod->TemplateScope = getCurScope()->isTemplateParamScope(); 1251d33133cdc1af466f9c276249b2621be03867888bEli Friedman 1252d33133cdc1af466f9c276249b2621be03867888bEli Friedman // Add all of the parameters prior to this one (they don't 1253d33133cdc1af466f9c276249b2621be03867888bEli Friedman // have default arguments). 1254d33133cdc1af466f9c276249b2621be03867888bEli Friedman LateMethod->DefaultArgs.reserve(FTI.NumArgs); 1255d33133cdc1af466f9c276249b2621be03867888bEli Friedman for (unsigned I = 0; I < ParamIdx; ++I) 1256d33133cdc1af466f9c276249b2621be03867888bEli Friedman LateMethod->DefaultArgs.push_back( 12578f8210c47797f013e0fb24a26f9c4751b10783feDouglas Gregor LateParsedDefaultArgument(FTI.ArgInfo[I].Param)); 1258d33133cdc1af466f9c276249b2621be03867888bEli Friedman } 1259d33133cdc1af466f9c276249b2621be03867888bEli Friedman 1260d33133cdc1af466f9c276249b2621be03867888bEli Friedman // Add this parameter to the list of parameters (it or may 1261d33133cdc1af466f9c276249b2621be03867888bEli Friedman // not have a default argument). 1262d33133cdc1af466f9c276249b2621be03867888bEli Friedman LateMethod->DefaultArgs.push_back( 1263d33133cdc1af466f9c276249b2621be03867888bEli Friedman LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, 1264d33133cdc1af466f9c276249b2621be03867888bEli Friedman FTI.ArgInfo[ParamIdx].DefaultArgTokens)); 1265d33133cdc1af466f9c276249b2621be03867888bEli Friedman } 1266d33133cdc1af466f9c276249b2621be03867888bEli Friedman } 1267d33133cdc1af466f9c276249b2621be03867888bEli Friedman} 1268d33133cdc1af466f9c276249b2621be03867888bEli Friedman 12694cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. 12704cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// 12714cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// member-declaration: 12724cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// decl-specifier-seq[opt] member-declarator-list[opt] ';' 12734cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// function-definition ';'[opt] 12744cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] 12754cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// using-declaration [TODO] 1276511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson/// [C++0x] static_assert-declaration 12775aeccdbb4bdc94e48c04cacc59fa812af32109b2Anders Carlsson/// template-declaration 1278bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner/// [GNU] '__extension__' member-declaration 12794cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// 12804cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// member-declarator-list: 12814cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// member-declarator 12824cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// member-declarator-list ',' member-declarator 12834cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// 12844cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// member-declarator: 12854cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// declarator pure-specifier[opt] 12864cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// declarator constant-initializer[opt] 12874cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// identifier[opt] ':' constant-expression 12884cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// 1289e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl/// pure-specifier: 12904cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// '= 0' 12914cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// 12924cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// constant-initializer: 12934cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// '=' constant-expression 12944cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// 129537b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregorvoid Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, 1296c9068d7dd94d439cec66c421115d15303e481025John McCall const ParsedTemplateInfo &TemplateInfo, 1297c9068d7dd94d439cec66c421115d15303e481025John McCall ParsingDeclRAIIObject *TemplateDiags) { 129860fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall // Access declarations. 129960fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall if (!TemplateInfo.Kind && 130060fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) && 13019ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall !TryAnnotateCXXScopeToken() && 130260fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall Tok.is(tok::annot_cxxscope)) { 130360fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall bool isAccessDecl = false; 130460fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall if (NextToken().is(tok::identifier)) 130560fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall isAccessDecl = GetLookAheadToken(2).is(tok::semi); 130660fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall else 130760fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall isAccessDecl = NextToken().is(tok::kw_operator); 130860fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall 130960fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall if (isAccessDecl) { 131060fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall // Collect the scope specifier token we annotated earlier. 131160fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall CXXScopeSpec SS; 1312b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); 131360fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall 131460fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall // Try to parse an unqualified-id. 131560fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall UnqualifiedId Name; 1316b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) { 131760fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall SkipUntil(tok::semi); 131860fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall return; 131960fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall } 132060fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall 132160fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall // TODO: recover from mistakenly-qualified operator declarations. 132260fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall if (ExpectAndConsume(tok::semi, 132360fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall diag::err_expected_semi_after, 132460fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall "access declaration", 132560fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall tok::semi)) 132660fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall return; 132760fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall 132823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnUsingDeclaration(getCurScope(), AS, 132960fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall false, SourceLocation(), 133060fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall SS, Name, 133160fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall /* AttrList */ 0, 133260fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall /* IsTypeName */ false, 133360fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall SourceLocation()); 133460fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall return; 133560fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall } 133660fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall } 133760fa3cfd7aa63c29f9fc2d593bac56a3646337ccJohn McCall 1338511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson // static_assert-declaration 1339682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner if (Tok.is(tok::kw_static_assert)) { 134037b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor // FIXME: Check for templates 134197144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner SourceLocation DeclEnd; 134297144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner ParseStaticAssertDeclaration(DeclEnd); 1343682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return; 1344682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner } 13451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1346682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner if (Tok.is(tok::kw_template)) { 13471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(!TemplateInfo.TemplateParams && 134837b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor "Nested template improperly parsed?"); 134997144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner SourceLocation DeclEnd; 13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd, 13514d9a16f36d3b768672d50e6d02000f982ae448d7Douglas Gregor AS); 1352682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return; 1353682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner } 13545aeccdbb4bdc94e48c04cacc59fa812af32109b2Anders Carlsson 1355bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner // Handle: member-declaration ::= '__extension__' member-declaration 1356bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner if (Tok.is(tok::kw___extension__)) { 1357bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner // __extension__ silences extension warnings in the subexpression. 1358bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner ExtensionRAIIObject O(Diags); // Use RAII to do this. 1359bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner ConsumeToken(); 1360c9068d7dd94d439cec66c421115d15303e481025John McCall return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags); 1361bc8d56496a6ecdba14769df03d75c001184f8c54Chris Lattner } 13629cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor 13634ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it 13644ed5d91db256f7dbe6bf716da0b801004c197254Chris Lattner // is a bitfield. 1365a1efc8c8c6460d860d6509b05b341e77ed9bfe87Chris Lattner ColonProtectionRAIIObject X(*this); 1366193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 1367bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt CXX0XAttributeList AttrList; 1368bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // Optional C++0x attribute-specifier 1369a1efc8c8c6460d860d6509b05b341e77ed9bfe87Chris Lattner if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 1370bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttrList = ParseCXX0XAttributes(); 1371334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet if (getLang().Microsoft && Tok.is(tok::l_square)) 1372334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet ParseMicrosoftAttributes(); 1373bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 13749cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor if (Tok.is(tok::kw_using)) { 137537b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor // FIXME: Check for template aliases 1376193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 1377bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (AttrList.HasAttr) 1378bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed) 1379bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt << AttrList.Range; 13801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13819cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // Eat 'using'. 13829cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor SourceLocation UsingLoc = ConsumeToken(); 13839cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor 13849cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor if (Tok.is(tok::kw_namespace)) { 13859cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor Diag(UsingLoc, diag::err_using_namespace_in_class); 13869cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor SkipUntil(tok::semi, true, true); 1387ae50d501f463d7032320ec31840f60ae68df3a55Chris Lattner } else { 13889cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor SourceLocation DeclEnd; 13899cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // Otherwise, it must be using-declaration. 139078b810559d89e996e00684335407443936ce34a1John McCall ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo, 139178b810559d89e996e00684335407443936ce34a1John McCall UsingLoc, DeclEnd, AS); 13929cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor } 13939cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor return; 13949cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor } 13959cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor 13964cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis SourceLocation DSStart = Tok.getLocation(); 13974cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // decl-specifier-seq: 13984cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // Parse the common declaration-specifiers piece. 1399c9068d7dd94d439cec66c421115d15303e481025John McCall ParsingDeclSpec DS(*this, TemplateDiags); 1400bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt DS.AddAttributes(AttrList.AttrList); 140137b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class); 14024cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 1403f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall MultiTemplateParamsArg TemplateParams(Actions, 1404dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0, 1405dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); 1406dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall 14074cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis if (Tok.is(tok::semi)) { 14084cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis ConsumeToken(); 1409d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *TheDecl = 1410c9068d7dd94d439cec66c421115d15303e481025John McCall Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); 1411c9068d7dd94d439cec66c421115d15303e481025John McCall DS.complete(TheDecl); 141267d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall return; 14134cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis } 141407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 141554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext); 14164cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 14173a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis if (Tok.isNot(tok::colon)) { 1418a1efc8c8c6460d860d6509b05b341e77ed9bfe87Chris Lattner // Don't parse FOO:BAR as if it were a typo for FOO::BAR. 1419a1efc8c8c6460d860d6509b05b341e77ed9bfe87Chris Lattner ColonProtectionRAIIObject X(*this); 1420a1efc8c8c6460d860d6509b05b341e77ed9bfe87Chris Lattner 14213a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis // Parse the first declarator. 14223a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis ParseDeclarator(DeclaratorInfo); 14233a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis // Error parsing the declarator? 142410bd36882406cdf4805e35add1ce2f11ab9ae152Douglas Gregor if (!DeclaratorInfo.hasName()) { 14253a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis // If so, skip until the semi-colon or a }. 14264cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis SkipUntil(tok::r_brace, true); 14273a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis if (Tok.is(tok::semi)) 14283a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis ConsumeToken(); 1429682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return; 14304cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis } 14314cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 14321b2fc0f3e181d99fb34f60e711066fb11628ecd0John Thompson // If attributes exist after the declarator, but before an '{', parse them. 14331b2fc0f3e181d99fb34f60e711066fb11628ecd0John Thompson if (Tok.is(tok::kw___attribute)) { 14341b2fc0f3e181d99fb34f60e711066fb11628ecd0John Thompson SourceLocation Loc; 14351b2fc0f3e181d99fb34f60e711066fb11628ecd0John Thompson AttributeList *AttrList = ParseGNUAttributes(&Loc); 14361b2fc0f3e181d99fb34f60e711066fb11628ecd0John Thompson DeclaratorInfo.AddAttributes(AttrList, Loc); 14371b2fc0f3e181d99fb34f60e711066fb11628ecd0John Thompson } 14381b2fc0f3e181d99fb34f60e711066fb11628ecd0John Thompson 14393a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis // function-definition: 14407ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor if (Tok.is(tok::l_brace) 1441d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl || (DeclaratorInfo.isFunctionDeclarator() && 1442d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) { 14433a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis if (!DeclaratorInfo.isFunctionDeclarator()) { 14443a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis Diag(Tok, diag::err_func_def_no_params); 14453a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis ConsumeBrace(); 14463a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis SkipUntil(tok::r_brace, true); 1447682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return; 14483a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis } 14493a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis 14503a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 14513a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis Diag(Tok, diag::err_function_declared_typedef); 14523a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis // This recovery skips the entire function body. It would be nice 14533a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis // to simply call ParseCXXInlineMethodDef() below, however Sema 14543a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis // assumes the declarator represents a function, not a typedef. 14553a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis ConsumeBrace(); 14563a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis SkipUntil(tok::r_brace, true); 1457682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return; 14583a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis } 14594cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 146037b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo); 1461682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return; 14623a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis } 14634cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis } 14644cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 14654cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // member-declarator-list: 14664cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // member-declarator 14674cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // member-declarator-list ',' member-declarator 14684cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 1469d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall llvm::SmallVector<Decl *, 8> DeclsInGroup; 147060d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult BitfieldSize; 147160d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult Init; 1472e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl bool Deleted = false; 14734cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 14744cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis while (1) { 14754cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // member-declarator: 14764cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // declarator pure-specifier[opt] 14774cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // declarator constant-initializer[opt] 14784cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // identifier[opt] ':' constant-expression 14794cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis if (Tok.is(tok::colon)) { 14804cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis ConsumeToken(); 14810e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl BitfieldSize = ParseConstantExpression(); 14820e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl if (BitfieldSize.isInvalid()) 14834cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis SkipUntil(tok::comma, true, true); 14844cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis } 14851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14864cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // pure-specifier: 14874cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // '= 0' 14884cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // 14894cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // constant-initializer: 14904cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // '=' constant-expression 1491e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // 1492e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // defaulted/deleted function-definition: 1493e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // '=' 'default' [TODO] 1494e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // '=' 'delete' 14954cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis if (Tok.is(tok::equal)) { 14964cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis ConsumeToken(); 149737bf9d2bb74944c9d9a52522412bc077629977f1Anders Carlsson if (Tok.is(tok::kw_delete)) { 149837bf9d2bb74944c9d9a52522412bc077629977f1Anders Carlsson if (!getLang().CPlusPlus0x) 149937bf9d2bb74944c9d9a52522412bc077629977f1Anders Carlsson Diag(Tok, diag::warn_deleted_function_accepted_as_extension); 1500e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl ConsumeToken(); 1501e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl Deleted = true; 1502e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl } else { 1503e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl Init = ParseInitializer(); 1504e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl if (Init.isInvalid()) 1505e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl SkipUntil(tok::comma, true, true); 1506e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl } 15074cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis } 15084cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 1509e6563256a4b3b9fee70ce3335d28406607c1faafChris Lattner // If a simple-asm-expr is present, parse it. 1510e6563256a4b3b9fee70ce3335d28406607c1faafChris Lattner if (Tok.is(tok::kw_asm)) { 1511e6563256a4b3b9fee70ce3335d28406607c1faafChris Lattner SourceLocation Loc; 151260d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult AsmLabel(ParseSimpleAsm(&Loc)); 1513e6563256a4b3b9fee70ce3335d28406607c1faafChris Lattner if (AsmLabel.isInvalid()) 1514e6563256a4b3b9fee70ce3335d28406607c1faafChris Lattner SkipUntil(tok::comma, true, true); 1515e6563256a4b3b9fee70ce3335d28406607c1faafChris Lattner 1516e6563256a4b3b9fee70ce3335d28406607c1faafChris Lattner DeclaratorInfo.setAsmLabel(AsmLabel.release()); 1517e6563256a4b3b9fee70ce3335d28406607c1faafChris Lattner DeclaratorInfo.SetRangeEnd(Loc); 1518e6563256a4b3b9fee70ce3335d28406607c1faafChris Lattner } 1519e6563256a4b3b9fee70ce3335d28406607c1faafChris Lattner 15204cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // If attributes exist after the declarator, parse them. 1521ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl if (Tok.is(tok::kw___attribute)) { 1522ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl SourceLocation Loc; 1523bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttributeList *AttrList = ParseGNUAttributes(&Loc); 1524ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.AddAttributes(AttrList, Loc); 1525ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl } 15264cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 152707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis // NOTE: If Sema is the Action module and declarator is an instance field, 1528682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner // this call will *not* return the created decl; It will return null. 152907952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis // See Sema::ActOnCXXMemberDeclarator for details. 153067d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall 1531d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *ThisDecl = 0; 153267d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall if (DS.isFriendSpecified()) { 1533bbbcdd9cc06b6078939129330ecc9bda3310984dJohn McCall // TODO: handle initializers, bitfields, 'delete' 153423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, 1535bbbcdd9cc06b6078939129330ecc9bda3310984dJohn McCall /*IsDefinition*/ false, 1536bbbcdd9cc06b6078939129330ecc9bda3310984dJohn McCall move(TemplateParams)); 153737b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor } else { 153823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, 153967d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall DeclaratorInfo, 154037b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor move(TemplateParams), 154167d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall BitfieldSize.release(), 154267d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall Init.release(), 1543d1a7846699a82f85ff3ce6b2e383409537c3f5c5Sebastian Redl /*IsDefinition*/Deleted, 154467d1a67f3db2f1aa69083c5c94164d6e0ee05b32John McCall Deleted); 154537b372b76a3fafe77186d7e6079e5642e2017478Douglas Gregor } 1546682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner if (ThisDecl) 1547682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner DeclsInGroup.push_back(ThisDecl); 15484cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 154972b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor if (DeclaratorInfo.isFunctionDeclarator() && 15501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump DeclaratorInfo.getDeclSpec().getStorageClassSpec() 155172b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor != DeclSpec::SCS_typedef) { 1552d33133cdc1af466f9c276249b2621be03867888bEli Friedman HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl); 155372b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor } 155472b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor 155554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall DeclaratorInfo.complete(ThisDecl); 155654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall 15574cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // If we don't have a comma, it is either the end of the list (a ';') 15584cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // or an error, bail out. 15594cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis if (Tok.isNot(tok::comma)) 15604cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis break; 15611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15624cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // Consume the comma. 15634cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis ConsumeToken(); 15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15654cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // Parse the next declarator. 15664cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis DeclaratorInfo.clear(); 156715faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl BitfieldSize = 0; 156815faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl Init = 0; 1569e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl Deleted = false; 15701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15714cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // Attributes are only allowed on the second declarator. 1572ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl if (Tok.is(tok::kw___attribute)) { 1573ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl SourceLocation Loc; 1574bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttributeList *AttrList = ParseGNUAttributes(&Loc); 1575ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.AddAttributes(AttrList, Loc); 1576ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl } 15774cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 15783a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis if (Tok.isNot(tok::colon)) 15793a9fdb4742b21c0a3c27f18c5e4e94bab6f9e64cArgyrios Kyrtzidis ParseDeclarator(DeclaratorInfo); 15804cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis } 15814cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 1582ae50d501f463d7032320ec31840f60ae68df3a55Chris Lattner if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { 1583ae50d501f463d7032320ec31840f60ae68df3a55Chris Lattner // Skip to end of block or statement. 1584ae50d501f463d7032320ec31840f60ae68df3a55Chris Lattner SkipUntil(tok::r_brace, true, true); 1585ae50d501f463d7032320ec31840f60ae68df3a55Chris Lattner // If we stopped at a ';', eat it. 1586ae50d501f463d7032320ec31840f60ae68df3a55Chris Lattner if (Tok.is(tok::semi)) ConsumeToken(); 1587682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return; 15884cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis } 15894cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 159023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(), 1591ae50d501f463d7032320ec31840f60ae68df3a55Chris Lattner DeclsInGroup.size()); 15924cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis} 15934cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 15944cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXMemberSpecification - Parse the class definition. 15954cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// 15964cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// member-specification: 15974cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// member-declaration member-specification[opt] 15984cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// access-specifier ':' member-specification[opt] 15994cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// 16004cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidisvoid Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, 1601d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall unsigned TagType, Decl *TagDecl) { 160231fc07df7f0fc89ebf83ca05a20b29de45a7598dSanjiv Gupta assert((TagType == DeclSpec::TST_struct || 16034cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis TagType == DeclSpec::TST_union || 160431fc07df7f0fc89ebf83ca05a20b29de45a7598dSanjiv Gupta TagType == DeclSpec::TST_class) && "Invalid TagType!"); 16054cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 1606f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, 1607f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall "parsing struct/union/class body"); 16081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 160926997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor // Determine whether this is a non-nested class. Note that local 161026997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor // classes are *not* considered to be nested classes. 161126997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor bool NonNestedClass = true; 161226997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor if (!ClassStack.empty()) { 161323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor for (const Scope *S = getCurScope(); S; S = S->getParent()) { 161426997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor if (S->isClassScope()) { 161526997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor // We're inside a class scope, so this is a nested class. 161626997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor NonNestedClass = false; 161726997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor break; 161826997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor } 161926997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor 162026997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor if ((S->getFlags() & Scope::FnScope)) { 162126997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor // If we're in a function or function template declared in the 162226997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor // body of a class, then this is a local class rather than a 162326997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor // nested class. 162426997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor const Scope *Parent = S->getParent(); 162526997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor if (Parent->isTemplateParamScope()) 162626997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor Parent = Parent->getParent(); 162726997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor if (Parent->isClassScope()) 162826997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor break; 162926997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor } 163026997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor } 163126997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor } 16324cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 16334cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // Enter a scope for the class. 16343218c4bb3b5d7250f12420de6db7ef3e3f805a75Douglas Gregor ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); 16354cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 16366569d68745c8213709740337d2be52b031384f58Douglas Gregor // Note that we are parsing a new (potentially-nested) class definition. 163726997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass); 16386569d68745c8213709740337d2be52b031384f58Douglas Gregor 1639ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor if (TagDecl) 164023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); 1641bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall 1642bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall if (Tok.is(tok::colon)) { 1643bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall ParseBaseClause(TagDecl); 1644bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall 1645bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall if (!Tok.is(tok::l_brace)) { 1646bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall Diag(Tok, diag::err_expected_lbrace_after_base_specifiers); 1647db7bb4a4e7d9744cbc994c90932e6f056228e1ffJohn McCall 1648db7bb4a4e7d9744cbc994c90932e6f056228e1ffJohn McCall if (TagDecl) 164923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); 1650bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall return; 1651bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall } 1652bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall } 1653bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall 1654bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall assert(Tok.is(tok::l_brace)); 1655bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall 1656bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall SourceLocation LBraceLoc = ConsumeBrace(); 1657bd0dfa5c37d422427080a0ae3af9b63e70e6e854John McCall 165842a4f66ffeb26e69b2f0ec873a5d41b0e39e634cJohn McCall if (TagDecl) 165923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc); 1660f9368159334ff86ea5fa367225c1a580977f3b03John McCall 16614cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // C++ 11p3: Members of a class defined with the keyword class are private 16624cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // by default. Members of a class defined with the keywords struct or union 16634cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // are public by default. 16644cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis AccessSpecifier CurAS; 16654cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis if (TagType == DeclSpec::TST_class) 16664cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis CurAS = AS_private; 16674cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis else 16684cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis CurAS = AS_public; 16694cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 167007976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor SourceLocation RBraceLoc; 167107976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor if (TagDecl) { 167207976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor // While we still have something to read, read the member-declarations. 167307976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 167407976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor // Each iteration of this loop reads one member-declaration. 167507976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor 167607976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor // Check for extraneous top-level semicolon. 167707976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor if (Tok.is(tok::semi)) { 167807976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor Diag(Tok, diag::ext_extra_struct_semi) 167907976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) 168007976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor << FixItHint::CreateRemoval(Tok.getLocation()); 168107976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor ConsumeToken(); 168207976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor continue; 168307976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor } 16841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 168507976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor AccessSpecifier AS = getAccessSpecifierIfPresent(); 168607976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor if (AS != AS_none) { 168707976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor // Current token is a C++ access specifier. 168807976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor CurAS = AS; 168907976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor SourceLocation ASLoc = Tok.getLocation(); 169007976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor ConsumeToken(); 169107976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor if (Tok.is(tok::colon)) 169207976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation()); 169307976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor else 169407976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor Diag(Tok, diag::err_expected_colon); 169507976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor ConsumeToken(); 169607976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor continue; 169707976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor } 16984cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 169907976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor // FIXME: Make sure we don't have a template here. 17004cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 170107976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor // Parse all the comma separated declarators. 170207976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor ParseCXXClassMemberDeclaration(CurAS); 170307976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor } 17041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 170507976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); 170607976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor } else { 170707976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor SkipUntil(tok::r_brace, false, false); 17084cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis } 17091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17104cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // If attributes exist after class contents, parse them. 17118113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek AttributeList *AttrList = 0; 17124cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis if (Tok.is(tok::kw___attribute)) 17138113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek AttrList = ParseGNUAttributes(); 17144cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 171542a4f66ffeb26e69b2f0ec873a5d41b0e39e634cJohn McCall if (TagDecl) 171623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, 171742a4f66ffeb26e69b2f0ec873a5d41b0e39e634cJohn McCall LBraceLoc, RBraceLoc, 17188113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek AttrList); 17194cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 17204cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // C++ 9.2p2: Within the class member-specification, the class is regarded as 17214cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // complete within function bodies, default arguments, 17224cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // exception-specifications, and constructor ctor-initializers (including 17234cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // such things in nested classes). 17244cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // 172572b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor // FIXME: Only function bodies and constructor ctor-initializers are 172672b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor // parsed correctly, fix the rest. 172707976d25eda0fed2734518be022ee84fab898cc6Douglas Gregor if (TagDecl && NonNestedClass) { 17284cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // We are not inside a nested class. This class and its nested classes 172972b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor // are complete and we can parse the delayed portions of method 173072b505b7904b3c9320a1312998800ba76e4f5841Douglas Gregor // declarations and the lexed inline method definitions. 1731e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor SourceLocation SavedPrevTokLocation = PrevTokLocation; 17326569d68745c8213709740337d2be52b031384f58Douglas Gregor ParseLexedMethodDeclarations(getCurrentClass()); 17336569d68745c8213709740337d2be52b031384f58Douglas Gregor ParseLexedMethodDefs(getCurrentClass()); 1734e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor PrevTokLocation = SavedPrevTokLocation; 17354cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis } 17364cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 173742a4f66ffeb26e69b2f0ec873a5d41b0e39e634cJohn McCall if (TagDecl) 173823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc); 1739db7bb4a4e7d9744cbc994c90932e6f056228e1ffJohn McCall 17404cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis // Leave the class scope. 17416569d68745c8213709740337d2be52b031384f58Douglas Gregor ParsingDef.Pop(); 17428935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor ClassScope.Exit(); 17434cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis} 17447ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor 17457ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// ParseConstructorInitializer - Parse a C++ constructor initializer, 17467ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// which explicitly initializes the members or base classes of a 17477ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// class (C++ [class.base.init]). For example, the three initializers 17487ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// after the ':' in the Derived constructor below: 17497ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// 17507ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// @code 17517ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// class Base { }; 17527ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// class Derived : Base { 17537ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// int x; 17547ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// float f; 17557ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// public: 17567ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// Derived(float f) : Base(), x(17), f(f) { } 17577ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// }; 17587ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// @endcode 17597ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// 17601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// [C++] ctor-initializer: 17611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// ':' mem-initializer-list 17627ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// 17631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// [C++] mem-initializer-list: 17641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// mem-initializer 17651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// mem-initializer , mem-initializer-list 1766d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { 17677ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); 17687ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor 17697ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor SourceLocation ColonLoc = ConsumeToken(); 17701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1771ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall llvm::SmallVector<CXXBaseOrMemberInitializer*, 4> MemInitializers; 17729db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor bool AnyErrors = false; 1773193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 17747ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor do { 17750133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor if (Tok.is(tok::code_completion)) { 17760133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor Actions.CodeCompleteConstructorInitializer(ConstructorDecl, 17770133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor MemInitializers.data(), 17780133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor MemInitializers.size()); 17790133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor ConsumeCodeCompletionToken(); 17800133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor } else { 17810133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); 17820133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor if (!MemInit.isInvalid()) 17830133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor MemInitializers.push_back(MemInit.get()); 17840133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor else 17850133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor AnyErrors = true; 17860133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor } 17870133f525a23e18dd444880f7554f25fbcbd834e5Douglas Gregor 17887ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor if (Tok.is(tok::comma)) 17897ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor ConsumeToken(); 17907ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor else if (Tok.is(tok::l_brace)) 17917ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor break; 1792b1f6fa48960eae269a3931d1fc545ed468d9a4d2Douglas Gregor // If the next token looks like a base or member initializer, assume that 1793b1f6fa48960eae269a3931d1fc545ed468d9a4d2Douglas Gregor // we're just missing a comma. 1794751f6922376dfe9432795b65a3649179e4ef5cf5Douglas Gregor else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { 1795751f6922376dfe9432795b65a3649179e4ef5cf5Douglas Gregor SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); 1796751f6922376dfe9432795b65a3649179e4ef5cf5Douglas Gregor Diag(Loc, diag::err_ctor_init_missing_comma) 1797751f6922376dfe9432795b65a3649179e4ef5cf5Douglas Gregor << FixItHint::CreateInsertion(Loc, ", "); 1798751f6922376dfe9432795b65a3649179e4ef5cf5Douglas Gregor } else { 17997ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor // Skip over garbage, until we get to '{'. Don't eat the '{'. 1800d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); 18017ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor SkipUntil(tok::l_brace, true, true); 18027ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor break; 18037ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor } 18047ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor } while (true); 18057ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor 18061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, 18079db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor MemInitializers.data(), MemInitializers.size(), 18089db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor AnyErrors); 18097ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor} 18107ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor 18117ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// ParseMemInitializer - Parse a C++ member initializer, which is 18127ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// part of a constructor initializer that explicitly initializes one 18137ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// member or base class (C++ [class.base.init]). See 18147ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// ParseConstructorInitializer for an example. 18157ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// 18167ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] mem-initializer: 18177ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// mem-initializer-id '(' expression-list[opt] ')' 18181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 18197ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] mem-initializer-id: 18207ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// '::'[opt] nested-name-specifier[opt] class-name 18217ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// identifier 1822d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallParser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { 1823bcfad54a43e5570e09daddd976bd4545933e75b1Fariborz Jahanian // parse '::'[opt] nested-name-specifier[opt] 1824bcfad54a43e5570e09daddd976bd4545933e75b1Fariborz Jahanian CXXScopeSpec SS; 1825b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); 1826b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType TemplateTypeTy; 1827961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian if (Tok.is(tok::annot_template_id)) { 1828961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian TemplateIdAnnotation *TemplateId 1829961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 1830d9b600c1a589200be905c53e2e10fceb57efa18dDouglas Gregor if (TemplateId->Kind == TNK_Type_template || 1831d9b600c1a589200be905c53e2e10fceb57efa18dDouglas Gregor TemplateId->Kind == TNK_Dependent_template_name) { 1832961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian AnnotateTemplateIdTokenAsType(&SS); 1833961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); 1834b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall TemplateTypeTy = getTypeAnnotation(Tok); 1835961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian } 1836961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian } 1837961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian if (!TemplateTypeTy && Tok.isNot(tok::identifier)) { 18381ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner Diag(Tok, diag::err_expected_member_or_base_name); 18397ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor return true; 18407ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor } 18411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18427ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor // Get the identifier. This may be a member name or a class name, 18437ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor // but we'll let the semantic analysis determine which it is. 1844961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0; 18457ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor SourceLocation IdLoc = ConsumeToken(); 18467ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor 18477ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor // Parse the '('. 18487ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor if (Tok.isNot(tok::l_paren)) { 18491ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner Diag(Tok, diag::err_expected_lparen); 18507ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor return true; 18517ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor } 18527ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor SourceLocation LParenLoc = ConsumeParen(); 18537ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor 18547ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor // Parse the optional expression-list. 1855a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl ExprVector ArgExprs(Actions); 18567ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor CommaLocsTy CommaLocs; 18577ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { 18587ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor SkipUntil(tok::r_paren); 18597ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor return true; 18607ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor } 18617ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor 18627ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 18637ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor 186423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, 1865961743326fd18776f897bf4461345dba680ef637Fariborz Jahanian TemplateTypeTy, IdLoc, 1866a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl LParenLoc, ArgExprs.take(), 1867a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor ArgExprs.size(), RParenLoc); 18687ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor} 18690fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor 18700fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor/// ParseExceptionSpecification - Parse a C++ exception-specification 18710fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor/// (C++ [except.spec]). 18720fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor/// 1873a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor/// exception-specification: 1874a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor/// 'throw' '(' type-id-list [opt] ')' 1875a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor/// [MS] 'throw' '(' '...' ')' 18761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 1877a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor/// type-id-list: 1878a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor/// type-id 1879a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor/// type-id-list ',' type-id 18800fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor/// 18817dc813462dd9fd3f6f4296f896a12de14264fef8Sebastian Redlbool Parser::ParseExceptionSpecification(SourceLocation &EndLoc, 1882b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall llvm::SmallVectorImpl<ParsedType> 1883ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl &Exceptions, 1884b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall llvm::SmallVectorImpl<SourceRange> 1885ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl &Ranges, 18867dc813462dd9fd3f6f4296f896a12de14264fef8Sebastian Redl bool &hasAnyExceptionSpec) { 18870fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor assert(Tok.is(tok::kw_throw) && "expected throw"); 18881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18890fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor SourceLocation ThrowLoc = ConsumeToken(); 18901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18910fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor if (!Tok.is(tok::l_paren)) { 18920fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor return Diag(Tok, diag::err_expected_lparen_after) << "throw"; 18930fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor } 18940fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor SourceLocation LParenLoc = ConsumeParen(); 18950fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor 1896a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor // Parse throw(...), a Microsoft extension that means "this function 1897a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor // can throw anything". 1898a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor if (Tok.is(tok::ellipsis)) { 18997dc813462dd9fd3f6f4296f896a12de14264fef8Sebastian Redl hasAnyExceptionSpec = true; 1900a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor SourceLocation EllipsisLoc = ConsumeToken(); 1901a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor if (!getLang().Microsoft) 1902a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); 1903ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 1904a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor return false; 1905a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor } 1906a4745616ebe36ba7699f18618382e764aa8183a1Douglas Gregor 19070fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor // Parse the sequence of type-ids. 1908ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl SourceRange Range; 19090fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor while (Tok.isNot(tok::r_paren)) { 1910ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl TypeResult Res(ParseTypeName(&Range)); 1911ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl if (!Res.isInvalid()) { 19127dc813462dd9fd3f6f4296f896a12de14264fef8Sebastian Redl Exceptions.push_back(Res.get()); 1913ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl Ranges.push_back(Range); 1914ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl } 19150fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor if (Tok.is(tok::comma)) 19160fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor ConsumeToken(); 19177dc813462dd9fd3f6f4296f896a12de14264fef8Sebastian Redl else 19180fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor break; 19190fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor } 19200fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor 1921ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 19220fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor return false; 19230fe7bea6fca9737c6c145aaa4a2ad3abe595782aDouglas Gregor} 19246569d68745c8213709740337d2be52b031384f58Douglas Gregor 1925dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor/// ParseTrailingReturnType - Parse a trailing return type on a new-style 1926dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor/// function declaration. 1927dab60ad68a3a98d687305941a3852e793705f945Douglas GregorTypeResult Parser::ParseTrailingReturnType() { 1928dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor assert(Tok.is(tok::arrow) && "expected arrow"); 1929dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor 1930dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor ConsumeToken(); 1931dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor 1932dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor // FIXME: Need to suppress declarations when parsing this typename. 1933dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor // Otherwise in this function definition: 1934dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor // 1935dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor // auto f() -> struct X {} 1936dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor // 1937dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor // struct X is parsed as class definition because of the trailing 1938dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor // brace. 1939dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor 1940dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor SourceRange Range; 1941dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor return ParseTypeName(&Range); 1942dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor} 1943dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor 19446569d68745c8213709740337d2be52b031384f58Douglas Gregor/// \brief We have just started parsing the definition of a new class, 19456569d68745c8213709740337d2be52b031384f58Douglas Gregor/// so push that class onto our stack of classes that is currently 19466569d68745c8213709740337d2be52b031384f58Douglas Gregor/// being parsed. 1947d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) { 194826997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor assert((NonNestedClass || !ClassStack.empty()) && 19496569d68745c8213709740337d2be52b031384f58Douglas Gregor "Nested class without outer class"); 195026997fd58c9560584edd154618f2f2c15ee68af4Douglas Gregor ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass)); 19516569d68745c8213709740337d2be52b031384f58Douglas Gregor} 19526569d68745c8213709740337d2be52b031384f58Douglas Gregor 19536569d68745c8213709740337d2be52b031384f58Douglas Gregor/// \brief Deallocate the given parsed class and all of its nested 19546569d68745c8213709740337d2be52b031384f58Douglas Gregor/// classes. 19556569d68745c8213709740337d2be52b031384f58Douglas Gregorvoid Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { 1956d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I) 1957d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor delete Class->LateParsedDeclarations[I]; 19586569d68745c8213709740337d2be52b031384f58Douglas Gregor delete Class; 19596569d68745c8213709740337d2be52b031384f58Douglas Gregor} 19606569d68745c8213709740337d2be52b031384f58Douglas Gregor 19616569d68745c8213709740337d2be52b031384f58Douglas Gregor/// \brief Pop the top class of the stack of classes that are 19626569d68745c8213709740337d2be52b031384f58Douglas Gregor/// currently being parsed. 19636569d68745c8213709740337d2be52b031384f58Douglas Gregor/// 19646569d68745c8213709740337d2be52b031384f58Douglas Gregor/// This routine should be called when we have finished parsing the 19656569d68745c8213709740337d2be52b031384f58Douglas Gregor/// definition of a class, but have not yet popped the Scope 19666569d68745c8213709740337d2be52b031384f58Douglas Gregor/// associated with the class's definition. 19676569d68745c8213709740337d2be52b031384f58Douglas Gregor/// 19686569d68745c8213709740337d2be52b031384f58Douglas Gregor/// \returns true if the class we've popped is a top-level class, 19696569d68745c8213709740337d2be52b031384f58Douglas Gregor/// false otherwise. 19706569d68745c8213709740337d2be52b031384f58Douglas Gregorvoid Parser::PopParsingClass() { 19716569d68745c8213709740337d2be52b031384f58Douglas Gregor assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); 19721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19736569d68745c8213709740337d2be52b031384f58Douglas Gregor ParsingClass *Victim = ClassStack.top(); 19746569d68745c8213709740337d2be52b031384f58Douglas Gregor ClassStack.pop(); 19756569d68745c8213709740337d2be52b031384f58Douglas Gregor if (Victim->TopLevelClass) { 19766569d68745c8213709740337d2be52b031384f58Douglas Gregor // Deallocate all of the nested classes of this class, 19776569d68745c8213709740337d2be52b031384f58Douglas Gregor // recursively: we don't need to keep any of this information. 19786569d68745c8213709740337d2be52b031384f58Douglas Gregor DeallocateParsedClasses(Victim); 19796569d68745c8213709740337d2be52b031384f58Douglas Gregor return; 19801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 19816569d68745c8213709740337d2be52b031384f58Douglas Gregor assert(!ClassStack.empty() && "Missing top-level class?"); 19826569d68745c8213709740337d2be52b031384f58Douglas Gregor 1983d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor if (Victim->LateParsedDeclarations.empty()) { 19846569d68745c8213709740337d2be52b031384f58Douglas Gregor // The victim is a nested class, but we will not need to perform 19856569d68745c8213709740337d2be52b031384f58Douglas Gregor // any processing after the definition of this class since it has 19866569d68745c8213709740337d2be52b031384f58Douglas Gregor // no members whose handling was delayed. Therefore, we can just 19876569d68745c8213709740337d2be52b031384f58Douglas Gregor // remove this nested class. 1988d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor DeallocateParsedClasses(Victim); 19896569d68745c8213709740337d2be52b031384f58Douglas Gregor return; 19906569d68745c8213709740337d2be52b031384f58Douglas Gregor } 19916569d68745c8213709740337d2be52b031384f58Douglas Gregor 19926569d68745c8213709740337d2be52b031384f58Douglas Gregor // This nested class has some members that will need to be processed 19936569d68745c8213709740337d2be52b031384f58Douglas Gregor // after the top-level class is completely defined. Therefore, add 19946569d68745c8213709740337d2be52b031384f58Douglas Gregor // it to the list of nested classes within its parent. 199523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor assert(getCurScope()->isClassScope() && "Nested class outside of class scope?"); 1996d54eb4410330383f48d3cc22b2ad8d23f120836bDouglas Gregor ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim)); 199723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope(); 19986569d68745c8213709740337d2be52b031384f58Douglas Gregor} 1999bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2000bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only 2001bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// parses standard attributes. 2002bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2003bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] attribute-specifier: 2004bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// '[' '[' attribute-list ']' ']' 2005bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2006bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] attribute-list: 2007bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// attribute[opt] 2008bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// attribute-list ',' attribute[opt] 2009bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2010bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] attribute: 2011bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// attribute-token attribute-argument-clause[opt] 2012bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2013bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] attribute-token: 2014bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// identifier 2015bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// attribute-scoped-token 2016bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2017bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] attribute-scoped-token: 2018bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// attribute-namespace '::' identifier 2019bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2020bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] attribute-namespace: 2021bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// identifier 2022bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2023bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] attribute-argument-clause: 2024bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// '(' balanced-token-seq ')' 2025bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2026bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] balanced-token-seq: 2027bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// balanced-token 2028bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// balanced-token-seq balanced-token 2029bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2030bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] balanced-token: 2031bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// '(' balanced-token-seq ')' 2032bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// '[' balanced-token-seq ']' 2033bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// '{' balanced-token-seq '}' 2034bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// any token but '(', ')', '[', ']', '{', or '}' 2035bbd37c62e34db3f5a95c899723484a76c71d7757Sean HuntCXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) { 2036bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) 2037bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt && "Not a C++0x attribute list"); 2038bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2039bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt SourceLocation StartLoc = Tok.getLocation(), Loc; 2040bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttributeList *CurrAttr = 0; 2041bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2042bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ConsumeBracket(); 2043bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ConsumeBracket(); 2044193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 2045bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (Tok.is(tok::comma)) { 2046bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Diag(Tok.getLocation(), diag::err_expected_ident); 2047bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ConsumeToken(); 2048bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2049bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2050bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt while (Tok.is(tok::identifier) || Tok.is(tok::comma)) { 2051bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // attribute not present 2052bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (Tok.is(tok::comma)) { 2053bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ConsumeToken(); 2054bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt continue; 2055bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2056bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2057bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo(); 2058bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt SourceLocation ScopeLoc, AttrLoc = ConsumeToken(); 2059193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 2060bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // scoped attribute 2061bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (Tok.is(tok::coloncolon)) { 2062bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ConsumeToken(); 2063bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2064bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (!Tok.is(tok::identifier)) { 2065bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Diag(Tok.getLocation(), diag::err_expected_ident); 2066bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt SkipUntil(tok::r_square, tok::comma, true, true); 2067bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt continue; 2068bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2069193575455e00eca03fd7177f60e3f2e6263cb661Kovarththanan Rajaratnam 2070bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ScopeName = AttrName; 2071bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ScopeLoc = AttrLoc; 2072bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2073bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttrName = Tok.getIdentifierInfo(); 2074bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttrLoc = ConsumeToken(); 2075bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2076bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2077bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt bool AttrParsed = false; 2078bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // No scoped names are supported; ideally we could put all non-standard 2079bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // attributes into namespaces. 2080bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (!ScopeName) { 2081bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt switch(AttributeList::getKind(AttrName)) 2082bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt { 2083bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // No arguments 20847725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt case AttributeList::AT_base_check: 20857725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt case AttributeList::AT_carries_dependency: 2086bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt case AttributeList::AT_final: 20877725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt case AttributeList::AT_hiding: 20887725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt case AttributeList::AT_noreturn: 20897725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt case AttributeList::AT_override: { 2090bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (Tok.is(tok::l_paren)) { 2091bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments) 2092bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt << AttrName->getName(); 2093bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt break; 2094bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2095bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 20968113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek CurrAttr = AttrFactory.Create(AttrName, AttrLoc, 0, AttrLoc, 0, 20978113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek SourceLocation(), 0, 0, CurrAttr, false, 20988113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek true); 2099bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttrParsed = true; 2100bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt break; 2101bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2102bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2103bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // One argument; must be a type-id or assignment-expression 2104bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt case AttributeList::AT_aligned: { 2105bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (Tok.isNot(tok::l_paren)) { 2106bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments) 2107bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt << AttrName->getName(); 2108bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt break; 2109bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2110bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt SourceLocation ParamLoc = ConsumeParen(); 2111bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 211260d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc); 2113bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2114bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt MatchRHSPunctuation(tok::r_paren, ParamLoc); 2115bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2116bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ExprVector ArgExprs(Actions); 2117bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ArgExprs.push_back(ArgExpr.release()); 21188113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek CurrAttr = AttrFactory.Create(AttrName, AttrLoc, 0, AttrLoc, 21198113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek 0, ParamLoc, ArgExprs.take(), 1, CurrAttr, 21208113ecfa4e41e2c888b1794389dfe3bce6386493Ted Kremenek false, true); 2121bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2122bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttrParsed = true; 2123bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt break; 2124bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2125bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2126bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // Silence warnings 2127bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt default: break; 2128bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2129bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2130bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2131bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // Skip the entire parameter clause, if any 2132bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (!AttrParsed && Tok.is(tok::l_paren)) { 2133bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt ConsumeParen(); 2134bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt // SkipUntil maintains the balancedness of tokens. 2135bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt SkipUntil(tok::r_paren, false); 2136bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2137bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2138bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2139bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) 2140bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt SkipUntil(tok::r_square, false); 2141bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt Loc = Tok.getLocation(); 2142bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) 2143bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt SkipUntil(tok::r_square, false); 2144bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2145bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true); 2146bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt return Attr; 2147bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt} 2148bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 2149bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]] 2150bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// attribute. 2151bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2152bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// FIXME: Simply returns an alignof() expression if the argument is a 2153bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// type. Ideally, the type should be propagated directly into Sema. 2154bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// 2155bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] 'align' '(' type-id ')' 2156bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt/// [C++0x] 'align' '(' assignment-expression ')' 215760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) { 2158bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (isTypeIdInParens()) { 2159f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); 2160bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt SourceLocation TypeLoc = Tok.getLocation(); 2161b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType Ty = ParseTypeName().get(); 2162bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt SourceRange TypeRange(Start, Tok.getLocation()); 2163b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, 2164b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall Ty.getAsOpaquePtr(), TypeRange); 2165bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } else 2166bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt return ParseConstantExpression(); 2167bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt} 2168334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet 2169334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr] 2170334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// 2171334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// [MS] ms-attribute: 2172334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// '[' token-seq ']' 2173334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// 2174334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// [MS] ms-attribute-seq: 2175334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// ms-attribute[opt] 2176334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet/// ms-attribute ms-attribute-seq 2177334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichetvoid Parser::ParseMicrosoftAttributes() { 2178334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); 2179334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet 2180334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet while (Tok.is(tok::l_square)) { 2181334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet ConsumeBracket(); 2182334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet SkipUntil(tok::r_square, true, true); 2183334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); 2184334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet } 2185334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet} 2186