ParseExprCXX.cpp revision 0486d746019f8310589b1f0d92edcc4bb3916b33
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" 16987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis#include "clang/Parse/DeclSpec.h" 17314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor#include "clang/Parse/Template.h" 183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h" 193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present. 232dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// 242dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which 251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or 262dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream. 27eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 28eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::'[opt] nested-name-specifier 29eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' 30eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 31eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// nested-name-specifier: 32eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// type-name '::' 33eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace-name '::' 34eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// nested-name-specifier identifier '::' 352dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier 'template'[opt] simple-template-id '::' 362dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// 372dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// 381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed 392dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty) 402dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// 411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following 422dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the 432dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed. 44eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 452dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of 462dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it. 472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// 482dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \returns true if a scope specifier was parsed. 49495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS, 502dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor Action::TypeTy *ObjectType, 51495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor bool EnteringContext) { 524bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis assert(getLang().CPlusPlus && 537452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner "Call sites of this function should be guarded by checking for C++"); 541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 55eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis if (Tok.is(tok::annot_cxxscope)) { 563507369940bfb269551bfa1fec812481f60e3552Douglas Gregor SS.setScopeRep(Tok.getAnnotationValue()); 57eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis SS.setRange(Tok.getAnnotationRange()); 58eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis ConsumeToken(); 594bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis return true; 60eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis } 61e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner 6239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor bool HasScopeSpecifier = false; 6339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor 645b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner if (Tok.is(tok::coloncolon)) { 655b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner // ::new and ::delete aren't nested-name-specifiers. 665b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner tok::TokenKind NextKind = NextToken().getKind(); 675b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner if (NextKind == tok::kw_new || NextKind == tok::kw_delete) 685b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner return false; 691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7055a7cefc846765ac7d142a63f773747a20518d71Chris Lattner // '::' - Global scope qualifier. 71357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner SourceLocation CCLoc = ConsumeToken(); 72357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner SS.setBeginLoc(CCLoc); 733507369940bfb269551bfa1fec812481f60e3552Douglas Gregor SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc)); 74357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner SS.setEndLoc(CCLoc); 7539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor HasScopeSpecifier = true; 76eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis } 77eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 7839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor while (true) { 792dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor if (HasScopeSpecifier) { 802dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // C++ [basic.lookup.classref]p5: 812dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // If the qualified-id has the form 823b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor // 832dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // ::class-name-or-namespace-name::... 843b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor // 852dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // the class-name-or-namespace-name is looked up in global scope as a 862dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // class-name or namespace-name. 872dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // 882dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // To implement this, we clear out the object type as soon as we've 892dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // seen a leading '::' or part of a nested-name-specifier. 902dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor ObjectType = 0; 9181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor 9281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor if (Tok.is(tok::code_completion)) { 9381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor // Code completion for a nested-name-specifier, where the code 9481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor // code completion token follows the '::'. 9581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext); 9681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor ConsumeToken(); 9781b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor } 982dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor } 991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // nested-name-specifier: 10177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner // nested-name-specifier 'template'[opt] simple-template-id '::' 10277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner 10377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner // Parse the optional 'template' keyword, then make sure we have 10477cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner // 'identifier <' after it. 10577cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner if (Tok.is(tok::kw_template)) { 1062dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // If we don't have a scope specifier or an object type, this isn't a 107eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman // nested-name-specifier, since they aren't allowed to start with 108eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman // 'template'. 1092dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor if (!HasScopeSpecifier && !ObjectType) 110eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman break; 111eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman 1127bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TentativeParsingAction TPA(*this); 11377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner SourceLocation TemplateKWLoc = ConsumeToken(); 114ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 115ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor UnqualifiedId TemplateName; 116ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (Tok.is(tok::identifier)) { 117ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the identifier. 1187bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 119ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor ConsumeToken(); 120ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } else if (Tok.is(tok::kw_operator)) { 121ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, 1227bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TemplateName)) { 1237bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TPA.Commit(); 124ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 1257bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor } 126ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 127ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId) { 128ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Diag(TemplateName.getSourceRange().getBegin(), 129ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor diag::err_id_after_template_in_nested_name_spec) 130ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor << TemplateName.getSourceRange(); 1317bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TPA.Commit(); 132ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 133ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 134ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } else { 1357bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TPA.Revert(); 13677cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner break; 13777cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner } 1381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1397bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor // If the next token is not '<', we have a qualified-id that refers 1407bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor // to a template name, such as T::template apply, but is not a 1417bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor // template-id. 1427bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor if (Tok.isNot(tok::less)) { 1437bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TPA.Revert(); 1447bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor break; 1457bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor } 1467bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor 1477bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor // Commit to parsing the template-id. 1487bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor TPA.Commit(); 1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateTy Template 150014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName, 151a481edb1b11c956a46cb42cd0dc4dd9851c10801Douglas Gregor ObjectType, EnteringContext); 152eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman if (!Template) 153eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman break; 154c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name, 155ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor &SS, TemplateName, TemplateKWLoc, false)) 156c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner break; 1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15877cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner continue; 15977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner } 1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) { 1621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // We have 16339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // 16439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // simple-template-id '::' 16539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // 16639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // So we need to check whether the simple-template-id is of the 167c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor // right kind (it should name a type or be dependent), and then 168c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor // convert it into a type within the nested-name-specifier. 1691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateIdAnnotation *TemplateId 17039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 1714bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis 1721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (TemplateId->Kind == TNK_Type_template || 173c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor TemplateId->Kind == TNK_Dependent_template_name) { 17431a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor AnnotateTemplateIdTokenAsType(&SS); 17539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor 1761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(Tok.is(tok::annot_typename) && 17739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor "AnnotateTemplateIdTokenAsType isn't working"); 17839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor Token TypeToken = Tok; 17939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor ConsumeToken(); 18039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); 18139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor SourceLocation CCLoc = ConsumeToken(); 1821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor if (!HasScopeSpecifier) { 18439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor SS.setBeginLoc(TypeToken.getLocation()); 18539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor HasScopeSpecifier = true; 18639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor } 1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18831a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor if (TypeToken.getAnnotationValue()) 18931a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor SS.setScopeRep( 1901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, 19131a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor TypeToken.getAnnotationValue(), 19231a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor TypeToken.getAnnotationRange(), 19331a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor CCLoc)); 19431a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor else 19531a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor SS.setScopeRep(0); 19639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor SS.setEndLoc(CCLoc); 19739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor continue; 19867b9e831943300ce54e564e601971828ce4def15Chris Lattner } 1991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20067b9e831943300ce54e564e601971828ce4def15Chris Lattner assert(false && "FIXME: Only type template names supported here"); 20139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor } 20239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor 2035c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner 2045c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // The rest of the nested-name-specifier possibilities start with 2055c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // tok::identifier. 2065c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner if (Tok.isNot(tok::identifier)) 2075c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner break; 2085c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner 2095c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner IdentifierInfo &II = *Tok.getIdentifierInfo(); 2105c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner 2115c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // nested-name-specifier: 2125c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // type-name '::' 2135c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // namespace-name '::' 2145c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // nested-name-specifier identifier '::' 2155c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner Token Next = NextToken(); 2165c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner if (Next.is(tok::coloncolon)) { 2175c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // We have an identifier followed by a '::'. Lookup this name 2185c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // as the name in a nested-name-specifier. 2195c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner SourceLocation IdLoc = ConsumeToken(); 2205c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); 2215c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner SourceLocation CCLoc = ConsumeToken(); 2221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2235c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner if (!HasScopeSpecifier) { 2245c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner SS.setBeginLoc(IdLoc); 2255c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner HasScopeSpecifier = true; 2265c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner } 2271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2285c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner if (SS.isInvalid()) 2295c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner continue; 2301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2315c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner SS.setScopeRep( 232495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II, 2332dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor ObjectType, EnteringContext)); 2345c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner SS.setEndLoc(CCLoc); 2355c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner continue; 2365c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner } 2371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2385c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // nested-name-specifier: 2395c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // type-name '<' 2405c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner if (Next.is(tok::less)) { 2415c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner TemplateTy Template; 242014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor UnqualifiedId TemplateName; 243014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateName.setIdentifier(&II, Tok.getLocation()); 244014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS, 245014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateName, 2462dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor ObjectType, 247495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor EnteringContext, 248495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor Template)) { 2495c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // We have found a template name, so annotate this this token 2505c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // with a template-id annotation. We do not permit the 2515c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // template-id to be translated into a type annotation, 2525c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // because some clients (e.g., the parsing of class template 2535c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // specializations) still want to see the original template-id 2545c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner // token. 255ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor ConsumeToken(); 256ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName, 257ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation(), false)) 258c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner break; 2595c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner continue; 2605c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner } 2615c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner } 2625c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner 26339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // We don't have any tokens that form the beginning of a 26439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // nested-name-specifier, so we're done. 26539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor break; 26639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor } 2671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor return HasScopeSpecifier; 269eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis} 270eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 271eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression. 272eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 273eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// id-expression: 274eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// unqualified-id 275eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// qualified-id 276eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 277eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// qualified-id: 278eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id 279eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' identifier 280eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' operator-function-id 281edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor/// '::' template-id 282eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 283eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not 284eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect: 285eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 286eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' conversion-function-id 287eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' '~' class-name 288eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 289eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics: 290eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 291eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {}; 292eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {} 293eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() { 294eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// :: A :: ~ C(); // Some Sema error about using destructor with a 295eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// // namespace. 296eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// :: ~ C(); // Some Parser error like 'unexpected ~'. 297eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// } 298eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 299eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like: 300eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 301eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// qualified-id: 302eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id 303eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::' unqualified-id 304eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 305eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the 306eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope. 307eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// 308ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a 309ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts, 310ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may 311ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear. 312ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// 313ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian RedlParser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) { 314eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis // qualified-id: 315eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id 316eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis // '::' unqualified-id 317eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis // 318eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis CXXScopeSpec SS; 3192dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); 32002a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor 32102a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor UnqualifiedId Name; 32202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor if (ParseUnqualifiedId(SS, 32302a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor /*EnteringContext=*/false, 32402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor /*AllowDestructorName=*/false, 32502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor /*AllowConstructorName=*/false, 3262d1c21414199a7452f122598189363a3922605b1Douglas Gregor /*ObjectType=*/0, 32702a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor Name)) 32802a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor return ExprError(); 329b681b61fea36618778b8030360e90e3f4641233bJohn McCall 330b681b61fea36618778b8030360e90e3f4641233bJohn McCall // This is only the direct operand of an & operator if it is not 331b681b61fea36618778b8030360e90e3f4641233bJohn McCall // followed by a postfix-expression suffix. 332b681b61fea36618778b8030360e90e3f4641233bJohn McCall if (isAddressOfOperand) { 333b681b61fea36618778b8030360e90e3f4641233bJohn McCall switch (Tok.getKind()) { 334b681b61fea36618778b8030360e90e3f4641233bJohn McCall case tok::l_square: 335b681b61fea36618778b8030360e90e3f4641233bJohn McCall case tok::l_paren: 336b681b61fea36618778b8030360e90e3f4641233bJohn McCall case tok::arrow: 337b681b61fea36618778b8030360e90e3f4641233bJohn McCall case tok::period: 338b681b61fea36618778b8030360e90e3f4641233bJohn McCall case tok::plusplus: 339b681b61fea36618778b8030360e90e3f4641233bJohn McCall case tok::minusminus: 340b681b61fea36618778b8030360e90e3f4641233bJohn McCall isAddressOfOperand = false; 341b681b61fea36618778b8030360e90e3f4641233bJohn McCall break; 342b681b61fea36618778b8030360e90e3f4641233bJohn McCall 343b681b61fea36618778b8030360e90e3f4641233bJohn McCall default: 344b681b61fea36618778b8030360e90e3f4641233bJohn McCall break; 345b681b61fea36618778b8030360e90e3f4641233bJohn McCall } 346b681b61fea36618778b8030360e90e3f4641233bJohn McCall } 34702a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor 34802a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren), 34902a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor isAddressOfOperand); 35002a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor 351eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis} 352eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another 3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type. 3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// postfix-expression: [C++ 5.2p1] 3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'dynamic_cast' '<' type-name '>' '(' expression ')' 3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'static_cast' '<' type-name '>' '(' expression ')' 3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'reinterpret_cast' '<' type-name '>' '(' expression ')' 3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'const_cast' '<' type-name '>' '(' expression ')' 3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 36220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXCasts() { 3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer tok::TokenKind Kind = Tok.getKind(); 3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CastName = 0; // For error messages 3655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Kind) { 3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: assert(0 && "Unknown C++ cast!"); abort(); 3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::kw_const_cast: CastName = "const_cast"; break; 3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break; 3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break; 3715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::kw_static_cast: CastName = "static_cast"; break; 3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation OpLoc = ConsumeToken(); 3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation LAngleBracketLoc = Tok.getLocation(); 3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName)) 37820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 380809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor TypeResult CastTy = ParseTypeName(); 3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation RAngleBracketLoc = Tok.getLocation(); 3825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3831ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) 38420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<"); 3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; 3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 38821e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName)) 38921e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis return ExprError(); 3905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 39121e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis OwningExprResult Result = ParseExpression(); 3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 39321e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis // Match the ')'. 39427591ff4fc64600fd67c5d81899e3efe5ef41a80Douglas Gregor RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 396809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor if (!Result.isInvalid() && !CastTy.isInvalid()) 39749badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor Result = Actions.ActOnCXXNamedCast(OpLoc, Kind, 398f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl LAngleBracketLoc, CastTy.get(), 399809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor RAngleBracketLoc, 400f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl LParenLoc, move(Result), RParenLoc); 4015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 40220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return move(Result); 4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 405c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression. 406c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// 407c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// postfix-expression: [C++ 5.2p1] 408c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// 'typeid' '(' expression ')' 409c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// 'typeid' '(' type-id ')' 410c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// 41120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXTypeid() { 412c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!"); 413c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 414c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl SourceLocation OpLoc = ConsumeToken(); 415c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl SourceLocation LParenLoc = Tok.getLocation(); 416c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl SourceLocation RParenLoc; 417c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 418c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl // typeid expressions are always parenthesized. 419c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, 420c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl "typeid")) 42120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 422c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 42315faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl OwningExprResult Result(Actions); 424c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 425c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl if (isTypeIdInParens()) { 426809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor TypeResult Ty = ParseTypeName(); 427c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 428c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl // Match the ')'. 429c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl MatchRHSPunctuation(tok::r_paren, LParenLoc); 430c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 431809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor if (Ty.isInvalid()) 43220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 433c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 434c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true, 435809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor Ty.get(), RParenLoc); 436c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl } else { 437e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor // C++0x [expr.typeid]p3: 4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // When typeid is applied to an expression other than an lvalue of a 4391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // polymorphic class type [...] The expression is an unevaluated 440e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor // operand (Clause 5). 441e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor // 4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Note that we can't tell whether the expression is an lvalue of a 443e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor // polymorphic class type until after we've parsed the expression, so 444ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor // we the expression is potentially potentially evaluated. 445ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor EnterExpressionEvaluationContext Unevaluated(Actions, 446ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor Action::PotentiallyPotentiallyEvaluated); 447c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl Result = ParseExpression(); 448c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 449c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl // Match the ')'. 4500e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl if (Result.isInvalid()) 451c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl SkipUntil(tok::r_paren); 452c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl else { 453c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl MatchRHSPunctuation(tok::r_paren, LParenLoc); 454c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 455c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false, 456effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl Result.release(), RParenLoc); 457c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl } 458c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl } 459c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 46020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return move(Result); 461c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl} 462c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl 4635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals. 4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 4655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// boolean-literal: [C++ 2.13.5] 4665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'true' 4675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'false' 46820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXBoolLiteral() { 4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer tok::TokenKind Kind = Tok.getKind(); 470f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind); 4715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 47250dd289f45738ed22b7583d52ed2525b927042ffChris Lattner 47350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression. 47450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// 47550dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// throw-expression: [C++ 15] 47650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// 'throw' assignment-expression[opt] 47720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseThrowExpression() { 47850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner assert(Tok.is(tok::kw_throw) && "Not throw!"); 47950dd289f45738ed22b7583d52ed2525b927042ffChris Lattner SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token. 48020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl 4812a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner // If the current token isn't the start of an assignment-expression, 4822a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner // then the expression is not present. This handles things like: 4832a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner // "C ? throw : (void)42", which is crazy but legal. 4842a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner switch (Tok.getKind()) { // FIXME: move this predicate somewhere common. 4852a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::semi: 4862a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::r_paren: 4872a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::r_square: 4882a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::r_brace: 4892a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::colon: 4902a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner case tok::comma: 491f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions)); 49250dd289f45738ed22b7583d52ed2525b927042ffChris Lattner 4932a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner default: 4942f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl OwningExprResult Expr(ParseAssignmentExpression()); 49520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl if (Expr.isInvalid()) return move(Expr); 496f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl return Actions.ActOnCXXThrow(ThrowLoc, move(Expr)); 4972a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner } 49850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner} 4994cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis 5004cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer. 5014cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// 5024cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is 5034cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which 5044cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called. 50520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXThis() { 5064cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis assert(Tok.is(tok::kw_this) && "Not 'this'!"); 5074cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis SourceLocation ThisLoc = ConsumeToken(); 508f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl return Actions.ActOnCXXThis(ThisLoc); 5094cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis} 510987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 511987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type. 512987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)") 513987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)") 514987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()"). 515987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// 516987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// postfix-expression: [C++ 5.2p1] 517987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] 518987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// typename-specifier '(' expression-list[opt] ')' [TODO] 519987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// 52020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult 52120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) { 522987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 5235ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get(); 524987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 525987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis assert(Tok.is(tok::l_paren) && "Expected '('!"); 526987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis SourceLocation LParenLoc = ConsumeParen(); 527987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 528a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl ExprVector Exprs(Actions); 529987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis CommaLocsTy CommaLocs; 530987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 531987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis if (Tok.isNot(tok::r_paren)) { 532987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis if (ParseExpressionList(Exprs, CommaLocs)) { 533987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis SkipUntil(tok::r_paren); 53420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 535987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis } 536987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis } 537987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 538987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis // Match the ')'. 539987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 540987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 541ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl // TypeRep could be null, if it references an invalid typedef. 542ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl if (!TypeRep) 543ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl return ExprError(); 544ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl 545987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&& 546987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis "Unexpected number of commas!"); 547f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep, 548f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl LParenLoc, move_arg(Exprs), 549beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad CommaLocs.data(), RParenLoc); 550987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis} 551987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis 55299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// ParseCXXCondition - if/switch/while condition expression. 55371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// 55471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// condition: 55571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// expression 55671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// type-specifier-seq declarator '=' assignment-expression 55771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] 55871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// '=' assignment-expression 55971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// 56099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param ExprResult if the condition was parsed as an expression, the 56199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed expression. 56299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// 56399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param DeclResult if the condition was parsed as a declaration, the 56499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed declaration. 56599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// 56699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \returns true if there was a parsing, false otherwise. 56799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregorbool Parser::ParseCXXCondition(OwningExprResult &ExprResult, 56899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor DeclPtrTy &DeclResult) { 56999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!isCXXConditionDeclaration()) { 57099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor ExprResult = ParseExpression(); // expression 57199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor DeclResult = DeclPtrTy(); 57299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return ExprResult.isInvalid(); 57399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor } 57471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 57571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis // type-specifier-seq 57671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis DeclSpec DS; 57771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis ParseSpecifierQualifierList(DS); 57871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 57971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis // declarator 58071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis Declarator DeclaratorInfo(DS, Declarator::ConditionContext); 58171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis ParseDeclarator(DeclaratorInfo); 58271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 58371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis // simple-asm-expr[opt] 58471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis if (Tok.is(tok::kw_asm)) { 585ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl SourceLocation Loc; 586ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl OwningExprResult AsmLabel(ParseSimpleAsm(&Loc)); 5870e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl if (AsmLabel.isInvalid()) { 58871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis SkipUntil(tok::semi); 58999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return true; 59071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis } 591effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl DeclaratorInfo.setAsmLabel(AsmLabel.release()); 592ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.SetRangeEnd(Loc); 59371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis } 59471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 59571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis // If attributes are present, parse them. 596ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl if (Tok.is(tok::kw___attribute)) { 597ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl SourceLocation Loc; 598bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt AttributeList *AttrList = ParseGNUAttributes(&Loc); 599ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.AddAttributes(AttrList, Loc); 600ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl } 60171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 60299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor // Type-check the declaration itself. 60399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope, 60499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor DeclaratorInfo); 60599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor DeclResult = Dcl.get(); 60699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor ExprResult = ExprError(); 60799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor 60871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis // '=' assignment-expression 60999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (Tok.is(tok::equal)) { 61099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor SourceLocation EqualLoc = ConsumeToken(); 61199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor OwningExprResult AssignExpr(ParseAssignmentExpression()); 61299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!AssignExpr.isInvalid()) 61399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Actions.AddInitializerToDecl(DeclResult, move(AssignExpr)); 61499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor } else { 61599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor // FIXME: C++0x allows a braced-init-list 61699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Diag(Tok, diag::err_expected_equal_after_declarator); 61799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor } 61899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor 61999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return false; 62071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis} 62171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis 622987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers. 623987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of 624987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier. 625987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// 626987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier: 627eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// '::'[opt] nested-name-specifier[opt] type-name 628987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO] 629987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// char 630987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// wchar_t 631987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// bool 632987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// short 633987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// int 634987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// long 635987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// signed 636987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// unsigned 637987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// float 638987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// double 639987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// void 640987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU] typeof-specifier 641987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto [TODO] 642987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// 643987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// type-name: 644987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// class-name 645987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// enum-name 646987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// typedef-name 647987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// 648987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) { 649987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis DS.SetRangeStart(Tok.getLocation()); 650987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis const char *PrevSpec; 651fec54013fcd0eb72642741584ca04c1bc292bef8John McCall unsigned DiagID; 652987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis SourceLocation Loc = Tok.getLocation(); 6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 654987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis switch (Tok.getKind()) { 65555a7cefc846765ac7d142a63f773747a20518d71Chris Lattner case tok::identifier: // foo::bar 65655a7cefc846765ac7d142a63f773747a20518d71Chris Lattner case tok::coloncolon: // ::foo::bar 65755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner assert(0 && "Annotation token should already be formed!"); 6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump default: 659987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis assert(0 && "Not a simple-type-specifier token!"); 660987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis abort(); 66155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner 662987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis // type-name 663b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner case tok::annot_typename: { 664fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, 665eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis Tok.getAnnotationValue()); 666987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 667987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis } 6681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 669987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis // builtin types 670987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_short: 671fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); 672987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 673987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_long: 674fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID); 675987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 676987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_signed: 677fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); 678987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 679987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_unsigned: 680fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID); 681987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 682987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_void: 683fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); 684987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 685987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_char: 686fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); 687987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 688987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_int: 689fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); 690987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 691987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_float: 692fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); 693987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 694987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_double: 695fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); 696987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 697987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_wchar_t: 698fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); 699987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 700f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case tok::kw_char16_t: 701fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); 702f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith break; 703f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case tok::kw_char32_t: 704fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); 705f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith break; 706987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_bool: 707fec54013fcd0eb72642741584ca04c1bc292bef8John McCall DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); 708987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis break; 7091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 710987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis // GNU typeof support. 711987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case tok::kw_typeof: 712987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis ParseTypeofSpecifier(DS); 7139b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor DS.Finish(Diags, PP); 714987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis return; 715987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis } 716b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner if (Tok.is(tok::annot_typename)) 717eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis DS.SetRangeEnd(Tok.getAnnotationEndLoc()); 718eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis else 719eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis DS.SetRangeEnd(Tok.getLocation()); 720987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis ConsumeToken(); 7219b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor DS.Finish(Diags, PP); 722987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis} 7231cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor 7242f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++ 7252f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers, 7262f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished 7272f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are 7282f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and 7292f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false 7302f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise. 7312f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// 7322f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// type-specifier-seq: [C++ 8.1] 7332f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// type-specifier type-specifier-seq[opt] 7342f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// 7352f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) { 7362f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor DS.SetRangeStart(Tok.getLocation()); 7372f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor const char *PrevSpec = 0; 738fec54013fcd0eb72642741584ca04c1bc292bef8John McCall unsigned DiagID; 739fec54013fcd0eb72642741584ca04c1bc292bef8John McCall bool isInvalid = 0; 7402f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor 7412f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor // Parse one or more of the type specifiers. 742fec54013fcd0eb72642741584ca04c1bc292bef8John McCall if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) { 7431ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner Diag(Tok, diag::err_operator_missing_type_specifier); 7442f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor return true; 7452f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor } 7461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 747fec54013fcd0eb72642741584ca04c1bc292bef8John McCall while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ; 7482f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor 7492f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor return false; 7502f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor} 7512f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor 7523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of 7533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form. 7543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 7553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or 7563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine 7573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will 7583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to 7593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller. 7603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 7613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if 7623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id. 7633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 7643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual 7653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name. 7663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 7673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or 7683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor. 7693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 7703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the 7713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier. 7723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 77346df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access 77446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed. 77546df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// 7763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id 7773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully, 7783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id. 7793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 7803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise. 7813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS, 7823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor IdentifierInfo *Name, 7833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor SourceLocation NameLoc, 7843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor bool EnteringContext, 7852d1c21414199a7452f122598189363a3922605b1Douglas Gregor TypeTy *ObjectType, 7863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor UnqualifiedId &Id) { 7873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id"); 7883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 7893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateTy Template; 7903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateNameKind TNK = TNK_Non_template; 7913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor switch (Id.getKind()) { 7923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor case UnqualifiedId::IK_Identifier: 793014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor case UnqualifiedId::IK_OperatorFunctionId: 794014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext, 795014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor Template); 7963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor break; 7973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 798014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor case UnqualifiedId::IK_ConstructorName: { 799014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor UnqualifiedId TemplateName; 800014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateName.setIdentifier(Name, NameLoc); 801014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType, 8022d1c21414199a7452f122598189363a3922605b1Douglas Gregor EnteringContext, Template); 8033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor break; 804014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor } 8053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 806014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor case UnqualifiedId::IK_DestructorName: { 807014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor UnqualifiedId TemplateName; 808014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateName.setIdentifier(Name, NameLoc); 8092d1c21414199a7452f122598189363a3922605b1Douglas Gregor if (ObjectType) { 810014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS, 811a481edb1b11c956a46cb42cd0dc4dd9851c10801Douglas Gregor TemplateName, ObjectType, 812a481edb1b11c956a46cb42cd0dc4dd9851c10801Douglas Gregor EnteringContext); 8132d1c21414199a7452f122598189363a3922605b1Douglas Gregor TNK = TNK_Dependent_template_name; 8142d1c21414199a7452f122598189363a3922605b1Douglas Gregor if (!Template.get()) 8152d1c21414199a7452f122598189363a3922605b1Douglas Gregor return true; 8162d1c21414199a7452f122598189363a3922605b1Douglas Gregor } else { 817014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType, 8182d1c21414199a7452f122598189363a3922605b1Douglas Gregor EnteringContext, Template); 8192d1c21414199a7452f122598189363a3922605b1Douglas Gregor 8202d1c21414199a7452f122598189363a3922605b1Douglas Gregor if (TNK == TNK_Non_template && Id.DestructorName == 0) { 8212d1c21414199a7452f122598189363a3922605b1Douglas Gregor // The identifier following the destructor did not refer to a template 8222d1c21414199a7452f122598189363a3922605b1Douglas Gregor // or to a type. Complain. 8232d1c21414199a7452f122598189363a3922605b1Douglas Gregor if (ObjectType) 8242d1c21414199a7452f122598189363a3922605b1Douglas Gregor Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type) 8252d1c21414199a7452f122598189363a3922605b1Douglas Gregor << Name; 8262d1c21414199a7452f122598189363a3922605b1Douglas Gregor else 8272d1c21414199a7452f122598189363a3922605b1Douglas Gregor Diag(NameLoc, diag::err_destructor_class_name); 8282d1c21414199a7452f122598189363a3922605b1Douglas Gregor return true; 8292d1c21414199a7452f122598189363a3922605b1Douglas Gregor } 8302d1c21414199a7452f122598189363a3922605b1Douglas Gregor } 8313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor break; 832014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor } 8333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor default: 8353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 8363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 8373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (TNK == TNK_Non_template) 8393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 8403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Parse the enclosed template argument list. 8423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor SourceLocation LAngleLoc, RAngleLoc; 8433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateArgList TemplateArgs; 8443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation, 8453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor &SS, true, LAngleLoc, 8463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateArgs, 8473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor RAngleLoc)) 8483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 8493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Id.getKind() == UnqualifiedId::IK_Identifier || 8513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) { 8523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Form a parsed representation of the template-id to be stored in the 8533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // UnqualifiedId. 8543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateIdAnnotation *TemplateId 8553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor = TemplateIdAnnotation::Allocate(TemplateArgs.size()); 8563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Id.getKind() == UnqualifiedId::IK_Identifier) { 8583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->Name = Id.Identifier; 859014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateId->Operator = OO_None; 8603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->TemplateNameLoc = Id.StartLocation; 8613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } else { 862014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateId->Name = 0; 863014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateId->Operator = Id.OperatorFunctionId.Operator; 864014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateId->TemplateNameLoc = Id.StartLocation; 8653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 8663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->Template = Template.getAs<void*>(); 8683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->Kind = TNK; 8693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->LAngleLoc = LAngleLoc; 8703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateId->RAngleLoc = RAngleLoc; 871314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor ParsedTemplateArgument *Args = TemplateId->getTemplateArgs(); 8723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); 873314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor Arg != ArgEnd; ++Arg) 8743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Args[Arg] = TemplateArgs[Arg]; 8753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Id.setTemplateId(TemplateId); 8773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 8783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 8793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Bundle the template arguments together. 8813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), 8823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor TemplateArgs.size()); 8833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Constructor and destructor names. 8853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Action::TypeResult Type 8863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor = Actions.ActOnTemplateIdType(Template, NameLoc, 8873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor LAngleLoc, TemplateArgsPtr, 8883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor RAngleLoc); 8893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Type.isInvalid()) 8903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 8913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Id.getKind() == UnqualifiedId::IK_ConstructorName) 8933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Id.setConstructorName(Type.get(), NameLoc, RAngleLoc); 8943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor else 8953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc); 8963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 8973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 8983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor} 8993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 900ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part 901ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id. 9023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 903ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or 904ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way. 9053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 906ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code 9073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id: [C++ 13.5] 9083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 'operator' operator 9093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 910ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// operator: one of 9113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// new delete new[] delete[] 9123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// + - * / % ^ & | ~ 9133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// ! = < > += -= *= /= %= 9143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// ^= &= |= << >> >>= <<= == != 9153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// <= >= && || ++ -- , ->* -> 9163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// () [] 9173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 9183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// conversion-function-id: [C++ 12.3.2] 9193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator conversion-type-id 9203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 9213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// conversion-type-id: 9223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// type-specifier-seq conversion-declarator[opt] 9233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 9243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// conversion-declarator: 9253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// ptr-operator conversion-declarator[opt] 9263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode 9273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 9283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If 9293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id. 9303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 9313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the 9323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier. 9333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 934ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access 935ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed. 936ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 937ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id. 938ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 939ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise. 940ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext, 941ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor TypeTy *ObjectType, 942ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor UnqualifiedId &Result) { 943ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); 944ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 945ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the 'operator' keyword. 946ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation KeywordLoc = ConsumeToken(); 947ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 948ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Determine what kind of operator name we have. 949ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor unsigned SymbolIdx = 0; 950ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation SymbolLocations[3]; 951ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor OverloadedOperatorKind Op = OO_None; 952ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor switch (Tok.getKind()) { 953ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::kw_new: 954ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::kw_delete: { 955ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor bool isNew = Tok.getKind() == tok::kw_new; 956ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the 'new' or 'delete'. 957ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = ConsumeToken(); 958ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (Tok.is(tok::l_square)) { 959ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the '['. 960ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation LBracketLoc = ConsumeBracket(); 961ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the ']'. 962ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square, 963ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor LBracketLoc); 964ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (RBracketLoc.isInvalid()) 965ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 966ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 967ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = LBracketLoc; 968ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = RBracketLoc; 969ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Op = isNew? OO_Array_New : OO_Array_Delete; 970ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } else { 971ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Op = isNew? OO_New : OO_Delete; 972ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 973ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 974ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 975ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 976ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 977ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::Token: \ 978ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = ConsumeToken(); \ 979ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Op = OO_##Name; \ 980ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 981ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) 982ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def" 983ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 984ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::l_paren: { 985ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the '('. 986ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation LParenLoc = ConsumeParen(); 987ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the ')'. 988ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, 989ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor LParenLoc); 990ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (RParenLoc.isInvalid()) 991ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 992ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 993ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = LParenLoc; 994ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = RParenLoc; 995ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Op = OO_Call; 996ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 997ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 998ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 999ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::l_square: { 1000ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the '['. 1001ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation LBracketLoc = ConsumeBracket(); 1002ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the ']'. 1003ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square, 1004ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor LBracketLoc); 1005ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (RBracketLoc.isInvalid()) 1006ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 1007ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1008ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = LBracketLoc; 1009ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor SymbolLocations[SymbolIdx++] = RBracketLoc; 1010ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Op = OO_Subscript; 1011ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 1012ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 1013ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1014ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor case tok::code_completion: { 1015ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Code completion for the operator name. 1016ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Actions.CodeCompleteOperatorName(CurScope); 1017ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1018ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the operator token. 1019ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor ConsumeToken(); 1020ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1021ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Don't try to parse any further. 1022ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 1023ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 1024ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1025ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor default: 1026ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor break; 1027ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 1028ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1029ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (Op != OO_None) { 1030ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // We have parsed an operator-function-id. 1031ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations); 1032ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return false; 1033ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 10340486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt 10350486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt // Parse a literal-operator-id. 10360486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt // 10370486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt // literal-operator-id: [C++0x 13.5.8] 10380486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt // operator "" identifier 10390486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt 10400486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) { 10410486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt if (Tok.getLength() != 2) 10420486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt Diag(Tok.getLocation(), diag::err_operator_string_not_empty); 10430486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt ConsumeStringToken(); 10440486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt 10450486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt if (Tok.isNot(tok::identifier)) { 10460486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt Diag(Tok.getLocation(), diag::err_expected_ident); 10470486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt return true; 10480486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt } 10490486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt 10500486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt IdentifierInfo *II = Tok.getIdentifierInfo(); 10510486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken()); 10520486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt Diag(KeywordLoc, diag::err_unsupported_literal_operator); 10530486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt return true; 10540486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt } 1055ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1056ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Parse a conversion-function-id. 1057ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // 1058ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // conversion-function-id: [C++ 12.3.2] 1059ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // operator conversion-type-id 1060ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // 1061ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // conversion-type-id: 1062ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // type-specifier-seq conversion-declarator[opt] 1063ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // 1064ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // conversion-declarator: 1065ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // ptr-operator conversion-declarator[opt] 1066ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1067ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Parse the type-specifier-seq. 1068ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor DeclSpec DS; 1069f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType? 1070ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 1071ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1072ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Parse the conversion-declarator, which is merely a sequence of 1073ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // ptr-operators. 1074ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Declarator D(DS, Declarator::TypeNameContext); 1075ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor ParseDeclaratorInternal(D, /*DirectDeclParser=*/0); 1076ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1077ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Finish up the type. 1078ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D); 1079ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (Ty.isInvalid()) 1080ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return true; 1081ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1082ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Note that this is a conversion-function-id. 1083ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Result.setConversionFunctionId(KeywordLoc, Ty.get(), 1084ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor D.getSourceRange().getEnd()); 1085ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return false; 1086ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor} 1087ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor 1088ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the 1089ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity. 1090ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 1091ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code 1092ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// unqualified-id: [C++ expr.prim.general] 1093ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// identifier 1094ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// operator-function-id 1095ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id 1096ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO] 1097ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// ~ class-name 1098ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template-id 1099ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 1100ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode 1101ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 1102ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If 1103ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id. 1104ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 1105ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the 1106ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier. 1107ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// 11083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name. 11093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 11103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name. 11113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 111246df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access 111346df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed. 111446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// 11153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id. 11163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// 11173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise. 11183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext, 11193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor bool AllowDestructorName, 11203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor bool AllowConstructorName, 11212d1c21414199a7452f122598189363a3922605b1Douglas Gregor TypeTy *ObjectType, 11223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor UnqualifiedId &Result) { 11233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // unqualified-id: 11243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // identifier 11253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // template-id (when it hasn't already been annotated) 11263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Tok.is(tok::identifier)) { 11273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Consume the identifier. 11283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor IdentifierInfo *Id = Tok.getIdentifierInfo(); 11293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor SourceLocation IdLoc = ConsumeToken(); 11303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (AllowConstructorName && 11323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Actions.isCurrentClassName(*Id, CurScope, &SS)) { 11333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // We have parsed a constructor name. 11343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope, 11353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor &SS, false), 11363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor IdLoc, IdLoc); 11373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } else { 11383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // We have parsed an identifier. 11393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Result.setIdentifier(Id, IdLoc); 11403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 11413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // If the next token is a '<', we may have a template. 11433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Tok.is(tok::less)) 11443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext, 11452d1c21414199a7452f122598189363a3922605b1Douglas Gregor ObjectType, Result); 11463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 11483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 11493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // unqualified-id: 11513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // template-id (already parsed and annotated) 11523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Tok.is(tok::annot_template_id)) { 11533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // FIXME: Could this be a constructor name??? 11543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // We have already parsed a template-id; consume the annotation token as 11563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // our unqualified-id. 11573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Result.setTemplateId( 11583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue())); 11593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor ConsumeToken(); 11603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 11613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 11623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // unqualified-id: 11643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // operator-function-id 11653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // conversion-function-id 11663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Tok.is(tok::kw_operator)) { 1167ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result)) 11683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 11693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 1170ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // If we have an operator-function-id and the next token is a '<', we may 1171ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // have a 1172ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // 1173ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // template-id: 1174ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // operator-function-id < template-argument-list[opt] > 1175ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor if (Result.getKind() == UnqualifiedId::IK_OperatorFunctionId && 1176ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Tok.is(tok::less)) 1177ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(), 1178ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor EnteringContext, ObjectType, 1179ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor Result); 11803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 11823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 11833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) { 11853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // C++ [expr.unary.op]p10: 11863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // There is an ambiguity in the unary-expression ~X(), where X is a 11873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // class-name. The ambiguity is resolved in favor of treating ~ as a 11883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // unary complement rather than treating ~X as referring to a destructor. 11893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Parse the '~'. 11913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor SourceLocation TildeLoc = ConsumeToken(); 11923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Parse the class-name. 11943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (Tok.isNot(tok::identifier)) { 11953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Diag(Tok, diag::err_destructor_class_name); 11963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 11973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 11983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 11993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Parse the class-name (or template-name in a simple-template-id). 12003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor IdentifierInfo *ClassName = Tok.getIdentifierInfo(); 12013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor SourceLocation ClassNameLoc = ConsumeToken(); 12023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 12032d1c21414199a7452f122598189363a3922605b1Douglas Gregor if (Tok.is(tok::less)) { 12042d1c21414199a7452f122598189363a3922605b1Douglas Gregor Result.setDestructorName(TildeLoc, 0, ClassNameLoc); 12052d1c21414199a7452f122598189363a3922605b1Douglas Gregor return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc, 12062d1c21414199a7452f122598189363a3922605b1Douglas Gregor EnteringContext, ObjectType, Result); 12072d1c21414199a7452f122598189363a3922605b1Douglas Gregor } 12082d1c21414199a7452f122598189363a3922605b1Douglas Gregor 12093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor // Note that this is a destructor name. 12103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc, 1211f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor CurScope, &SS, false, ObjectType); 12123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor if (!Ty) { 12132d1c21414199a7452f122598189363a3922605b1Douglas Gregor if (ObjectType) 12142d1c21414199a7452f122598189363a3922605b1Douglas Gregor Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type) 12152d1c21414199a7452f122598189363a3922605b1Douglas Gregor << ClassName; 12162d1c21414199a7452f122598189363a3922605b1Douglas Gregor else 12172d1c21414199a7452f122598189363a3922605b1Douglas Gregor Diag(ClassNameLoc, diag::err_destructor_class_name); 12183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 12193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 12203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 12213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor Result.setDestructorName(TildeLoc, Ty, ClassNameLoc); 12223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return false; 12233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor } 12243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 12252d1c21414199a7452f122598189363a3922605b1Douglas Gregor Diag(Tok, diag::err_expected_unqualified_id) 12262d1c21414199a7452f122598189363a3922605b1Douglas Gregor << getLang().CPlusPlus; 12273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor return true; 12283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor} 12293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor 12304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate 12314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors. 12321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 123359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has 123459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed. If the :: was present, "UseGlobal" is true and "Start" 123559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location. Otherwise, "Start" is the location of the 'new' token. 12364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 12374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-expression: 12384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '::'[opt] 'new' new-placement[opt] new-type-id 12394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-initializer[opt] 12404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '::'[opt] 'new' new-placement[opt] '(' type-id ')' 12414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-initializer[opt] 12424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 12434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-placement: 12444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '(' expression-list ')' 12454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 1246cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// new-type-id: 1247cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// type-specifier-seq new-declarator[opt] 1248cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// 1249cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// new-declarator: 1250cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// ptr-operator new-declarator[opt] 1251cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// direct-new-declarator 1252cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl/// 12534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-initializer: 12544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '(' expression-list[opt] ')' 12554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// [C++0x] braced-init-list [TODO] 12564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 125759232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::OwningExprResult 125859232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) { 125959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner assert(Tok.is(tok::kw_new) && "expected 'new' token"); 126059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner ConsumeToken(); // Consume 'new' 12614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 12624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // A '(' now can be a new-placement or the '(' wrapping the type-id in the 12634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // second form of new-expression. It can't be a new-type-id. 12644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1265a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl ExprVector PlacementArgs(Actions); 12664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SourceLocation PlacementLParen, PlacementRParen; 12674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 12684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl bool ParenTypeId; 1269cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl DeclSpec DS; 1270cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 12714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (Tok.is(tok::l_paren)) { 12724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // If it turns out to be a placement, we change the type location. 12734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl PlacementLParen = ConsumeParen(); 1274cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) { 1275cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); 127620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 1277cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl } 12784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 12794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen); 1280cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (PlacementRParen.isInvalid()) { 1281cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); 128220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 1283cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl } 12844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1285cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (PlacementArgs.empty()) { 12864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Reset the placement locations. There was no placement. 12874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl PlacementLParen = PlacementRParen = SourceLocation(); 12884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ParenTypeId = true; 12894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } else { 12904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // We still need the type. 12914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (Tok.is(tok::l_paren)) { 1292cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SourceLocation LParen = ConsumeParen(); 1293cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseSpecifierQualifierList(DS); 1294ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.SetSourceRange(DS.getSourceRange()); 1295cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseDeclarator(DeclaratorInfo); 1296cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl MatchRHSPunctuation(tok::r_paren, LParen); 12974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ParenTypeId = true; 12984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } else { 1299cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (ParseCXXTypeSpecifierSeq(DS)) 1300cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl DeclaratorInfo.setInvalidType(true); 1301ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl else { 1302ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.SetSourceRange(DS.getSourceRange()); 1303cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseDeclaratorInternal(DeclaratorInfo, 1304cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl &Parser::ParseDirectNewDeclarator); 1305ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl } 13064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ParenTypeId = false; 13074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 13084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 13094c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } else { 1310cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl // A new-type-id is a simplified type-id, where essentially the 1311cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl // direct-declarator is replaced by a direct-new-declarator. 1312cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (ParseCXXTypeSpecifierSeq(DS)) 1313cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl DeclaratorInfo.setInvalidType(true); 1314ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl else { 1315ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl DeclaratorInfo.SetSourceRange(DS.getSourceRange()); 1316cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseDeclaratorInternal(DeclaratorInfo, 1317cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl &Parser::ParseDirectNewDeclarator); 1318ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl } 13194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ParenTypeId = false; 13204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 1321eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner if (DeclaratorInfo.isInvalidType()) { 1322cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); 132320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 1324cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl } 13254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1326a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl ExprVector ConstructorArgs(Actions); 13274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SourceLocation ConstructorLParen, ConstructorRParen; 13284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 13294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (Tok.is(tok::l_paren)) { 13304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ConstructorLParen = ConsumeParen(); 13314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (Tok.isNot(tok::r_paren)) { 13324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl CommaLocsTy CommaLocs; 1333cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (ParseExpressionList(ConstructorArgs, CommaLocs)) { 1334cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); 133520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 1336cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl } 13374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 13384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen); 1339cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl if (ConstructorRParen.isInvalid()) { 1340cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); 134120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 1342cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl } 13434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 13444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1345f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen, 1346f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl move_arg(PlacementArgs), PlacementRParen, 1347f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl ParenTypeId, DeclaratorInfo, ConstructorLParen, 1348f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl move_arg(ConstructorArgs), ConstructorRParen); 13494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 13504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 13514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be 13524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal. 13534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 13544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// direct-new-declarator: 13554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '[' expression ']' 13564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// direct-new-declarator '[' constant-expression ']' 13574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 135859232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) { 13594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Parse the array dimensions. 13604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl bool first = true; 13614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl while (Tok.is(tok::l_square)) { 13624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SourceLocation LLoc = ConsumeBracket(); 13632f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl OwningExprResult Size(first ? ParseExpression() 13642f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl : ParseConstantExpression()); 13650e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl if (Size.isInvalid()) { 13664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Recover 13674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SkipUntil(tok::r_square); 13684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl return; 13694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 13704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl first = false; 13714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1372ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc); 13734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false, 13747e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Size.release(), LLoc, RLoc), 1375ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl RLoc); 13764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1377ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl if (RLoc.isInvalid()) 13784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl return; 13794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 13804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 13814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 13824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id. 13834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator. 13844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 13854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-expression: 13864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '::'[opt] 'new' new-placement[opt] '(' type-id ')' 13874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-initializer[opt] 13884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 13894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// new-placement: 13904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '(' expression-list ')' 13914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 1392cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redlbool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs, 139359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner Declarator &D) { 13944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // The '(' was already consumed. 13954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (isTypeIdInParens()) { 1396cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseSpecifierQualifierList(D.getMutableDeclSpec()); 1397ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl D.SetSourceRange(D.getDeclSpec().getSourceRange()); 1398cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ParseDeclarator(D); 1399eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner return D.isInvalidType(); 14004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 14014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 14024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // It's not a type, it has to be an expression list. 14034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Discard the comma locations - ActOnCXXNew has enough parameters. 14044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl CommaLocsTy CommaLocs; 14054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl return ParseExpressionList(PlacementArgs, CommaLocs); 14064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 14074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 14084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used 14094c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new. 14104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// 141159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional 141259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true 141359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location. Otherwise, "Start" is the location of the 141459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token. 141559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 14164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// delete-expression: 14174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '::'[opt] 'delete' cast-expression 14184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// '::'[opt] 'delete' '[' ']' cast-expression 141959232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::OwningExprResult 142059232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) { 142159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword"); 142259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner ConsumeToken(); // Consume 'delete' 14234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 14244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Array delete? 14254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl bool ArrayDelete = false; 14264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (Tok.is(tok::l_square)) { 14274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ArrayDelete = true; 14284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SourceLocation LHS = ConsumeBracket(); 14294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS); 14304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl if (RHS.isInvalid()) 143120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return ExprError(); 14324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 14334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 14342f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl OwningExprResult Operand(ParseCastExpression(false)); 14350e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl if (Operand.isInvalid()) 143620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl return move(Operand); 14374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 1438f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand)); 14394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 144064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 14411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) { 144264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl switch(kind) { 144364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl default: assert(false && "Not a known unary type trait."); 144464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign; 144564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy; 144664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor; 144764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign; 144864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy; 144964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor; 145064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor; 145164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor; 145264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_abstract: return UTT_IsAbstract; 145364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_class: return UTT_IsClass; 145464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_empty: return UTT_IsEmpty; 145564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_enum: return UTT_IsEnum; 145664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_pod: return UTT_IsPOD; 145764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_polymorphic: return UTT_IsPolymorphic; 145864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case tok::kw___is_union: return UTT_IsUnion; 145964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl } 146064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl} 146164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 146264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait 146364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits 146464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates. 146564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// 146664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// primary-expression: 146764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU] unary-type-trait '(' type-id ')' 146864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// 14691eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParser::OwningExprResult Parser::ParseUnaryTypeTrait() { 147064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind()); 147164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl SourceLocation Loc = ConsumeToken(); 147264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 147364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl SourceLocation LParen = Tok.getLocation(); 147464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen)) 147564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl return ExprError(); 147664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 147764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl // FIXME: Error reporting absolutely sucks! If the this fails to parse a type 147864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl // there will be cryptic errors about mismatched parentheses and missing 147964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl // specifiers. 1480809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor TypeResult Ty = ParseTypeName(); 148164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 148264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen); 148364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 1484809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor if (Ty.isInvalid()) 1485809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor return ExprError(); 1486809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor 1487809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen); 148864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl} 1489f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1490f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a 1491f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate 1492f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens. 1493f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::OwningExprResult 1494f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType, 1495f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis TypeTy *&CastTy, 1496f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis SourceLocation LParenLoc, 1497f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis SourceLocation &RParenLoc) { 1498f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis assert(getLang().CPlusPlus && "Should only be called for C++!"); 1499f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis assert(ExprType == CastExpr && "Compound literals are not ambiguous!"); 1500f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis assert(isTypeIdInParens() && "Not a type-id!"); 1501f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1502f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis OwningExprResult Result(Actions, true); 1503f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis CastTy = 0; 1504f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1505f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // We need to disambiguate a very ugly part of the C++ syntax: 1506f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // 1507f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // (T())x; - type-id 1508f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // (T())*x; - type-id 1509f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // (T())/x; - expression 1510f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // (T()); - expression 1511f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // 1512f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // The bad news is that we cannot use the specialized tentative parser, since 1513f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // it can only verify that the thing inside the parens can be parsed as 1514f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // type-id, it is not useful for determining the context past the parens. 1515f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // 1516f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // The good news is that the parser can disambiguate this part without 1517a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis // making any unnecessary Action calls. 1518f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // 1519f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // It uses a scheme similar to parsing inline methods. The parenthesized 1520f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // tokens are cached, the context that follows is determined (possibly by 1521f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // parsing a cast-expression), and then we re-introduce the cached tokens 1522f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // into the token stream and parse them appropriately. 1523f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 15241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ParenParseOption ParseAs; 1525f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis CachedTokens Toks; 1526f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1527f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Store the tokens of the parentheses. We will parse them after we determine 1528f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // the context that follows them. 1529f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) { 1530f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // We didn't find the ')' we expected. 1531f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis MatchRHSPunctuation(tok::r_paren, LParenLoc); 1532f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis return ExprError(); 1533f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis } 1534f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1535f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis if (Tok.is(tok::l_brace)) { 1536f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis ParseAs = CompoundLiteral; 1537f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis } else { 1538f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis bool NotCastExpr; 1539b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression 1540b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) { 1541b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman NotCastExpr = true; 1542b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman } else { 1543b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman // Try parsing the cast-expression that may follow. 1544b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman // If it is not a cast-expression, NotCastExpr will be true and no token 1545b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman // will be consumed. 1546b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman Result = ParseCastExpression(false/*isUnaryExpression*/, 1547b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman false/*isAddressofOperand*/, 15482ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman NotCastExpr, false); 1549b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman } 1550f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1551f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // If we parsed a cast-expression, it's really a type-id, otherwise it's 1552f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // an expression. 1553f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis ParseAs = NotCastExpr ? SimpleExpr : CastExpr; 1554f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis } 1555f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 15561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // The current token should go after the cached tokens. 1557f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis Toks.push_back(Tok); 1558f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Re-enter the stored parenthesized tokens into the token stream, so we may 1559f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // parse them now. 1560f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis PP.EnterTokenStream(Toks.data(), Toks.size(), 1561f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis true/*DisableMacroExpansion*/, false/*OwnsTokens*/); 1562f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Drop the current token and bring the first cached one. It's the same token 1563f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // as when we entered this function. 1564f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis ConsumeAnyToken(); 1565f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1566f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis if (ParseAs >= CompoundLiteral) { 1567f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis TypeResult Ty = ParseTypeName(); 1568f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1569f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Match the ')'. 1570f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis if (Tok.is(tok::r_paren)) 1571f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis RParenLoc = ConsumeParen(); 1572f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis else 1573f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis MatchRHSPunctuation(tok::r_paren, LParenLoc); 1574f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1575f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis if (ParseAs == CompoundLiteral) { 1576f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis ExprType = CompoundLiteral; 1577f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc); 1578f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis } 15791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1580f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // We parsed '(' type-id ')' and the thing after it wasn't a '{'. 1581f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis assert(ParseAs == CastExpr); 1582f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1583f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis if (Ty.isInvalid()) 1584f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis return ExprError(); 1585f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1586f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis CastTy = Ty.get(); 1587f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis 1588f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Result is what ParseCastExpression returned earlier. 1589f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis if (!Result.isInvalid()) 15901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc, 15912ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman move(Result)); 1592f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis return move(Result); 1593f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis } 15941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1595f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis // Not a compound literal, and not followed by a cast-expression. 1596f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis assert(ParseAs == SimpleExpr); 1597f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1598f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis ExprType = SimpleExpr; 1599f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis Result = ParseExpression(); 1600f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis if (!Result.isInvalid() && Tok.is(tok::r_paren)) 1601f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result)); 1602f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1603f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis // Match the ')'. 1604f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis if (Result.isInvalid()) { 1605f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis SkipUntil(tok::r_paren); 1606f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis return ExprError(); 1607f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis } 16081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1609f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis if (Tok.is(tok::r_paren)) 1610f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis RParenLoc = ConsumeParen(); 1611f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis else 1612f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis MatchRHSPunctuation(tok::r_paren, LParenLoc); 1613f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis 1614f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis return move(Result); 1615f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis} 1616