ParseExprCXX.cpp revision bbd37c62e34db3f5a95c899723484a76c71d7757
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();
32902a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
33002a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
33102a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                                   isAddressOfOperand);
33202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
333eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
334eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type.
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       postfix-expression: [C++ 5.2p1]
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'dynamic_cast' '<' type-name '>' '(' expression ')'
3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'static_cast' '<' type-name '>' '(' expression ')'
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'const_cast' '<' type-name '>' '(' expression ')'
3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
34420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXCasts() {
3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CastName = 0;     // For error messages
3475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Kind) {
3495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown C++ cast!"); abort();
3505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_const_cast:       CastName = "const_cast";       break;
3515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_static_cast:      CastName = "static_cast";      break;
3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation OpLoc = ConsumeToken();
3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LAngleBracketLoc = Tok.getLocation();
3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
36020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
362809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult CastTy = ParseTypeName();
3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation RAngleBracketLoc = Tok.getLocation();
3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3651ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
36620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
37021e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
37121e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    return ExprError();
3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
37321e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  OwningExprResult Result = ParseExpression();
3741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37521e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  // Match the ')'.
37627591ff4fc64600fd67c5d81899e3efe5ef41a80Douglas Gregor  RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
378809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (!Result.isInvalid() && !CastTy.isInvalid())
37949badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
380f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                       LAngleBracketLoc, CastTy.get(),
381809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                       RAngleBracketLoc,
382f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                       LParenLoc, move(Result), RParenLoc);
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
38420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
387c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression.
388c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
389c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///       postfix-expression: [C++ 5.2p1]
390c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' expression ')'
391c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' type-id ')'
392c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
39320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXTypeid() {
394c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
395c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
396c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation OpLoc = ConsumeToken();
397c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation LParenLoc = Tok.getLocation();
398c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation RParenLoc;
399c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
400c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  // typeid expressions are always parenthesized.
401c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
402c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      "typeid"))
40320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
404c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
40515faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningExprResult Result(Actions);
406c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
407c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (isTypeIdInParens()) {
408809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult Ty = ParseTypeName();
409c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
410c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
411c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    MatchRHSPunctuation(tok::r_paren, LParenLoc);
412c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
413809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (Ty.isInvalid())
41420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
415c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
416c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
417809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                    Ty.get(), RParenLoc);
418c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  } else {
419e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // C++0x [expr.typeid]p3:
4201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   When typeid is applied to an expression other than an lvalue of a
4211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   polymorphic class type [...] The expression is an unevaluated
422e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //   operand (Clause 5).
423e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //
4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Note that we can't tell whether the expression is an lvalue of a
425e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // polymorphic class type until after we've parsed the expression, so
426ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // we the expression is potentially potentially evaluated.
427ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(Actions,
428ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                       Action::PotentiallyPotentiallyEvaluated);
429c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = ParseExpression();
430c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
431c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
4320e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Result.isInvalid())
433c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      SkipUntil(tok::r_paren);
434c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    else {
435c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      MatchRHSPunctuation(tok::r_paren, LParenLoc);
436c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
437c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
438effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl                                      Result.release(), RParenLoc);
439c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    }
440c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  }
441c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
44220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
443c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
444c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
4455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       boolean-literal: [C++ 2.13.5]
4485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'true'
4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'false'
45020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXBoolLiteral() {
4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
452f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
4535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
45450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
45550dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression.
45650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///
45750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///       throw-expression: [C++ 15]
45850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///         'throw' assignment-expression[opt]
45920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseThrowExpression() {
46050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  assert(Tok.is(tok::kw_throw) && "Not throw!");
46150dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
46220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl
4632a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // If the current token isn't the start of an assignment-expression,
4642a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // then the expression is not present.  This handles things like:
4652a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  //   "C ? throw : (void)42", which is crazy but legal.
4662a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
4672a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::semi:
4682a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_paren:
4692a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_square:
4702a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_brace:
4712a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::colon:
4722a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::comma:
473f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl    return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
47450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
4752a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  default:
4762f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    OwningExprResult Expr(ParseAssignmentExpression());
47720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    if (Expr.isInvalid()) return move(Expr);
478f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl    return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
4792a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  }
48050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
4814cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
4824cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer.
4834cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
4844cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
4854cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which
4864cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called.
48720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXThis() {
4884cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  assert(Tok.is(tok::kw_this) && "Not 'this'!");
4894cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  SourceLocation ThisLoc = ConsumeToken();
490f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXThis(ThisLoc);
4914cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
492987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
493987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
494987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)")
495987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)")
496987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()").
497987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
498987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       postfix-expression: [C++ 5.2p1]
499987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         simple-type-specifier '(' expression-list[opt] ')'      [C++ 5.2.3]
500987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typename-specifier '(' expression-list[opt] ')'         [TODO]
501987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
50220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult
50320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
504987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
5055ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor  TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
506987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
507987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  assert(Tok.is(tok::l_paren) && "Expected '('!");
508987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation LParenLoc = ConsumeParen();
509987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
510a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector Exprs(Actions);
511987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  CommaLocsTy CommaLocs;
512987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
513987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  if (Tok.isNot(tok::r_paren)) {
514987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    if (ParseExpressionList(Exprs, CommaLocs)) {
515987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis      SkipUntil(tok::r_paren);
51620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
517987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    }
518987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
519987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
520987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // Match the ')'.
521987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
522987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
523ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl  // TypeRep could be null, if it references an invalid typedef.
524ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl  if (!TypeRep)
525ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl    return ExprError();
526ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl
527987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
528987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis         "Unexpected number of commas!");
529f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
530f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                           LParenLoc, move_arg(Exprs),
531beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                           CommaLocs.data(), RParenLoc);
532987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
533987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
53471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// ParseCXXCondition - if/switch/while/for condition expression.
53571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
53671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///       condition:
53771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         expression
53871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         type-specifier-seq declarator '=' assignment-expression
53971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
54071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///             '=' assignment-expression
54171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
5422f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian RedlParser::OwningExprResult Parser::ParseCXXCondition() {
543a8a4598b6f2a07339ab8a1248295a07d771a2b2aArgyrios Kyrtzidis  if (!isCXXConditionDeclaration())
54471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    return ParseExpression(); // expression
54571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
54671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  SourceLocation StartLoc = Tok.getLocation();
54771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
54871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // type-specifier-seq
54971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  DeclSpec DS;
55071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
55171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
55271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // declarator
55371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
55471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
55571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
55671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // simple-asm-expr[opt]
55771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.is(tok::kw_asm)) {
558ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
559ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
5600e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (AsmLabel.isInvalid()) {
56171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis      SkipUntil(tok::semi);
5622f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      return ExprError();
56371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    }
564effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    DeclaratorInfo.setAsmLabel(AsmLabel.release());
565ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.SetRangeEnd(Loc);
56671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  }
56771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
56871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // If attributes are present, parse them.
569ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Tok.is(tok::kw___attribute)) {
570ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
571bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    AttributeList *AttrList = ParseGNUAttributes(&Loc);
572ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.AddAttributes(AttrList, Loc);
573ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
57471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
57571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // '=' assignment-expression
57671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.isNot(tok::equal))
5772f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
57871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  SourceLocation EqualLoc = ConsumeToken();
5792f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult AssignExpr(ParseAssignmentExpression());
5800e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (AssignExpr.isInvalid())
5812f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    return ExprError();
5822f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
583f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
584f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                                  DeclaratorInfo,EqualLoc,
585f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                                  move(AssignExpr));
58671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis}
58771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
588987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
589987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of
590987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier.
591987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
592987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       simple-type-specifier:
593eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier[opt] type-name
594987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
595987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         char
596987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         wchar_t
597987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         bool
598987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         short
599987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         int
600987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         long
601987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         signed
602987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         unsigned
603987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         float
604987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         double
605987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         void
606987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU]   typeof-specifier
607987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto               [TODO]
608987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
609987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       type-name:
610987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         class-name
611987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         enum-name
612987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typedef-name
613987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
614987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
615987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  DS.SetRangeStart(Tok.getLocation());
616987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  const char *PrevSpec;
617fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
618987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation Loc = Tok.getLocation();
6191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
620987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  switch (Tok.getKind()) {
62155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::identifier:   // foo::bar
62255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::coloncolon:   // ::foo::bar
62355a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    assert(0 && "Annotation token should already be formed!");
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
625987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    assert(0 && "Not a simple-type-specifier token!");
626987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    abort();
62755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner
628987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // type-name
629b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  case tok::annot_typename: {
630fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
631eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis                       Tok.getAnnotationValue());
632987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
633987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
635987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // builtin types
636987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_short:
637fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
638987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
639987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_long:
640fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
641987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
642987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_signed:
643fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
644987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
645987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_unsigned:
646fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
647987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
648987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_void:
649fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
650987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
651987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_char:
652fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
653987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
654987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_int:
655fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
656987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
657987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_float:
658fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
659987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
660987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_double:
661fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
662987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
663987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_wchar_t:
664fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
665987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
666f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char16_t:
667fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
668f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
669f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char32_t:
670fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
671f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
672987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_bool:
673fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
674987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
6751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
676987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // GNU typeof support.
677987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_typeof:
678987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    ParseTypeofSpecifier(DS);
6799b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor    DS.Finish(Diags, PP);
680987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    return;
681987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
682b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  if (Tok.is(tok::annot_typename))
683eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
684eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  else
685eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getLocation());
686987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  ConsumeToken();
6879b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor  DS.Finish(Diags, PP);
688987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
6891cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
6902f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
6912f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers,
6922f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished
6932f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are
6942f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and
6952f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false
6962f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise.
6972f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
6982f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///   type-specifier-seq: [C++ 8.1]
6992f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///     type-specifier type-specifier-seq[opt]
7002f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
7012f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
7022f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  DS.SetRangeStart(Tok.getLocation());
7032f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  const char *PrevSpec = 0;
704fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
705fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  bool isInvalid = 0;
7062f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
7072f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // Parse one or more of the type specifiers.
708fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
7091ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_operator_missing_type_specifier);
7102f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor    return true;
7112f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  }
7121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
713fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
7142f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
7152f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  return false;
7162f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
7172f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
7183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of
7193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form.
7203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or
7223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
7233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will
7243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to
7253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller.
7263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if
7283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id.
7293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual
7313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name.
7323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or
7343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor.
7353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the
7373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
7383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
73946df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
74046df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
74146df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
7423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id
7433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully,
7443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id.
7453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise.
7473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
7483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          IdentifierInfo *Name,
7493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          SourceLocation NameLoc,
7503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          bool EnteringContext,
7512d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                          TypeTy *ObjectType,
7523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          UnqualifiedId &Id) {
7533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
7543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
7553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateTy Template;
7563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateNameKind TNK = TNK_Non_template;
7573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (Id.getKind()) {
7583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
759014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
760014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
761014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                 Template);
7623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
7633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
764014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_ConstructorName: {
765014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
766014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
767014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
7682d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                 EnteringContext, Template);
7693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
770014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
7713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
772014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_DestructorName: {
773014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
774014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
7752d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (ObjectType) {
776014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
777a481edb1b11c956a46cb42cd0dc4dd9851c10801Douglas Gregor                                                    TemplateName, ObjectType,
778a481edb1b11c956a46cb42cd0dc4dd9851c10801Douglas Gregor                                                    EnteringContext);
7792d1c21414199a7452f122598189363a3922605b1Douglas Gregor      TNK = TNK_Dependent_template_name;
7802d1c21414199a7452f122598189363a3922605b1Douglas Gregor      if (!Template.get())
7812d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
7822d1c21414199a7452f122598189363a3922605b1Douglas Gregor    } else {
783014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
7842d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                   EnteringContext, Template);
7852d1c21414199a7452f122598189363a3922605b1Douglas Gregor
7862d1c21414199a7452f122598189363a3922605b1Douglas Gregor      if (TNK == TNK_Non_template && Id.DestructorName == 0) {
7872d1c21414199a7452f122598189363a3922605b1Douglas Gregor        // The identifier following the destructor did not refer to a template
7882d1c21414199a7452f122598189363a3922605b1Douglas Gregor        // or to a type. Complain.
7892d1c21414199a7452f122598189363a3922605b1Douglas Gregor        if (ObjectType)
7902d1c21414199a7452f122598189363a3922605b1Douglas Gregor          Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
7912d1c21414199a7452f122598189363a3922605b1Douglas Gregor            << Name;
7922d1c21414199a7452f122598189363a3922605b1Douglas Gregor        else
7932d1c21414199a7452f122598189363a3922605b1Douglas Gregor          Diag(NameLoc, diag::err_destructor_class_name);
7942d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
7952d1c21414199a7452f122598189363a3922605b1Douglas Gregor      }
7962d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
7973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
798014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
7993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  default:
8013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
8023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
8033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (TNK == TNK_Non_template)
8053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
8063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Parse the enclosed template argument list.
8083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
8093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgList TemplateArgs;
8103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
8113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       &SS, true, LAngleLoc,
8123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgs,
8133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       RAngleLoc))
8143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
8153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
8173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) {
8183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Form a parsed representation of the template-id to be stored in the
8193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // UnqualifiedId.
8203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateIdAnnotation *TemplateId
8213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      = TemplateIdAnnotation::Allocate(TemplateArgs.size());
8223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
8243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->Name = Id.Identifier;
825014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = OO_None;
8263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
8273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
828014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Name = 0;
829014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = Id.OperatorFunctionId.Operator;
830014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
8313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
8323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Template = Template.getAs<void*>();
8343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Kind = TNK;
8353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->LAngleLoc = LAngleLoc;
8363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->RAngleLoc = RAngleLoc;
837314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
8383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
839314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor         Arg != ArgEnd; ++Arg)
8403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Args[Arg] = TemplateArgs[Arg];
8413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setTemplateId(TemplateId);
8433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
8443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
8453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Bundle the template arguments together.
8473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
8483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                     TemplateArgs.size());
8493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Constructor and destructor names.
8513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  Action::TypeResult Type
8523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    = Actions.ActOnTemplateIdType(Template, NameLoc,
8533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  LAngleLoc, TemplateArgsPtr,
8543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  RAngleLoc);
8553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Type.isInvalid())
8563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
8573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
8593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
8603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  else
8613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
8623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return false;
8643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
8653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
866ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part
867ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id.
8683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
869ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or
870ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way.
8713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
872ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
8733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       operator-function-id: [C++ 13.5]
8743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         'operator' operator
8753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
876ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       operator: one of
8773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            new   delete  new[]   delete[]
8783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            +     -    *  /    %  ^    &   |   ~
8793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            !     =    <  >    += -=   *=  /=  %=
8803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
8813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
8823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ()    []
8833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-function-id: [C++ 12.3.2]
8853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator conversion-type-id
8863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-type-id:
8883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         type-specifier-seq conversion-declarator[opt]
8893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-declarator:
8913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ptr-operator conversion-declarator[opt]
8923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode
8933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
8953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
8963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the
8983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
8993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
900ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
901ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed.
902ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
903ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
904ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
905ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise.
906ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
907ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        TypeTy *ObjectType,
908ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        UnqualifiedId &Result) {
909ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
910ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
911ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Consume the 'operator' keyword.
912ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation KeywordLoc = ConsumeToken();
913ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
914ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Determine what kind of operator name we have.
915ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  unsigned SymbolIdx = 0;
916ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation SymbolLocations[3];
917ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  OverloadedOperatorKind Op = OO_None;
918ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  switch (Tok.getKind()) {
919ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_new:
920ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_delete: {
921ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      bool isNew = Tok.getKind() == tok::kw_new;
922ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the 'new' or 'delete'.
923ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();
924ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::l_square)) {
925ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the '['.
926ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SourceLocation LBracketLoc = ConsumeBracket();
927ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the ']'.
928ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
929ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                         LBracketLoc);
930ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (RBracketLoc.isInvalid())
931ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          return true;
932ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
933ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SymbolLocations[SymbolIdx++] = LBracketLoc;
934ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SymbolLocations[SymbolIdx++] = RBracketLoc;
935ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_Array_New : OO_Array_Delete;
936ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
937ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_New : OO_Delete;
938ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
939ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
940ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
941ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
942ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
943ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::Token:                                                     \
944ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
945ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_##Name;                                                    \
946ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
947ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
948ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def"
949ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
950ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_paren: {
951ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the '('.
952ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation LParenLoc = ConsumeParen();
953ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the ')'.
954ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
955ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                     LParenLoc);
956ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (RParenLoc.isInvalid())
957ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
958ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
959ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = LParenLoc;
960ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = RParenLoc;
961ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Call;
962ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
963ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
964ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
965ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_square: {
966ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the '['.
967ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation LBracketLoc = ConsumeBracket();
968ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the ']'.
969ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
970ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                       LBracketLoc);
971ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (RBracketLoc.isInvalid())
972ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
973ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
974ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = LBracketLoc;
975ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = RBracketLoc;
976ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Subscript;
977ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
978ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
979ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
980ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::code_completion: {
981ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Code completion for the operator name.
982ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Actions.CodeCompleteOperatorName(CurScope);
983ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
984ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the operator token.
985ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      ConsumeToken();
986ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
987ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Don't try to parse any further.
988ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return true;
989ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
990ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
991ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    default:
992ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
993ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
994ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
995ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Op != OO_None) {
996ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // We have parsed an operator-function-id.
997ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
998ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return false;
999ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
1000ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1001ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse a conversion-function-id.
1002ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1003ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-function-id: [C++ 12.3.2]
1004ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     operator conversion-type-id
1005ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1006ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-type-id:
1007ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     type-specifier-seq conversion-declarator[opt]
1008ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1009ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-declarator:
1010ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     ptr-operator conversion-declarator[opt]
1011ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1012ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the type-specifier-seq.
1013ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DeclSpec DS;
1014f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor  if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
1015ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1016ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1017ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the conversion-declarator, which is merely a sequence of
1018ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // ptr-operators.
1019ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Declarator D(DS, Declarator::TypeNameContext);
1020ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1021ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1022ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Finish up the type.
1023ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1024ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Ty.isInvalid())
1025ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1026ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1027ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Note that this is a conversion-function-id.
1028ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1029ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                 D.getSourceRange().getEnd());
1030ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return false;
1031ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
1032ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1033ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1034ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity.
1035ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1036ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
1037ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       unqualified-id: [C++ expr.prim.general]
1038ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         identifier
1039ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         operator-function-id
1040ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         conversion-function-id
1041ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO]
1042ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         ~ class-name
1043ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         template-id
1044ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1045ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode
1046ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1047ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
1048ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1049ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1050ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the
1051ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier.
1052ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
10533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name.
10543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name.
10563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
105746df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
105846df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
105946df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
10603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
10613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise.
10633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
10643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowDestructorName,
10653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowConstructorName,
10662d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                TypeTy *ObjectType,
10673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                UnqualifiedId &Result) {
10683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
10693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   identifier
10703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (when it hasn't already been annotated)
10713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::identifier)) {
10723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the identifier.
10733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *Id = Tok.getIdentifierInfo();
10743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation IdLoc = ConsumeToken();
10753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (AllowConstructorName &&
10773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        Actions.isCurrentClassName(*Id, CurScope, &SS)) {
10783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed a constructor name.
10793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
10803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                                    &SS, false),
10813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                IdLoc, IdLoc);
10823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
10833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an identifier.
10843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setIdentifier(Id, IdLoc);
10853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
10863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // If the next token is a '<', we may have a template.
10883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.is(tok::less))
10893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
10902d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                          ObjectType, Result);
10913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
10933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
10943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
10963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (already parsed and annotated)
10973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::annot_template_id)) {
10983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // FIXME: Could this be a constructor name???
10993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // We have already parsed a template-id; consume the annotation token as
11013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // our unqualified-id.
11023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setTemplateId(
11033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                  static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
11043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ConsumeToken();
11053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
11063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
11073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
11093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   operator-function-id
11103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   conversion-function-id
11113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::kw_operator)) {
1112ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
11133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
11143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1115ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // If we have an operator-function-id and the next token is a '<', we may
1116ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // have a
1117ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //
1118ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //   template-id:
1119ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //     operator-function-id < template-argument-list[opt] >
1120ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    if (Result.getKind() == UnqualifiedId::IK_OperatorFunctionId &&
1121ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Tok.is(tok::less))
1122ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1123ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                          EnteringContext, ObjectType,
1124ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                          Result);
11253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
11273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
11283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
11303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // C++ [expr.unary.op]p10:
11313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   There is an ambiguity in the unary-expression ~X(), where X is a
11323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   class-name. The ambiguity is resolved in favor of treating ~ as a
11333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //    unary complement rather than treating ~X as referring to a destructor.
11343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the '~'.
11363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation TildeLoc = ConsumeToken();
11373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name.
11393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.isNot(tok::identifier)) {
11403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Diag(Tok, diag::err_destructor_class_name);
11413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
11423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
11433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name (or template-name in a simple-template-id).
11453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
11463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation ClassNameLoc = ConsumeToken();
11473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11482d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (Tok.is(tok::less)) {
11492d1c21414199a7452f122598189363a3922605b1Douglas Gregor      Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
11502d1c21414199a7452f122598189363a3922605b1Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
11512d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                          EnteringContext, ObjectType, Result);
11522d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
11532d1c21414199a7452f122598189363a3922605b1Douglas Gregor
11543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a destructor name.
11553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
1156f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor                                             CurScope, &SS, false, ObjectType);
11573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (!Ty) {
11582d1c21414199a7452f122598189363a3922605b1Douglas Gregor      if (ObjectType)
11592d1c21414199a7452f122598189363a3922605b1Douglas Gregor        Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
11602d1c21414199a7452f122598189363a3922605b1Douglas Gregor          << ClassName;
11612d1c21414199a7452f122598189363a3922605b1Douglas Gregor      else
11622d1c21414199a7452f122598189363a3922605b1Douglas Gregor        Diag(ClassNameLoc, diag::err_destructor_class_name);
11633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
11643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
11653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
11673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
11683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
11693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11702d1c21414199a7452f122598189363a3922605b1Douglas Gregor  Diag(Tok, diag::err_expected_unqualified_id)
11712d1c21414199a7452f122598189363a3922605b1Douglas Gregor    << getLang().CPlusPlus;
11723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return true;
11733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
11743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
11764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors.
11771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
117859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has
117959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
118059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location.  Otherwise, "Start" is the location of the 'new' token.
11814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
11824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
11834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] new-type-id
11844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
11854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
11864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
11874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
11884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
11894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
11904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1191cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-type-id:
1192cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   type-specifier-seq new-declarator[opt]
1193cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
1194cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-declarator:
1195cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   ptr-operator new-declarator[opt]
1196cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   direct-new-declarator
1197cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
11984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-initializer:
11994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list[opt] ')'
12004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// [C++0x]           braced-init-list                                   [TODO]
12014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
120259232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::OwningExprResult
120359232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
120459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_new) && "expected 'new' token");
120559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken();   // Consume 'new'
12064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
12074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
12084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // second form of new-expression. It can't be a new-type-id.
12094c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1210a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector PlacementArgs(Actions);
12114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation PlacementLParen, PlacementRParen;
12124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
12134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ParenTypeId;
1214cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  DeclSpec DS;
1215cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
12164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
12174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // If it turns out to be a placement, we change the type location.
12184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementLParen = ConsumeParen();
1219cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1220cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
122120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1222cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
12234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
12244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
1225cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementRParen.isInvalid()) {
1226cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
122720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1228cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
12294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1230cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementArgs.empty()) {
12314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Reset the placement locations. There was no placement.
12324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      PlacementLParen = PlacementRParen = SourceLocation();
12334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      ParenTypeId = true;
12344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    } else {
12354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // We still need the type.
12364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      if (Tok.is(tok::l_paren)) {
1237cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SourceLocation LParen = ConsumeParen();
1238cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseSpecifierQualifierList(DS);
1239ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1240cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseDeclarator(DeclaratorInfo);
1241cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        MatchRHSPunctuation(tok::r_paren, LParen);
12424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl        ParenTypeId = true;
12434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      } else {
1244cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        if (ParseCXXTypeSpecifierSeq(DS))
1245cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          DeclaratorInfo.setInvalidType(true);
1246ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        else {
1247ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1248cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          ParseDeclaratorInternal(DeclaratorInfo,
1249cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                                  &Parser::ParseDirectNewDeclarator);
1250ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        }
12514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl        ParenTypeId = false;
12524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      }
12534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
12544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  } else {
1255cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // A new-type-id is a simplified type-id, where essentially the
1256cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // direct-declarator is replaced by a direct-new-declarator.
1257cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseCXXTypeSpecifierSeq(DS))
1258cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      DeclaratorInfo.setInvalidType(true);
1259ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    else {
1260ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1261cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      ParseDeclaratorInternal(DeclaratorInfo,
1262cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                              &Parser::ParseDirectNewDeclarator);
1263ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    }
12644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ParenTypeId = false;
12654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
1266eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (DeclaratorInfo.isInvalidType()) {
1267cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
126820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
1269cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
12704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1271a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector ConstructorArgs(Actions);
12724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation ConstructorLParen, ConstructorRParen;
12734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
12744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
12754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorLParen = ConsumeParen();
12764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (Tok.isNot(tok::r_paren)) {
12774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      CommaLocsTy CommaLocs;
1278cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1279cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
128020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl        return ExprError();
1281cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      }
12824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
12834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
1284cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ConstructorRParen.isInvalid()) {
1285cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
128620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1287cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
12884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
12894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1290f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1291f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(PlacementArgs), PlacementRParen,
1292f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             ParenTypeId, DeclaratorInfo, ConstructorLParen,
1293f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(ConstructorArgs), ConstructorRParen);
12944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
12954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
12964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
12974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal.
12984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
12994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        direct-new-declarator:
13004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '[' expression ']'
13014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   direct-new-declarator '[' constant-expression ']'
13024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
130359232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) {
13044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Parse the array dimensions.
13054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool first = true;
13064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  while (Tok.is(tok::l_square)) {
13074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LLoc = ConsumeBracket();
13082f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    OwningExprResult Size(first ? ParseExpression()
13092f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl                                : ParseConstantExpression());
13100e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Size.isInvalid()) {
13114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Recover
13124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      SkipUntil(tok::r_square);
13134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
13144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
13154c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    first = false;
13164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1317ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
13184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
13197e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            Size.release(), LLoc, RLoc),
1320ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl                  RLoc);
13214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1322ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (RLoc.isInvalid())
13234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
13244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
13254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
13264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
13274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
13284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator.
13294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
13304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
13314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
13324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
13334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
13344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
13354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
13364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1337cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redlbool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
133859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner                                         Declarator &D) {
13394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // The '(' was already consumed.
13404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (isTypeIdInParens()) {
1341cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseSpecifierQualifierList(D.getMutableDeclSpec());
1342ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetSourceRange(D.getDeclSpec().getSourceRange());
1343cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseDeclarator(D);
1344eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    return D.isInvalidType();
13454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
13464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
13474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // It's not a type, it has to be an expression list.
13484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Discard the comma locations - ActOnCXXNew has enough parameters.
13494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  CommaLocsTy CommaLocs;
13504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return ParseExpressionList(PlacementArgs, CommaLocs);
13514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
13524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
13534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
13544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new.
13554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
135659232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional
135759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
135859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location.  Otherwise, "Start" is the location of the
135959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token.
136059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner///
13614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        delete-expression:
13624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' cast-expression
13634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' '[' ']' cast-expression
136459232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::OwningExprResult
136559232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
136659232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
136759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken(); // Consume 'delete'
13684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
13694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Array delete?
13704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ArrayDelete = false;
13714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_square)) {
13724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ArrayDelete = true;
13734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LHS = ConsumeBracket();
13744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
13754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (RHS.isInvalid())
137620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
13774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
13784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
13792f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Operand(ParseCastExpression(false));
13800e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Operand.isInvalid())
138120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return move(Operand);
13824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1383f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
13844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
138564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
138764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch(kind) {
138864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  default: assert(false && "Not a known unary type trait.");
138964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign;
139064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_copy:        return UTT_HasNothrowCopy;
139164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
139264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign;
139364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_copy:        return UTT_HasTrivialCopy;
139464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
139564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor;
139664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor;
139764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_abstract:             return UTT_IsAbstract;
139864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_class:                return UTT_IsClass;
139964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_empty:                return UTT_IsEmpty;
140064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_enum:                 return UTT_IsEnum;
140164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_pod:                  return UTT_IsPOD;
140264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_polymorphic:          return UTT_IsPolymorphic;
140364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_union:                return UTT_IsUnion;
140464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
140564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
140664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
140764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
140864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits
140964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates.
141064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
141164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///       primary-expression:
141264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU]             unary-type-trait '(' type-id ')'
141364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
14141eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParser::OwningExprResult Parser::ParseUnaryTypeTrait() {
141564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
141664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation Loc = ConsumeToken();
141764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
141864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation LParen = Tok.getLocation();
141964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
142064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return ExprError();
142164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
142264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
142364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // there will be cryptic errors about mismatched parentheses and missing
142464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // specifiers.
1425809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
142664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
142764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
142864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
1429809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
1430809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
1431809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
1432809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
143364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
1434f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1435f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1436f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1437f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens.
1438f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::OwningExprResult
1439f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1440f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         TypeTy *&CastTy,
1441f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation LParenLoc,
1442f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation &RParenLoc) {
1443f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(getLang().CPlusPlus && "Should only be called for C++!");
1444f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1445f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(isTypeIdInParens() && "Not a type-id!");
1446f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1447f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  OwningExprResult Result(Actions, true);
1448f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  CastTy = 0;
1449f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1450f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // We need to disambiguate a very ugly part of the C++ syntax:
1451f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1452f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())x;  - type-id
1453f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())*x; - type-id
1454f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())/x; - expression
1455f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T());   - expression
1456f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1457f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The bad news is that we cannot use the specialized tentative parser, since
1458f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // it can only verify that the thing inside the parens can be parsed as
1459f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // type-id, it is not useful for determining the context past the parens.
1460f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1461f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The good news is that the parser can disambiguate this part without
1462a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis  // making any unnecessary Action calls.
1463f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  //
1464f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // It uses a scheme similar to parsing inline methods. The parenthesized
1465f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // tokens are cached, the context that follows is determined (possibly by
1466f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parsing a cast-expression), and then we re-introduce the cached tokens
1467f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // into the token stream and parse them appropriately.
1468f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
14691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParenParseOption ParseAs;
1470f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  CachedTokens Toks;
1471f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1472f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Store the tokens of the parentheses. We will parse them after we determine
1473f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // the context that follows them.
1474f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1475f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We didn't find the ')' we expected.
1476f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
1477f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
1478f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
1479f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1480f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::l_brace)) {
1481f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = CompoundLiteral;
1482f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  } else {
1483f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    bool NotCastExpr;
1484b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1485b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1486b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      NotCastExpr = true;
1487b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    } else {
1488b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // Try parsing the cast-expression that may follow.
1489b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // If it is not a cast-expression, NotCastExpr will be true and no token
1490b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // will be consumed.
1491b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      Result = ParseCastExpression(false/*isUnaryExpression*/,
1492b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman                                   false/*isAddressofOperand*/,
14932ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                                   NotCastExpr, false);
1494b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    }
1495f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1496f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // If we parsed a cast-expression, it's really a type-id, otherwise it's
1497f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // an expression.
1498f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
1499f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
1500f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
15011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The current token should go after the cached tokens.
1502f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Toks.push_back(Tok);
1503f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Re-enter the stored parenthesized tokens into the token stream, so we may
1504f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parse them now.
1505f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  PP.EnterTokenStream(Toks.data(), Toks.size(),
1506f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1507f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Drop the current token and bring the first cached one. It's the same token
1508f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // as when we entered this function.
1509f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  ConsumeAnyToken();
1510f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1511f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (ParseAs >= CompoundLiteral) {
1512f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    TypeResult Ty = ParseTypeName();
1513f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1514f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Match the ')'.
1515f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Tok.is(tok::r_paren))
1516f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      RParenLoc = ConsumeParen();
1517f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    else
1518f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      MatchRHSPunctuation(tok::r_paren, LParenLoc);
1519f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1520f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (ParseAs == CompoundLiteral) {
1521f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      ExprType = CompoundLiteral;
1522f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1523f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    }
15241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1525f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1526f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    assert(ParseAs == CastExpr);
1527f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1528f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Ty.isInvalid())
1529f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ExprError();
1530f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1531f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    CastTy = Ty.get();
1532f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1533f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Result is what ParseCastExpression returned earlier.
1534f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    if (!Result.isInvalid())
15351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
15362ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                                     move(Result));
1537f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return move(Result);
1538f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
15391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1540f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Not a compound literal, and not followed by a cast-expression.
1541f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  assert(ParseAs == SimpleExpr);
1542f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1543f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  ExprType = SimpleExpr;
1544f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Result = ParseExpression();
1545f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (!Result.isInvalid() && Tok.is(tok::r_paren))
1546f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1547f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1548f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // Match the ')'.
1549f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Result.isInvalid()) {
1550f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    SkipUntil(tok::r_paren);
1551f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
1552f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
15531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1554f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::r_paren))
1555f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    RParenLoc = ConsumeParen();
1556f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  else
1557f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
1558f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1559f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  return move(Result);
1560f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis}
1561