ParseExprCXX.cpp revision 34b41d939a1328f484511c6002ba2456db879a29
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements the Expression parsing implementation for C++. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h" 155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h" 16bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor#include "RAIIObjectsForParser.h" 1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h" 1819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h" 193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h" 203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present. 242dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// 252dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which 261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or 272dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream. 28eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 29eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::'[opt] nested-name-specifier 30eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' 31eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 32eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// nested-name-specifier: 33eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// type-name '::' 34eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace-name '::' 35eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// nested-name-specifier identifier '::' 362dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier 'template'[opt] simple-template-id '::' 372dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// 382dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// 391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed 402dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty) 412dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// 421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following 432dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the 442dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed. 45eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 462dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of 472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it. 482dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// 49d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param MayBePseudoDestructor When non-NULL, points to a flag that 50d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// indicates whether this nested-name-specifier may be part of a 51d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// pseudo-destructor name. In this case, the flag will be set false 52d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we don't actually end up parsing a destructor name. Moreorover, 53d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we do end up determining that we are parsing a destructor name, 54d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// the last component of the nested-name-specifier is not parsed as 55d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// part of the scope specifier. 56d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor 57b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor/// member access expression, e.g., the \p T:: in \p p->T::m. 58b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor/// 599ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// \returns true if there was an error parsing a scope specifier 60495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS, 61b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType ObjectType, 62b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor bool EnteringContext, 63d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor bool *MayBePseudoDestructor) { 644bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis assert(getLang().CPlusPlus && 657452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner "Call sites of this function should be guarded by checking for C++"); 661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 67eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis if (Tok.is(tok::annot_cxxscope)) { 68ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall SS.setScopeRep(static_cast<NestedNameSpecifier*>(Tok.getAnnotationValue())); 69eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis SS.setRange(Tok.getAnnotationRange()); 70eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis ConsumeToken(); 719ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 72eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis } 73e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner 7439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor bool HasScopeSpecifier = false; 7539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor 765b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner if (Tok.is(tok::coloncolon)) { 775b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner // ::new and ::delete aren't nested-name-specifiers. 785b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner tok::TokenKind NextKind = NextToken().getKind(); 795b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner if (NextKind == tok::kw_new || NextKind == tok::kw_delete) 805b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner return false; 811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner // '::' - Global scope qualifier. 83357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner SourceLocation CCLoc = ConsumeToken(); 84357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner SS.setBeginLoc(CCLoc); 8523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), CCLoc)); 86357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner SS.setEndLoc(CCLoc); 8739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor HasScopeSpecifier = true; 88eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis } 89eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 90d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor bool CheckForDestructor = false; 91d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor if (MayBePseudoDestructor && *MayBePseudoDestructor) { 92d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor CheckForDestructor = true; 93d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor *MayBePseudoDestructor = false; 94d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor } 95d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor 9639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor while (true) { 972dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor if (HasScopeSpecifier) { 982dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // C++ [basic.lookup.classref]p5: 992dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // If the qualified-id has the form 1003b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor // 1012dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // ::class-name-or-namespace-name::... 1023b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor // 1032dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // the class-name-or-namespace-name is looked up in global scope as a 1042dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // class-name or namespace-name. 1052dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // 1062dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // To implement this, we clear out the object type as soon as we've 1072dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // seen a leading '::' or part of a nested-name-specifier. 108b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ObjectType = ParsedType(); 10981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor 11081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor if (Tok.is(tok::code_completion)) { 11181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor // Code completion for a nested-name-specifier, where the code 11281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor // code completion token follows the '::'. 11323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext); 114dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor ConsumeCodeCompletionToken(); 11581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor } 1162dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor } 1171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // nested-name-specifier: 11977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner // nested-name-specifier 'template'[opt] simple-template-id '::' 12077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner 12177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner // Parse the optional 'template' keyword, then make sure we have 12277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner // 'identifier <' after it. 12377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner if (Tok.is(tok::kw_template)) { 1242dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // If we don't have a scope specifier or an object type, this isn't a 125eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman // nested-name-specifier, since they aren't allowed to start with 126eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman // 'template'. 1272dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor if (!HasScopeSpecifier && !ObjectType) 128eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman break; 129eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman 1307bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TentativeParsingAction TPA(*this); 13177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner SourceLocation TemplateKWLoc = ConsumeToken(); 132ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 133ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor UnqualifiedId TemplateName; 134ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (Tok.is(tok::identifier)) { 135ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the identifier. 1367bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 137ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor ConsumeToken(); 138ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } else if (Tok.is(tok::kw_operator)) { 139ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, 1407bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TemplateName)) { 1417bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TPA.Commit(); 142ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 1437bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor } 144ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 145e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId && 146e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) { 147ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Diag(TemplateName.getSourceRange().getBegin(), 148ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor diag::err_id_after_template_in_nested_name_spec) 149ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor << TemplateName.getSourceRange(); 1507bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TPA.Commit(); 151ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 152ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 153ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } else { 1547bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TPA.Revert(); 15577cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner break; 15677cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner } 1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1587bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor // If the next token is not '<', we have a qualified-id that refers 1597bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor // to a template name, such as T::template apply, but is not a 1607bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor // template-id. 1617bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor if (Tok.isNot(tok::less)) { 1627bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TPA.Revert(); 1637bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor break; 1647bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor } 1657bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor 1667bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor // Commit to parsing the template-id. 1677bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TPA.Commit(); 168d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor TemplateTy Template; 16923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(), 170d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor TemplateKWLoc, 171d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor SS, 172d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor TemplateName, 173d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor ObjectType, 174d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor EnteringContext, 175d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor Template)) { 176d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName, 177d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor TemplateKWLoc, false)) 178d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor return true; 179d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor } else 1809ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return true; 1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner continue; 18377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner } 1841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) { 1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // We have 18739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // 18839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // simple-template-id '::' 18939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // 19039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // So we need to check whether the simple-template-id is of the 191c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor // right kind (it should name a type or be dependent), and then 192c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor // convert it into a type within the nested-name-specifier. 1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateIdAnnotation *TemplateId 19439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 195d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) { 196d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor *MayBePseudoDestructor = true; 1979ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 198d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor } 199d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor 2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (TemplateId->Kind == TNK_Type_template || 201c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor TemplateId->Kind == TNK_Dependent_template_name) { 20231a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor AnnotateTemplateIdTokenAsType(&SS); 20339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor 2041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(Tok.is(tok::annot_typename) && 20539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor "AnnotateTemplateIdTokenAsType isn't working"); 20639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor Token TypeToken = Tok; 20739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor ConsumeToken(); 20839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); 20939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor SourceLocation CCLoc = ConsumeToken(); 2101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor if (!HasScopeSpecifier) { 21239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor SS.setBeginLoc(TypeToken.getLocation()); 21339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor HasScopeSpecifier = true; 21439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor } 2151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 216b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall if (ParsedType T = getTypeAnnotation(TypeToken)) { 217b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall CXXScopeTy *Scope = 218b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, T, 21931a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor TypeToken.getAnnotationRange(), 220b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall CCLoc); 221b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall SS.setScopeRep(Scope); 222b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall } else 22331a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor SS.setScopeRep(0); 22439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor SS.setEndLoc(CCLoc); 22539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor continue; 22667b9e831943300ce54e564e601971828ce4def15Chris Lattner } 2271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22867b9e831943300ce54e564e601971828ce4def15Chris Lattner assert(false && "FIXME: Only type template names supported here"); 22939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor } 23039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor 2315c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner 2325c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // The rest of the nested-name-specifier possibilities start with 2335c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // tok::identifier. 2345c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner if (Tok.isNot(tok::identifier)) 2355c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner break; 2365c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner 2375c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner IdentifierInfo &II = *Tok.getIdentifierInfo(); 2385c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner 2395c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // nested-name-specifier: 2405c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // type-name '::' 2415c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // namespace-name '::' 2425c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // nested-name-specifier identifier '::' 2435c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner Token Next = NextToken(); 24446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner 24546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover 24646646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner // and emit a fixit hint for it. 247b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor if (Next.is(tok::colon) && !ColonIsSacred) { 24823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II, ObjectType, 249b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor EnteringContext) && 250b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor // If the token after the colon isn't an identifier, it's still an 251b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor // error, but they probably meant something else strange so don't 252b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor // recover like this. 253b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor PP.LookAhead(1).is(tok::identifier)) { 254b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor Diag(Next, diag::err_unexected_colon_in_nested_name_spec) 255849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor << FixItHint::CreateReplacement(Next.getLocation(), "::"); 256b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor 257b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor // Recover as if the user wrote '::'. 258b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor Next.setKind(tok::coloncolon); 259b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor } 26046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner } 26146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner 2625c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner if (Next.is(tok::coloncolon)) { 26377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) && 26423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(), 26577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor II, ObjectType)) { 266d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor *MayBePseudoDestructor = true; 2679ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 268d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor } 269d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor 2705c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // We have an identifier followed by a '::'. Lookup this name 2715c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // as the name in a nested-name-specifier. 2725c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner SourceLocation IdLoc = ConsumeToken(); 27346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) && 27446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner "NextToken() not working properly!"); 2755c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner SourceLocation CCLoc = ConsumeToken(); 2761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2775c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner if (!HasScopeSpecifier) { 2785c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner SS.setBeginLoc(IdLoc); 2795c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner HasScopeSpecifier = true; 2805c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner } 2811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 282661c36b057d07c76c16fee95c01d96f3956265beArgyrios Kyrtzidis if (!SS.isInvalid()) 283661c36b057d07c76c16fee95c01d96f3956265beArgyrios Kyrtzidis SS.setScopeRep( 28423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, IdLoc, CCLoc, II, 285661c36b057d07c76c16fee95c01d96f3956265beArgyrios Kyrtzidis ObjectType, EnteringContext)); 2865c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner SS.setEndLoc(CCLoc); 2875c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner continue; 2885c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner } 2891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2905c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // nested-name-specifier: 2915c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // type-name '<' 2925c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner if (Next.is(tok::less)) { 2935c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner TemplateTy Template; 294014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor UnqualifiedId TemplateName; 295014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateName.setIdentifier(&II, Tok.getLocation()); 2961fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor bool MemberOfUnknownSpecialization; 29723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS, 2987c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara /*hasTemplateKeyword=*/false, 299014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateName, 3002dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor ObjectType, 301495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor EnteringContext, 3021fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor Template, 3031fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor MemberOfUnknownSpecialization)) { 3045c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // We have found a template name, so annotate this this token 3055c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // with a template-id annotation. We do not permit the 3065c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // template-id to be translated into a type annotation, 3075c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // because some clients (e.g., the parsing of class template 3085c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // specializations) still want to see the original template-id 3095c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // token. 310ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor ConsumeToken(); 311ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName, 312ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation(), false)) 3139ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return true; 3145c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner continue; 315d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor } 316d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor 317d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) && 318d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor IsTemplateArgumentList(1)) { 319d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor // We have something like t::getAs<T>, where getAs is a 320d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor // member of an unknown specialization. However, this will only 321d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor // parse correctly as a template, so suggest the keyword 'template' 322d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor // before 'getAs' and treat this as a dependent template name. 323d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword) 324d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor << II.getName() 325d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor << FixItHint::CreateInsertion(Tok.getLocation(), "template "); 326d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor 327d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor if (TemplateNameKind TNK 32823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor = Actions.ActOnDependentTemplateName(getCurScope(), 329d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor Tok.getLocation(), SS, 330d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor TemplateName, ObjectType, 331d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor EnteringContext, Template)) { 332d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor // Consume the identifier. 333d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor ConsumeToken(); 334d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName, 335d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor SourceLocation(), false)) 336d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor return true; 337d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor } 338d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor else 339d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor return true; 340d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor 341d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor continue; 3425c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner } 3435c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner } 3445c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner 34539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // We don't have any tokens that form the beginning of a 34639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // nested-name-specifier, so we're done. 34739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor break; 34839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor } 3491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 350d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // Even if we didn't see any pieces of a nested-name-specifier, we 351d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // still check whether there is a tilde in this position, which 352d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // indicates a potential pseudo-destructor. 353d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor if (CheckForDestructor && Tok.is(tok::tilde)) 354d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor *MayBePseudoDestructor = true; 355d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor 3569ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 357eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis} 358eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 359eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression. 360eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 361eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// id-expression: 362eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// unqualified-id 363eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// qualified-id 364eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 365eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// qualified-id: 366eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id 367eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' identifier 368eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' operator-function-id 369edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor/// '::' template-id 370eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 371eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not 372eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect: 373eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 374eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' conversion-function-id 375eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' '~' class-name 376eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 377eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics: 378eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 379eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {}; 380eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {} 381eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() { 382eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// :: A :: ~ C(); // Some Sema error about using destructor with a 383eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// // namespace. 384eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// :: ~ C(); // Some Parser error like 'unexpected ~'. 385eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// } 386eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 387eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like: 388eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 389eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// qualified-id: 390eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id 391eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' unqualified-id 392eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 393eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the 394eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope. 395eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 396ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a 397ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts, 398ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may 399ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear. 400ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// 40160d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) { 402eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis // qualified-id: 403eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id 404eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis // '::' unqualified-id 405eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis // 406eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis CXXScopeSpec SS; 407b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); 40802a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor 40902a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor UnqualifiedId Name; 41002a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor if (ParseUnqualifiedId(SS, 41102a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor /*EnteringContext=*/false, 41202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor /*AllowDestructorName=*/false, 41302a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor /*AllowConstructorName=*/false, 414b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall /*ObjectType=*/ ParsedType(), 41502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor Name)) 41602a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor return ExprError(); 417b681b61fea36618778b8030360e90e3f4641233bJohn McCall 418b681b61fea36618778b8030360e90e3f4641233bJohn McCall // This is only the direct operand of an & operator if it is not 419b681b61fea36618778b8030360e90e3f4641233bJohn McCall // followed by a postfix-expression suffix. 4209c72c6088d591ace8503b842d39448c2040f3033John McCall if (isAddressOfOperand && isPostfixExpressionSuffixStart()) 4219c72c6088d591ace8503b842d39448c2040f3033John McCall isAddressOfOperand = false; 42202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor 42323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren), 42402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor isAddressOfOperand); 42502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor 426eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis} 427eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another 4295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type. 4305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 4315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// postfix-expression: [C++ 5.2p1] 4325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'dynamic_cast' '<' type-name '>' '(' expression ')' 4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'static_cast' '<' type-name '>' '(' expression ')' 4345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'reinterpret_cast' '<' type-name '>' '(' expression ')' 4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'const_cast' '<' type-name '>' '(' expression ')' 4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 43760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXCasts() { 4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer tok::TokenKind Kind = Tok.getKind(); 4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CastName = 0; // For error messages 4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Kind) { 4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: assert(0 && "Unknown C++ cast!"); abort(); 4435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::kw_const_cast: CastName = "const_cast"; break; 4445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break; 4455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break; 4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::kw_static_cast: CastName = "static_cast"; break; 4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation OpLoc = ConsumeToken(); 4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation LAngleBracketLoc = Tok.getLocation(); 4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName)) 45320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 455809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor TypeResult CastTy = ParseTypeName(); 4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation RAngleBracketLoc = Tok.getLocation(); 4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4581ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) 45920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<"); 4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; 4625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 46321e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName)) 46421e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis return ExprError(); 4655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 46660d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult Result = ParseExpression(); 4671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 46821e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis // Match the ')'. 46927591ff4fc64600fd67c5d81899e3efe5ef41a80Douglas Gregor RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 4705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 471809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor if (!Result.isInvalid() && !CastTy.isInvalid()) 47249badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor Result = Actions.ActOnCXXNamedCast(OpLoc, Kind, 473f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl LAngleBracketLoc, CastTy.get(), 474809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor RAngleBracketLoc, 4759ae2f076ca5ab1feb3ba95629099ec2319833701John McCall LParenLoc, Result.take(), RParenLoc); 4765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 47720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return move(Result); 4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 480c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression. 481c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// 482c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// postfix-expression: [C++ 5.2p1] 483c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// 'typeid' '(' expression ')' 484c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// 'typeid' '(' type-id ')' 485c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// 48660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXTypeid() { 487c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!"); 488c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 489c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl SourceLocation OpLoc = ConsumeToken(); 490c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl SourceLocation LParenLoc = Tok.getLocation(); 491c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl SourceLocation RParenLoc; 492c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 493c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl // typeid expressions are always parenthesized. 494c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, 495c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl "typeid")) 49620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 497c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 49860d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult Result; 499c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 500c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl if (isTypeIdInParens()) { 501809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor TypeResult Ty = ParseTypeName(); 502c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 503c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl // Match the ')'. 5044eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 505c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 5064eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor if (Ty.isInvalid() || RParenLoc.isInvalid()) 50720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 508c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 509c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true, 510b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall Ty.get().getAsOpaquePtr(), RParenLoc); 511c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl } else { 512e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor // C++0x [expr.typeid]p3: 5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // When typeid is applied to an expression other than an lvalue of a 5141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // polymorphic class type [...] The expression is an unevaluated 515e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor // operand (Clause 5). 516e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor // 5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Note that we can't tell whether the expression is an lvalue of a 518e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor // polymorphic class type until after we've parsed the expression, so 519ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor // we the expression is potentially potentially evaluated. 520ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor EnterExpressionEvaluationContext Unevaluated(Actions, 521f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall Sema::PotentiallyPotentiallyEvaluated); 522c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl Result = ParseExpression(); 523c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 524c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl // Match the ')'. 5250e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl if (Result.isInvalid()) 526c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl SkipUntil(tok::r_paren); 527c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl else { 5284eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 5294eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor if (RParenLoc.isInvalid()) 5304eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor return ExprError(); 5314eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor 532c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false, 533effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl Result.release(), RParenLoc); 534c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl } 535c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl } 536c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 53720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return move(Result); 538c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl} 539c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 54001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression. 54101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// 54201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// '__uuidof' '(' expression ')' 54301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// '__uuidof' '(' type-id ')' 54401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// 54501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetExprResult Parser::ParseCXXUuidof() { 54601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!"); 54701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 54801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet SourceLocation OpLoc = ConsumeToken(); 54901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet SourceLocation LParenLoc = Tok.getLocation(); 55001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet SourceLocation RParenLoc; 55101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 55201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet // __uuidof expressions are always parenthesized. 55301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, 55401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet "__uuidof")) 55501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet return ExprError(); 55601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 55701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet ExprResult Result; 55801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 55901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet if (isTypeIdInParens()) { 56001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet TypeResult Ty = ParseTypeName(); 56101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 56201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet // Match the ')'. 56301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 56401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 56501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet if (Ty.isInvalid()) 56601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet return ExprError(); 56701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 56801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true, 56901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet Ty.get().getAsOpaquePtr(), RParenLoc); 57001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet } else { 57101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); 57201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet Result = ParseExpression(); 57301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 57401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet // Match the ')'. 57501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet if (Result.isInvalid()) 57601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet SkipUntil(tok::r_paren); 57701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet else { 57801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 57901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 58001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false, 58101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet Result.release(), RParenLoc); 58201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet } 58301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet } 58401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 58501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet return move(Result); 58601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet} 58701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet 588d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \brief Parse a C++ pseudo-destructor expression after the base, 589d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// . or -> operator, and nested-name-specifier have already been 590d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// parsed. 591d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// 592d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// postfix-expression: [C++ 5.2] 593d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// postfix-expression . pseudo-destructor-name 594d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// postfix-expression -> pseudo-destructor-name 595d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// 596d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// pseudo-destructor-name: 597d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name 598d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// ::[opt] nested-name-specifier template simple-template-id :: 599d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// ~type-name 600d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// ::[opt] nested-name-specifier[opt] ~type-name 601d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// 60260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult 603d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc, 604d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor tok::TokenKind OpKind, 605d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor CXXScopeSpec &SS, 606b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType ObjectType) { 607d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // We're parsing either a pseudo-destructor-name or a dependent 608d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // member access that has the same form as a 609d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // pseudo-destructor-name. We parse both in the same way and let 610d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // the action model sort them out. 611d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // 612d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // Note that the ::[opt] nested-name-specifier[opt] has already 613d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // been parsed, and if there was a simple-template-id, it has 614d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // been coalesced into a template-id annotation token. 615d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor UnqualifiedId FirstTypeName; 616d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor SourceLocation CCLoc; 617d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor if (Tok.is(tok::identifier)) { 618d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 619d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor ConsumeToken(); 620d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail"); 621d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor CCLoc = ConsumeToken(); 622d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor } else if (Tok.is(tok::annot_template_id)) { 623d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor FirstTypeName.setTemplateId( 624d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor (TemplateIdAnnotation *)Tok.getAnnotationValue()); 625d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor ConsumeToken(); 626d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail"); 627d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor CCLoc = ConsumeToken(); 628d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor } else { 629d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor FirstTypeName.setIdentifier(0, SourceLocation()); 630d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor } 631d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor 632d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // Parse the tilde. 633d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail"); 634d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor SourceLocation TildeLoc = ConsumeToken(); 635d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor if (!Tok.is(tok::identifier)) { 636d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor Diag(Tok, diag::err_destructor_tilde_identifier); 637d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor return ExprError(); 638d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor } 639d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor 640d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // Parse the second type. 641d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor UnqualifiedId SecondTypeName; 642d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor IdentifierInfo *Name = Tok.getIdentifierInfo(); 643d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor SourceLocation NameLoc = ConsumeToken(); 644d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor SecondTypeName.setIdentifier(Name, NameLoc); 645d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor 646d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // If there is a '<', the second type name is a template-id. Parse 647d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor // it as such. 648d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor if (Tok.is(tok::less) && 649d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType, 6500278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor SecondTypeName, /*AssumeTemplateName=*/true, 6510278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor /*TemplateKWLoc*/SourceLocation())) 652d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor return ExprError(); 653d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor 6549ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, 6559ae2f076ca5ab1feb3ba95629099ec2319833701John McCall OpLoc, OpKind, 656d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor SS, FirstTypeName, CCLoc, 657d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor TildeLoc, SecondTypeName, 658d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor Tok.is(tok::l_paren)); 659d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor} 660d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor 6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals. 6625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// boolean-literal: [C++ 2.13.5] 6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'true' 6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'false' 66660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXBoolLiteral() { 6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer tok::TokenKind Kind = Tok.getKind(); 668f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind); 6695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 67050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner 67150dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression. 67250dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// 67350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// throw-expression: [C++ 15] 67450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// 'throw' assignment-expression[opt] 67560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseThrowExpression() { 67650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner assert(Tok.is(tok::kw_throw) && "Not throw!"); 67750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token. 67820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl 6792a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner // If the current token isn't the start of an assignment-expression, 6802a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner // then the expression is not present. This handles things like: 6812a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner // "C ? throw : (void)42", which is crazy but legal. 6822a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner switch (Tok.getKind()) { // FIXME: move this predicate somewhere common. 6832a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::semi: 6842a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::r_paren: 6852a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::r_square: 6862a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::r_brace: 6872a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::colon: 6882a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::comma: 6899ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Actions.ActOnCXXThrow(ThrowLoc, 0); 69050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner 6912a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner default: 69260d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult Expr(ParseAssignmentExpression()); 69320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl if (Expr.isInvalid()) return move(Expr); 6949ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Actions.ActOnCXXThrow(ThrowLoc, Expr.take()); 6952a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner } 69650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner} 6974cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 6984cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer. 6994cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// 7004cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is 7014cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which 7024cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called. 70360d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXThis() { 7044cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis assert(Tok.is(tok::kw_this) && "Not 'this'!"); 7054cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis SourceLocation ThisLoc = ConsumeToken(); 706f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl return Actions.ActOnCXXThis(ThisLoc); 7074cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis} 708987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 709987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type. 710987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)") 711987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)") 712987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()"). 713987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// 714987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// postfix-expression: [C++ 5.2p1] 715987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] 716987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// typename-specifier '(' expression-list[opt] ')' [TODO] 717987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// 71860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult 71920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) { 720987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 721b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get(); 722987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 723987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis assert(Tok.is(tok::l_paren) && "Expected '('!"); 724bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor GreaterThanIsOperatorScope G(GreaterThanIsOperator, true); 725bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor 726987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis SourceLocation LParenLoc = ConsumeParen(); 727987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 728a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl ExprVector Exprs(Actions); 729987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis CommaLocsTy CommaLocs; 730987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 731987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis if (Tok.isNot(tok::r_paren)) { 732987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis if (ParseExpressionList(Exprs, CommaLocs)) { 733987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis SkipUntil(tok::r_paren); 73420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 735987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis } 736987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis } 737987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 738987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis // Match the ')'. 739987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 740987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 741ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl // TypeRep could be null, if it references an invalid typedef. 742ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl if (!TypeRep) 743ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl return ExprError(); 744ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl 745987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&& 746987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis "Unexpected number of commas!"); 747ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs), 748a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor RParenLoc); 749987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis} 750987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 75199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// ParseCXXCondition - if/switch/while condition expression. 75271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// 75371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// condition: 75471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// expression 75571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// type-specifier-seq declarator '=' assignment-expression 75671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] 75771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// '=' assignment-expression 75871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// 75999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param ExprResult if the condition was parsed as an expression, the 76099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed expression. 76199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// 76299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param DeclResult if the condition was parsed as a declaration, the 76399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed declaration. 76499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// 765586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param Loc The location of the start of the statement that requires this 766586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// condition, e.g., the "for" in a for loop. 767586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// 768586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param ConvertToBoolean Whether the condition expression should be 769586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// converted to a boolean value. 770586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// 77199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \returns true if there was a parsing, false otherwise. 77260d7b3a319d84d688752be3870615ac0f111fb16John McCallbool Parser::ParseCXXCondition(ExprResult &ExprOut, 77360d7b3a319d84d688752be3870615ac0f111fb16John McCall Decl *&DeclOut, 774586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor SourceLocation Loc, 775586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor bool ConvertToBoolean) { 77601dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor if (Tok.is(tok::code_completion)) { 777f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition); 778dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor ConsumeCodeCompletionToken(); 77901dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor } 78001dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor 78199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!isCXXConditionDeclaration()) { 782586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor // Parse the expression. 78360d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprOut = ParseExpression(); // expression 78460d7b3a319d84d688752be3870615ac0f111fb16John McCall DeclOut = 0; 78560d7b3a319d84d688752be3870615ac0f111fb16John McCall if (ExprOut.isInvalid()) 786586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return true; 787586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor 788586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor // If required, convert to a boolean value. 789586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor if (ConvertToBoolean) 79060d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprOut 79160d7b3a319d84d688752be3870615ac0f111fb16John McCall = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get()); 79260d7b3a319d84d688752be3870615ac0f111fb16John McCall return ExprOut.isInvalid(); 79399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor } 79471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 79571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis // type-specifier-seq 79671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis DeclSpec DS; 79771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis ParseSpecifierQualifierList(DS); 79871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 79971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis // declarator 80071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis Declarator DeclaratorInfo(DS, Declarator::ConditionContext); 80171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis ParseDeclarator(DeclaratorInfo); 80271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 80371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis // simple-asm-expr[opt] 80471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis if (Tok.is(tok::kw_asm)) { 805ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl SourceLocation Loc; 80660d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult AsmLabel(ParseSimpleAsm(&Loc)); 8070e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl if (AsmLabel.isInvalid()) { 80871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis SkipUntil(tok::semi); 80999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return true; 81071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis } 811effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl DeclaratorInfo.setAsmLabel(AsmLabel.release()); 812ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.SetRangeEnd(Loc); 81371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis } 81471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 81571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis // If attributes are present, parse them. 8167f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall MaybeParseGNUAttributes(DeclaratorInfo); 81771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 81899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor // Type-check the declaration itself. 81960d7b3a319d84d688752be3870615ac0f111fb16John McCall DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(), 8207f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall DeclaratorInfo); 82160d7b3a319d84d688752be3870615ac0f111fb16John McCall DeclOut = Dcl.get(); 82260d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprOut = ExprError(); 823a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis 82471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis // '=' assignment-expression 825a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis if (isTokenEqualOrMistypedEqualEqual( 826a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis diag::err_invalid_equalequal_after_declarator)) { 827dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin ConsumeToken(); 82860d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult AssignExpr(ParseAssignmentExpression()); 82999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!AssignExpr.isInvalid()) 83034b41d939a1328f484511c6002ba2456db879a29Richard Smith Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false, 83134b41d939a1328f484511c6002ba2456db879a29Richard Smith DS.getTypeSpecType() == DeclSpec::TST_auto); 83299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor } else { 83399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor // FIXME: C++0x allows a braced-init-list 83499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Diag(Tok, diag::err_expected_equal_after_declarator); 83599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor } 83699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor 837586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor // FIXME: Build a reference to this declaration? Convert it to bool? 838586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor // (This is currently handled by Sema). 839586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor 84099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return false; 84171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis} 84271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 8436aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brief Determine whether the current token starts a C++ 8446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// simple-type-specifier. 8456aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::isCXXSimpleTypeSpecifier() const { 8466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor switch (Tok.getKind()) { 8476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::annot_typename: 8486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_short: 8496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_long: 8506aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_signed: 8516aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_unsigned: 8526aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_void: 8536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_char: 8546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_int: 8556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_float: 8566aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_double: 8576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_wchar_t: 8586aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_char16_t: 8596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_char32_t: 8606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_bool: 8616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor // FIXME: C++0x decltype support. 8626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor // GNU typeof support. 8636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor case tok::kw_typeof: 8646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor return true; 8656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor 8666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor default: 8676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor break; 8686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor } 8696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor 8706aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor return false; 8716aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor} 8726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor 873987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers. 874987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of 875987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier. 876987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// 877987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier: 878eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::'[opt] nested-name-specifier[opt] type-name 879987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO] 880987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// char 881987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// wchar_t 882987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// bool 883987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// short 884987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// int 885987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// long 886987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// signed 887987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// unsigned 888987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// float 889987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// double 890987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// void 891987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU] typeof-specifier 892987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto [TODO] 893987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// 894987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// type-name: 895987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// class-name 896987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// enum-name 897987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// typedef-name 898987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// 899987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) { 900987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis DS.SetRangeStart(Tok.getLocation()); 901987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis const char *PrevSpec; 902fec54013fcd0eb72642741584ca04c1bc292bef8John McCall unsigned DiagID; 903987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis SourceLocation Loc = Tok.getLocation(); 9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 905987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis switch (Tok.getKind()) { 90655a7cefc846765ac7d142a63f773747a20518d71Chris Lattner case tok::identifier: // foo::bar 90755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner case tok::coloncolon: // ::foo::bar 90855a7cefc846765ac7d142a63f773747a20518d71Chris Lattner assert(0 && "Annotation token should already be formed!"); 9091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump default: 910987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis assert(0 && "Not a simple-type-specifier token!"); 911987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis abort(); 91255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner 913987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis // type-name 914b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner case tok::annot_typename: { 9156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor if (getTypeAnnotation(Tok)) 9166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, 9176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor getTypeAnnotation(Tok)); 9186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor else 9196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor DS.SetTypeSpecError(); 9209bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor 9219bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor DS.SetRangeEnd(Tok.getAnnotationEndLoc()); 9229bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor ConsumeToken(); 9239bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor 9249bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' 9259bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an 9269bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor // Objective-C interface. If we don't have Objective-C or a '<', this is 9279bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor // just a normal reference to a typedef name. 9289bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor if (Tok.is(tok::less) && getLang().ObjC1) 9299bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor ParseObjCProtocolQualifiers(DS); 9309bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor 9319bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor DS.Finish(Diags, PP); 9329bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor return; 933987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis } 9341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 935987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis // builtin types 936987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_short: 937fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); 938987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 939987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_long: 940fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID); 941987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 942987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_signed: 943fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); 944987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 945987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_unsigned: 946fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID); 947987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 948987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_void: 949fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); 950987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 951987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_char: 952fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); 953987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 954987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_int: 955fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); 956987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 957987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_float: 958fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); 959987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 960987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_double: 961fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); 962987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 963987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_wchar_t: 964fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); 965987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 966f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case tok::kw_char16_t: 967fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); 968f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith break; 969f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case tok::kw_char32_t: 970fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); 971f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith break; 972987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_bool: 973fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); 974987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 9751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9766aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor // FIXME: C++0x decltype support. 977987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis // GNU typeof support. 978987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_typeof: 979987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis ParseTypeofSpecifier(DS); 9809b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor DS.Finish(Diags, PP); 981987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis return; 982987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis } 983b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner if (Tok.is(tok::annot_typename)) 984eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis DS.SetRangeEnd(Tok.getAnnotationEndLoc()); 985eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis else 986eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis DS.SetRangeEnd(Tok.getLocation()); 987987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis ConsumeToken(); 9889b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor DS.Finish(Diags, PP); 989987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis} 9901cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor 9912f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++ 9922f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers, 9932f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished 9942f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are 9952f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and 9962f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false 9972f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise. 9982f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// 9992f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// type-specifier-seq: [C++ 8.1] 10002f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// type-specifier type-specifier-seq[opt] 10012f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// 10022f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) { 10032f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor DS.SetRangeStart(Tok.getLocation()); 10042f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor const char *PrevSpec = 0; 1005fec54013fcd0eb72642741584ca04c1bc292bef8John McCall unsigned DiagID; 1006fec54013fcd0eb72642741584ca04c1bc292bef8John McCall bool isInvalid = 0; 10072f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor 10082f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor // Parse one or more of the type specifiers. 1009d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, 1010d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl ParsedTemplateInfo(), /*SuppressDeclarations*/true)) { 10119fa8e569407e02148888136609431a3fe083096dNick Lewycky Diag(Tok, diag::err_expected_type); 10122f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor return true; 10132f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor } 10141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1015d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, 1016d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl ParsedTemplateInfo(), /*SuppressDeclarations*/true)) 1017d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl {} 10182f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor 1019396a9f235e160093b5f803f7a6a18fad7b68bdbeDouglas Gregor DS.Finish(Diags, PP); 10202f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor return false; 10212f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor} 10222f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor 10233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of 10243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form. 10253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 10263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or 10273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine 10283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will 10293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to 10303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller. 10313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 10323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if 10333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id. 10343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 10353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual 10363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name. 10373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 10383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or 10393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor. 10403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 10413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the 10423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier. 10433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 104446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access 104546df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed. 104646df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// 10473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id 10483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully, 10493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id. 10503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 1051d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param AssumeTemplateId When true, this routine will assume that the name 1052d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// refers to a template without performing name lookup to verify. 1053d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// 10543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise. 10553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS, 10563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor IdentifierInfo *Name, 10573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor SourceLocation NameLoc, 10583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor bool EnteringContext, 1059b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType ObjectType, 1060d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor UnqualifiedId &Id, 10610278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor bool AssumeTemplateId, 10620278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor SourceLocation TemplateKWLoc) { 10630278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor assert((AssumeTemplateId || Tok.is(tok::less)) && 10640278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor "Expected '<' to finish parsing a template-id"); 10653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 10663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateTy Template; 10673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateNameKind TNK = TNK_Non_template; 10683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor switch (Id.getKind()) { 10693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor case UnqualifiedId::IK_Identifier: 1070014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor case UnqualifiedId::IK_OperatorFunctionId: 1071e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt case UnqualifiedId::IK_LiteralOperatorId: 1072d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor if (AssumeTemplateId) { 107323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS, 1074d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor Id, ObjectType, EnteringContext, 1075d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor Template); 1076d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor if (TNK == TNK_Non_template) 1077d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor return true; 10781fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor } else { 10791fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor bool MemberOfUnknownSpecialization; 10807c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara TNK = Actions.isTemplateName(getCurScope(), SS, 10817c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara TemplateKWLoc.isValid(), Id, 10827c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara ObjectType, EnteringContext, Template, 10831fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor MemberOfUnknownSpecialization); 10841fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor 10851fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor if (TNK == TNK_Non_template && MemberOfUnknownSpecialization && 10861fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor ObjectType && IsTemplateArgumentList()) { 10871fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor // We have something like t->getAs<T>(), where getAs is a 10881fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor // member of an unknown specialization. However, this will only 10891fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor // parse correctly as a template, so suggest the keyword 'template' 10901fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor // before 'getAs' and treat this as a dependent template name. 10911fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor std::string Name; 10921fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor if (Id.getKind() == UnqualifiedId::IK_Identifier) 10931fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor Name = Id.Identifier->getName(); 10941fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor else { 10951fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor Name = "operator "; 10961fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) 10971fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor Name += getOperatorSpelling(Id.OperatorFunctionId.Operator); 10981fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor else 10991fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor Name += Id.Identifier->getName(); 11001fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor } 11011fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword) 11021fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor << Name 11031fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor << FixItHint::CreateInsertion(Id.StartLocation, "template "); 110423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, 1105d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor SS, Id, ObjectType, 1106d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor EnteringContext, Template); 1107d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor if (TNK == TNK_Non_template) 11081fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor return true; 11091fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor } 11101fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor } 11113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor break; 11123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 1113014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor case UnqualifiedId::IK_ConstructorName: { 1114014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor UnqualifiedId TemplateName; 11151fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor bool MemberOfUnknownSpecialization; 1116014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateName.setIdentifier(Name, NameLoc); 11177c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(), 11187c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara TemplateName, ObjectType, 11191fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor EnteringContext, Template, 11201fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor MemberOfUnknownSpecialization); 11213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor break; 1122014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor } 11233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 1124014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor case UnqualifiedId::IK_DestructorName: { 1125014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor UnqualifiedId TemplateName; 11261fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor bool MemberOfUnknownSpecialization; 1127014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateName.setIdentifier(Name, NameLoc); 11282d1c21414199a7452f122598189363a3922605b1Douglas Gregor if (ObjectType) { 112923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS, 1130d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor TemplateName, ObjectType, 1131d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor EnteringContext, Template); 1132d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor if (TNK == TNK_Non_template) 11332d1c21414199a7452f122598189363a3922605b1Douglas Gregor return true; 11342d1c21414199a7452f122598189363a3922605b1Douglas Gregor } else { 11357c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(), 11367c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara TemplateName, ObjectType, 11371fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor EnteringContext, Template, 11381fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor MemberOfUnknownSpecialization); 11392d1c21414199a7452f122598189363a3922605b1Douglas Gregor 1140b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall if (TNK == TNK_Non_template && !Id.DestructorName.get()) { 1141124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor Diag(NameLoc, diag::err_destructor_template_id) 1142124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor << Name << SS.getRange(); 11432d1c21414199a7452f122598189363a3922605b1Douglas Gregor return true; 11442d1c21414199a7452f122598189363a3922605b1Douglas Gregor } 11452d1c21414199a7452f122598189363a3922605b1Douglas Gregor } 11463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor break; 1147014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor } 11483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor default: 11503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 11513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 11523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (TNK == TNK_Non_template) 11543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 11553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Parse the enclosed template argument list. 11573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor SourceLocation LAngleLoc, RAngleLoc; 11583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateArgList TemplateArgs; 11590278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor if (Tok.is(tok::less) && 11600278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor ParseTemplateIdAfterTemplateName(Template, Id.StartLocation, 11613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor &SS, true, LAngleLoc, 11623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateArgs, 11633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor RAngleLoc)) 11643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 11653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Id.getKind() == UnqualifiedId::IK_Identifier || 1167e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt Id.getKind() == UnqualifiedId::IK_OperatorFunctionId || 1168e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) { 11693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Form a parsed representation of the template-id to be stored in the 11703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // UnqualifiedId. 11713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateIdAnnotation *TemplateId 11723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor = TemplateIdAnnotation::Allocate(TemplateArgs.size()); 11733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Id.getKind() == UnqualifiedId::IK_Identifier) { 11753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->Name = Id.Identifier; 1176014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateId->Operator = OO_None; 11773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->TemplateNameLoc = Id.StartLocation; 11783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } else { 1179014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateId->Name = 0; 1180014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateId->Operator = Id.OperatorFunctionId.Operator; 1181014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateId->TemplateNameLoc = Id.StartLocation; 11823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 11833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11842b5289b6fd7e3d9899868410a498c081c9595662John McCall TemplateId->Template = Template; 11853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->Kind = TNK; 11863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->LAngleLoc = LAngleLoc; 11873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->RAngleLoc = RAngleLoc; 1188314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor ParsedTemplateArgument *Args = TemplateId->getTemplateArgs(); 11893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); 1190314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor Arg != ArgEnd; ++Arg) 11913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Args[Arg] = TemplateArgs[Arg]; 11923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Id.setTemplateId(TemplateId); 11943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 11953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 11963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Bundle the template arguments together. 11983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), 11993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateArgs.size()); 12003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 12013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Constructor and destructor names. 1202f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TypeResult Type 12033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor = Actions.ActOnTemplateIdType(Template, NameLoc, 12043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor LAngleLoc, TemplateArgsPtr, 12053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor RAngleLoc); 12063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Type.isInvalid()) 12073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 12083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 12093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Id.getKind() == UnqualifiedId::IK_ConstructorName) 12103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Id.setConstructorName(Type.get(), NameLoc, RAngleLoc); 12113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor else 12123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc); 12133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 12143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 12153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor} 12163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 1217ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part 1218ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id. 12193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 1220ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or 1221ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way. 12223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 1223ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code 12243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id: [C++ 13.5] 12253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 'operator' operator 12263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 1227ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// operator: one of 12283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// new delete new[] delete[] 12293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// + - * / % ^ & | ~ 12303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// ! = < > += -= *= /= %= 12313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// ^= &= |= << >> >>= <<= == != 12323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// <= >= && || ++ -- , ->* -> 12333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// () [] 12343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 12353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// conversion-function-id: [C++ 12.3.2] 12363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator conversion-type-id 12373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 12383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// conversion-type-id: 12393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// type-specifier-seq conversion-declarator[opt] 12403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 12413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// conversion-declarator: 12423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// ptr-operator conversion-declarator[opt] 12433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode 12443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 12453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If 12463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id. 12473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 12483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the 12493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier. 12503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 1251ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access 1252ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed. 1253ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 1254ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id. 1255ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 1256ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise. 1257ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext, 1258b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType ObjectType, 1259ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor UnqualifiedId &Result) { 1260ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); 1261ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1262ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the 'operator' keyword. 1263ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation KeywordLoc = ConsumeToken(); 1264ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1265ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Determine what kind of operator name we have. 1266ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor unsigned SymbolIdx = 0; 1267ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation SymbolLocations[3]; 1268ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor OverloadedOperatorKind Op = OO_None; 1269ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor switch (Tok.getKind()) { 1270ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::kw_new: 1271ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::kw_delete: { 1272ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor bool isNew = Tok.getKind() == tok::kw_new; 1273ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the 'new' or 'delete'. 1274ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = ConsumeToken(); 1275ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (Tok.is(tok::l_square)) { 1276ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the '['. 1277ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation LBracketLoc = ConsumeBracket(); 1278ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the ']'. 1279ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square, 1280ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor LBracketLoc); 1281ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (RBracketLoc.isInvalid()) 1282ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 1283ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1284ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = LBracketLoc; 1285ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = RBracketLoc; 1286ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Op = isNew? OO_Array_New : OO_Array_Delete; 1287ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } else { 1288ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Op = isNew? OO_New : OO_Delete; 1289ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 1290ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 1291ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 1292ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1293ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1294ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::Token: \ 1295ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = ConsumeToken(); \ 1296ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Op = OO_##Name; \ 1297ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 1298ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) 1299ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def" 1300ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1301ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::l_paren: { 1302ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the '('. 1303ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation LParenLoc = ConsumeParen(); 1304ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the ')'. 1305ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, 1306ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor LParenLoc); 1307ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (RParenLoc.isInvalid()) 1308ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 1309ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1310ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = LParenLoc; 1311ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = RParenLoc; 1312ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Op = OO_Call; 1313ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 1314ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 1315ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1316ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::l_square: { 1317ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the '['. 1318ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation LBracketLoc = ConsumeBracket(); 1319ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the ']'. 1320ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square, 1321ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor LBracketLoc); 1322ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (RBracketLoc.isInvalid()) 1323ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 1324ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1325ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = LBracketLoc; 1326ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = RBracketLoc; 1327ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Op = OO_Subscript; 1328ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 1329ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 1330ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1331ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::code_completion: { 1332ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Code completion for the operator name. 133323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CodeCompleteOperatorName(getCurScope()); 1334ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1335ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the operator token. 1336dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor ConsumeCodeCompletionToken(); 1337ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1338ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Don't try to parse any further. 1339ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 1340ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 1341ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1342ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor default: 1343ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 1344ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 1345ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1346ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (Op != OO_None) { 1347ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // We have parsed an operator-function-id. 1348ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations); 1349ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return false; 1350ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 13510486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt 13520486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt // Parse a literal-operator-id. 13530486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt // 13540486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt // literal-operator-id: [C++0x 13.5.8] 13550486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt // operator "" identifier 13560486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt 13570486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) { 13580486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt if (Tok.getLength() != 2) 13590486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt Diag(Tok.getLocation(), diag::err_operator_string_not_empty); 13600486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt ConsumeStringToken(); 13610486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt 13620486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt if (Tok.isNot(tok::identifier)) { 13630486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt Diag(Tok.getLocation(), diag::err_expected_ident); 13640486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt return true; 13650486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt } 13660486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt 13670486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt IdentifierInfo *II = Tok.getIdentifierInfo(); 13680486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken()); 13693e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt return false; 13700486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt } 1371ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1372ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Parse a conversion-function-id. 1373ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // 1374ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // conversion-function-id: [C++ 12.3.2] 1375ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // operator conversion-type-id 1376ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // 1377ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // conversion-type-id: 1378ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // type-specifier-seq conversion-declarator[opt] 1379ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // 1380ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // conversion-declarator: 1381ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // ptr-operator conversion-declarator[opt] 1382ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1383ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Parse the type-specifier-seq. 1384ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor DeclSpec DS; 1385f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType? 1386ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 1387ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1388ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Parse the conversion-declarator, which is merely a sequence of 1389ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // ptr-operators. 1390ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Declarator D(DS, Declarator::TypeNameContext); 1391ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor ParseDeclaratorInternal(D, /*DirectDeclParser=*/0); 1392ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1393ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Finish up the type. 1394f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D); 1395ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (Ty.isInvalid()) 1396ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 1397ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1398ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Note that this is a conversion-function-id. 1399ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Result.setConversionFunctionId(KeywordLoc, Ty.get(), 1400ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor D.getSourceRange().getEnd()); 1401ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return false; 1402ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor} 1403ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1404ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the 1405ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity. 1406ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 1407ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code 1408ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// unqualified-id: [C++ expr.prim.general] 1409ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// identifier 1410ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// operator-function-id 1411ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id 1412ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO] 1413ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// ~ class-name 1414ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template-id 1415ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 1416ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode 1417ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 1418ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If 1419ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id. 1420ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 1421ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the 1422ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier. 1423ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 14243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name. 14253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 14263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name. 14273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 142846df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access 142946df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed. 143046df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// 14313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id. 14323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 14333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise. 14343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext, 14353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor bool AllowDestructorName, 14363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor bool AllowConstructorName, 1437b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType ObjectType, 14383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor UnqualifiedId &Result) { 14390278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor 14400278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor // Handle 'A::template B'. This is for template-ids which have not 14410278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor // already been annotated by ParseOptionalCXXScopeSpecifier(). 14420278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor bool TemplateSpecified = false; 14430278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor SourceLocation TemplateKWLoc; 14440278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor if (getLang().CPlusPlus && Tok.is(tok::kw_template) && 14450278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor (ObjectType || SS.isSet())) { 14460278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor TemplateSpecified = true; 14470278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor TemplateKWLoc = ConsumeToken(); 14480278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor } 14490278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor 14503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // unqualified-id: 14513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // identifier 14523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // template-id (when it hasn't already been annotated) 14533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Tok.is(tok::identifier)) { 14543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Consume the identifier. 14553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor IdentifierInfo *Id = Tok.getIdentifierInfo(); 14563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor SourceLocation IdLoc = ConsumeToken(); 14573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 1458b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor if (!getLang().CPlusPlus) { 1459b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor // If we're not in C++, only identifiers matter. Record the 1460b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor // identifier and return. 1461b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor Result.setIdentifier(Id, IdLoc); 1462b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor return false; 1463b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor } 1464b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor 14653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (AllowConstructorName && 146623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.isCurrentClassName(*Id, getCurScope(), &SS)) { 14673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // We have parsed a constructor name. 146823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(), 14693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor &SS, false), 14703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor IdLoc, IdLoc); 14713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } else { 14723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // We have parsed an identifier. 14733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Result.setIdentifier(Id, IdLoc); 14743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 14753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 14763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // If the next token is a '<', we may have a template. 14770278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor if (TemplateSpecified || Tok.is(tok::less)) 14783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext, 14790278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor ObjectType, Result, 14800278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor TemplateSpecified, TemplateKWLoc); 14813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 14823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 14833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 14843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 14853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // unqualified-id: 14863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // template-id (already parsed and annotated) 14873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Tok.is(tok::annot_template_id)) { 14880efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor TemplateIdAnnotation *TemplateId 14890efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()); 14900efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor 14910efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor // If the template-name names the current class, then this is a constructor 14920efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor if (AllowConstructorName && TemplateId->Name && 149323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { 14940efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor if (SS.isSet()) { 14950efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor // C++ [class.qual]p2 specifies that a qualified template-name 14960efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor // is taken as the constructor name where a constructor can be 14970efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor // declared. Thus, the template arguments are extraneous, so 14980efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor // complain about them and remove them entirely. 14990efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor Diag(TemplateId->TemplateNameLoc, 15000efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor diag::err_out_of_line_constructor_template_id) 15010efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor << TemplateId->Name 1502849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor << FixItHint::CreateRemoval( 15030efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc)); 15040efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor Result.setConstructorName(Actions.getTypeName(*TemplateId->Name, 15050efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor TemplateId->TemplateNameLoc, 150623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor getCurScope(), 15070efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor &SS, false), 15080efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor TemplateId->TemplateNameLoc, 15090efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor TemplateId->RAngleLoc); 15100efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor TemplateId->Destroy(); 15110efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor ConsumeToken(); 15120efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor return false; 15130efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor } 15140efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor 15150efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor Result.setConstructorTemplateId(TemplateId); 15160efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor ConsumeToken(); 15170efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor return false; 15180efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor } 15190efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor 15203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // We have already parsed a template-id; consume the annotation token as 15213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // our unqualified-id. 15220efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor Result.setTemplateId(TemplateId); 15233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor ConsumeToken(); 15243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 15253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 15263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 15273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // unqualified-id: 15283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // operator-function-id 15293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // conversion-function-id 15303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Tok.is(tok::kw_operator)) { 1531ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result)) 15323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 15333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 1534e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt // If we have an operator-function-id or a literal-operator-id and the next 1535e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt // token is a '<', we may have a 1536ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // 1537ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // template-id: 1538ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // operator-function-id < template-argument-list[opt] > 1539e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId || 1540e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) && 15410278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor (TemplateSpecified || Tok.is(tok::less))) 1542ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(), 1543ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor EnteringContext, ObjectType, 15440278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor Result, 15450278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor TemplateSpecified, TemplateKWLoc); 15463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 15473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 15483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 15493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 1550b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor if (getLang().CPlusPlus && 1551b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) { 15523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // C++ [expr.unary.op]p10: 15533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // There is an ambiguity in the unary-expression ~X(), where X is a 15543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // class-name. The ambiguity is resolved in favor of treating ~ as a 15553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // unary complement rather than treating ~X as referring to a destructor. 15563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 15573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Parse the '~'. 15583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor SourceLocation TildeLoc = ConsumeToken(); 15593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 15603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Parse the class-name. 15613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Tok.isNot(tok::identifier)) { 1562124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor Diag(Tok, diag::err_destructor_tilde_identifier); 15633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 15643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 15653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 15663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Parse the class-name (or template-name in a simple-template-id). 15673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor IdentifierInfo *ClassName = Tok.getIdentifierInfo(); 15683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor SourceLocation ClassNameLoc = ConsumeToken(); 15693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 15700278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor if (TemplateSpecified || Tok.is(tok::less)) { 1571b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc); 15722d1c21414199a7452f122598189363a3922605b1Douglas Gregor return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc, 15730278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor EnteringContext, ObjectType, Result, 15740278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor TemplateSpecified, TemplateKWLoc); 15752d1c21414199a7452f122598189363a3922605b1Douglas Gregor } 15762d1c21414199a7452f122598189363a3922605b1Douglas Gregor 15773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Note that this is a destructor name. 1578b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName, 1579b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ClassNameLoc, getCurScope(), 1580b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall SS, ObjectType, 1581b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall EnteringContext); 1582124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor if (!Ty) 15833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 1584124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor 15853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Result.setDestructorName(TildeLoc, Ty, ClassNameLoc); 15863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 15873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 15883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 15892d1c21414199a7452f122598189363a3922605b1Douglas Gregor Diag(Tok, diag::err_expected_unqualified_id) 15902d1c21414199a7452f122598189363a3922605b1Douglas Gregor << getLang().CPlusPlus; 15913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 15923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor} 15933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 15944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate 15954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors. 15961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 159759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has 159859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed. If the :: was present, "UseGlobal" is true and "Start" 159959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location. Otherwise, "Start" is the location of the 'new' token. 16004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 16014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-expression: 16024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '::'[opt] 'new' new-placement[opt] new-type-id 16034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-initializer[opt] 16044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '::'[opt] 'new' new-placement[opt] '(' type-id ')' 16054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-initializer[opt] 16064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 16074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-placement: 16084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '(' expression-list ')' 16094c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 1610cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// new-type-id: 1611cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// type-specifier-seq new-declarator[opt] 1612cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// 1613cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// new-declarator: 1614cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// ptr-operator new-declarator[opt] 1615cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// direct-new-declarator 1616cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// 16174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-initializer: 16184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '(' expression-list[opt] ')' 16194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// [C++0x] braced-init-list [TODO] 16204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 162160d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult 162259232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) { 162359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner assert(Tok.is(tok::kw_new) && "expected 'new' token"); 162459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner ConsumeToken(); // Consume 'new' 16254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 16264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // A '(' now can be a new-placement or the '(' wrapping the type-id in the 16274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // second form of new-expression. It can't be a new-type-id. 16284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1629a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl ExprVector PlacementArgs(Actions); 16304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SourceLocation PlacementLParen, PlacementRParen; 16314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 16324bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor SourceRange TypeIdParens; 1633cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl DeclSpec DS; 1634cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 16354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (Tok.is(tok::l_paren)) { 16364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // If it turns out to be a placement, we change the type location. 16374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl PlacementLParen = ConsumeParen(); 1638cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) { 1639cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); 164020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 1641cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl } 16424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 16434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen); 1644cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (PlacementRParen.isInvalid()) { 1645cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); 164620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 1647cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl } 16484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1649cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (PlacementArgs.empty()) { 16504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Reset the placement locations. There was no placement. 16514bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor TypeIdParens = SourceRange(PlacementLParen, PlacementRParen); 16524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl PlacementLParen = PlacementRParen = SourceLocation(); 16534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } else { 16544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // We still need the type. 16554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (Tok.is(tok::l_paren)) { 16564bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor TypeIdParens.setBegin(ConsumeParen()); 1657cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseSpecifierQualifierList(DS); 1658ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.SetSourceRange(DS.getSourceRange()); 1659cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseDeclarator(DeclaratorInfo); 16604bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren, 16614bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor TypeIdParens.getBegin())); 16624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } else { 1663cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (ParseCXXTypeSpecifierSeq(DS)) 1664cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl DeclaratorInfo.setInvalidType(true); 1665ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl else { 1666ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.SetSourceRange(DS.getSourceRange()); 1667cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseDeclaratorInternal(DeclaratorInfo, 1668cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl &Parser::ParseDirectNewDeclarator); 1669ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl } 16704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 16714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 16724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } else { 1673cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl // A new-type-id is a simplified type-id, where essentially the 1674cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl // direct-declarator is replaced by a direct-new-declarator. 1675cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (ParseCXXTypeSpecifierSeq(DS)) 1676cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl DeclaratorInfo.setInvalidType(true); 1677ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl else { 1678ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.SetSourceRange(DS.getSourceRange()); 1679cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseDeclaratorInternal(DeclaratorInfo, 1680cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl &Parser::ParseDirectNewDeclarator); 1681ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl } 16824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 1683eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner if (DeclaratorInfo.isInvalidType()) { 1684cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); 168520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 1686cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl } 16874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1688a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl ExprVector ConstructorArgs(Actions); 16894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SourceLocation ConstructorLParen, ConstructorRParen; 16904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 16914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (Tok.is(tok::l_paren)) { 16924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ConstructorLParen = ConsumeParen(); 16934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (Tok.isNot(tok::r_paren)) { 16944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl CommaLocsTy CommaLocs; 1695cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (ParseExpressionList(ConstructorArgs, CommaLocs)) { 1696cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); 169720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 1698cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl } 16994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 17004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen); 1701cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (ConstructorRParen.isInvalid()) { 1702cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); 170320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 1704cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl } 17054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 17064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1707f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen, 1708f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl move_arg(PlacementArgs), PlacementRParen, 17094bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor TypeIdParens, DeclaratorInfo, ConstructorLParen, 1710f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl move_arg(ConstructorArgs), ConstructorRParen); 17114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 17124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 17134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be 17144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal. 17154c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 17164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// direct-new-declarator: 17174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '[' expression ']' 17184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// direct-new-declarator '[' constant-expression ']' 17194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 172059232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) { 17214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Parse the array dimensions. 17224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl bool first = true; 17234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl while (Tok.is(tok::l_square)) { 17244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SourceLocation LLoc = ConsumeBracket(); 172560d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult Size(first ? ParseExpression() 17262f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl : ParseConstantExpression()); 17270e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl if (Size.isInvalid()) { 17284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Recover 17294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SkipUntil(tok::r_square); 17304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl return; 17314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 17324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl first = false; 17334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1734ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc); 17357f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall D.AddTypeInfo(DeclaratorChunk::getArray(0, ParsedAttributes(), 17367f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall /*static=*/false, /*star=*/false, 17377e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Size.release(), LLoc, RLoc), 1738ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl RLoc); 17394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1740ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl if (RLoc.isInvalid()) 17414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl return; 17424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 17434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 17444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 17454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id. 17464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator. 17474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 17484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-expression: 17494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '::'[opt] 'new' new-placement[opt] '(' type-id ')' 17504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-initializer[opt] 17514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 17524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-placement: 17534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '(' expression-list ')' 17544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 1755ca0408fb49c1370430672acf2d770b7151cf71deJohn McCallbool Parser::ParseExpressionListOrTypeId( 1756ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall llvm::SmallVectorImpl<Expr*> &PlacementArgs, 175759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner Declarator &D) { 17584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // The '(' was already consumed. 17594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (isTypeIdInParens()) { 1760cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseSpecifierQualifierList(D.getMutableDeclSpec()); 1761ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl D.SetSourceRange(D.getDeclSpec().getSourceRange()); 1762cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseDeclarator(D); 1763eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner return D.isInvalidType(); 17644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 17654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 17664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // It's not a type, it has to be an expression list. 17674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Discard the comma locations - ActOnCXXNew has enough parameters. 17684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl CommaLocsTy CommaLocs; 17694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl return ParseExpressionList(PlacementArgs, CommaLocs); 17704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 17714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 17724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used 17734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new. 17744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 177559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional 177659232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true 177759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location. Otherwise, "Start" is the location of the 177859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token. 177959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 17804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// delete-expression: 17814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '::'[opt] 'delete' cast-expression 17824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '::'[opt] 'delete' '[' ']' cast-expression 178360d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult 178459232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) { 178559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword"); 178659232d35f5820e334b6c8b007ae8006f4390055dChris Lattner ConsumeToken(); // Consume 'delete' 17874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 17884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Array delete? 17894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl bool ArrayDelete = false; 17904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (Tok.is(tok::l_square)) { 17914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ArrayDelete = true; 17924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SourceLocation LHS = ConsumeBracket(); 17934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS); 17944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (RHS.isInvalid()) 179520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 17964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 17974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 179860d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult Operand(ParseCastExpression(false)); 17990e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl if (Operand.isInvalid()) 180020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return move(Operand); 18014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 18029ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take()); 18034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 180464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 18051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) { 180664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl switch(kind) { 180738c2b730a8553fa1cf369d0c5567f8b5d0a3dda8Francois Pichet default: llvm_unreachable("Not a known unary type trait"); 180864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign; 180964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy; 181064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor; 181164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign; 181264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy; 181364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor; 181464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor; 181564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor; 181664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_abstract: return UTT_IsAbstract; 181764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_class: return UTT_IsClass; 181864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_empty: return UTT_IsEmpty; 181964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_enum: return UTT_IsEnum; 182064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_pod: return UTT_IsPOD; 182164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_polymorphic: return UTT_IsPolymorphic; 182264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_union: return UTT_IsUnion; 1823ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl case tok::kw___is_literal: return UTT_IsLiteral; 182464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl } 18256ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet} 18266ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 18276ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichetstatic BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) { 18286ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet switch(kind) { 182938c2b730a8553fa1cf369d0c5567f8b5d0a3dda8Francois Pichet default: llvm_unreachable("Not a known binary type trait"); 1830f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet case tok::kw___is_base_of: return BTT_IsBaseOf; 1831f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible; 18329f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor case tok::kw___is_convertible_to: return BTT_IsConvertibleTo; 18336ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet } 183464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl} 183564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 183664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait 183764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits 183864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates. 183964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// 184064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// primary-expression: 184164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU] unary-type-trait '(' type-id ')' 184264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// 184360d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseUnaryTypeTrait() { 184464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind()); 184564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl SourceLocation Loc = ConsumeToken(); 184664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 184764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl SourceLocation LParen = Tok.getLocation(); 184864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen)) 184964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl return ExprError(); 185064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 185164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl // FIXME: Error reporting absolutely sucks! If the this fails to parse a type 185264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl // there will be cryptic errors about mismatched parentheses and missing 185364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl // specifiers. 1854809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor TypeResult Ty = ParseTypeName(); 185564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 185664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen); 185764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 1858809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor if (Ty.isInvalid()) 1859809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor return ExprError(); 1860809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor 18613d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen); 186264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl} 1863f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 18646ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// ParseBinaryTypeTrait - Parse the built-in binary type-trait 18656ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// pseudo-functions that allow implementation of the TR1/C++0x type traits 18666ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// templates. 18676ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// 18686ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// primary-expression: 18696ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// [GNU] binary-type-trait '(' type-id ',' type-id ')' 18706ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// 18716ad6f2848d7652ab2991286eb48be440d3493b28Francois PichetExprResult Parser::ParseBinaryTypeTrait() { 18726ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind()); 18736ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet SourceLocation Loc = ConsumeToken(); 18746ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 18756ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet SourceLocation LParen = Tok.getLocation(); 18766ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen)) 18776ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet return ExprError(); 18786ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 18796ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet TypeResult LhsTy = ParseTypeName(); 18806ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet if (LhsTy.isInvalid()) { 18816ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet SkipUntil(tok::r_paren); 18826ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet return ExprError(); 18836ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet } 18846ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 18856ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) { 18866ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet SkipUntil(tok::r_paren); 18876ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet return ExprError(); 18886ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet } 18896ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 18906ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet TypeResult RhsTy = ParseTypeName(); 18916ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet if (RhsTy.isInvalid()) { 18926ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet SkipUntil(tok::r_paren); 18936ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet return ExprError(); 18946ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet } 18956ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 18966ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen); 18976ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 18986ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen); 18996ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet} 19006ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 1901f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a 1902f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate 1903f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens. 190460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult 1905f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType, 1906b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType &CastTy, 1907f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis SourceLocation LParenLoc, 1908f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis SourceLocation &RParenLoc) { 1909f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis assert(getLang().CPlusPlus && "Should only be called for C++!"); 1910f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis assert(ExprType == CastExpr && "Compound literals are not ambiguous!"); 1911f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis assert(isTypeIdInParens() && "Not a type-id!"); 1912f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 191360d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult Result(true); 1914b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall CastTy = ParsedType(); 1915f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1916f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // We need to disambiguate a very ugly part of the C++ syntax: 1917f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // 1918f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // (T())x; - type-id 1919f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // (T())*x; - type-id 1920f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // (T())/x; - expression 1921f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // (T()); - expression 1922f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // 1923f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // The bad news is that we cannot use the specialized tentative parser, since 1924f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // it can only verify that the thing inside the parens can be parsed as 1925f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // type-id, it is not useful for determining the context past the parens. 1926f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // 1927f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // The good news is that the parser can disambiguate this part without 1928a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis // making any unnecessary Action calls. 1929f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // 1930f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // It uses a scheme similar to parsing inline methods. The parenthesized 1931f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // tokens are cached, the context that follows is determined (possibly by 1932f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // parsing a cast-expression), and then we re-introduce the cached tokens 1933f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // into the token stream and parse them appropriately. 1934f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 19351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ParenParseOption ParseAs; 1936f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis CachedTokens Toks; 1937f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1938f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Store the tokens of the parentheses. We will parse them after we determine 1939f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // the context that follows them. 194014b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) { 1941f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // We didn't find the ')' we expected. 1942f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis MatchRHSPunctuation(tok::r_paren, LParenLoc); 1943f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis return ExprError(); 1944f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis } 1945f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1946f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis if (Tok.is(tok::l_brace)) { 1947f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis ParseAs = CompoundLiteral; 1948f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis } else { 1949f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis bool NotCastExpr; 1950b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression 1951b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) { 1952b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman NotCastExpr = true; 1953b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman } else { 1954b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman // Try parsing the cast-expression that may follow. 1955b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman // If it is not a cast-expression, NotCastExpr will be true and no token 1956b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman // will be consumed. 1957b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman Result = ParseCastExpression(false/*isUnaryExpression*/, 1958b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman false/*isAddressofOperand*/, 1959b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall NotCastExpr, 1960b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType()/*TypeOfCast*/); 1961b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman } 1962f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1963f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // If we parsed a cast-expression, it's really a type-id, otherwise it's 1964f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // an expression. 1965f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis ParseAs = NotCastExpr ? SimpleExpr : CastExpr; 1966f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis } 1967f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 19681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // The current token should go after the cached tokens. 1969f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis Toks.push_back(Tok); 1970f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Re-enter the stored parenthesized tokens into the token stream, so we may 1971f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // parse them now. 1972f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis PP.EnterTokenStream(Toks.data(), Toks.size(), 1973f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis true/*DisableMacroExpansion*/, false/*OwnsTokens*/); 1974f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Drop the current token and bring the first cached one. It's the same token 1975f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // as when we entered this function. 1976f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis ConsumeAnyToken(); 1977f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1978f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis if (ParseAs >= CompoundLiteral) { 1979f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis TypeResult Ty = ParseTypeName(); 1980f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1981f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Match the ')'. 1982f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis if (Tok.is(tok::r_paren)) 1983f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis RParenLoc = ConsumeParen(); 1984f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis else 1985f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis MatchRHSPunctuation(tok::r_paren, LParenLoc); 1986f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1987f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis if (ParseAs == CompoundLiteral) { 1988f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis ExprType = CompoundLiteral; 1989f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc); 1990f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis } 19911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1992f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // We parsed '(' type-id ')' and the thing after it wasn't a '{'. 1993f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis assert(ParseAs == CastExpr); 1994f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1995f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis if (Ty.isInvalid()) 1996f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis return ExprError(); 1997f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1998f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis CastTy = Ty.get(); 1999f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 2000f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Result is what ParseCastExpression returned earlier. 2001f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis if (!Result.isInvalid()) 200223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc, 20039ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Result.take()); 2004f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis return move(Result); 2005f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis } 20061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2007f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Not a compound literal, and not followed by a cast-expression. 2008f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis assert(ParseAs == SimpleExpr); 2009f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 2010f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis ExprType = SimpleExpr; 2011f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis Result = ParseExpression(); 2012f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis if (!Result.isInvalid() && Tok.is(tok::r_paren)) 20139ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take()); 2014f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 2015f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // Match the ')'. 2016f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis if (Result.isInvalid()) { 2017f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis SkipUntil(tok::r_paren); 2018f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis return ExprError(); 2019f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis } 20201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2021f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis if (Tok.is(tok::r_paren)) 2022f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis RParenLoc = ConsumeParen(); 2023f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis else 2024f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis MatchRHSPunctuation(tok::r_paren, LParenLoc); 2025f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 2026f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis return move(Result); 2027f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis} 2028