ParseExprCXX.cpp revision 014e88d94ff83e3aad4e33b16413a2d1817ec208
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"
173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h"
183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present.
222dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
232dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or
252dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream.
26eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
27eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'[opt] nested-name-specifier
28eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'
29eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
30eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       nested-name-specifier:
31eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         type-name '::'
32eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         namespace-name '::'
33eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         nested-name-specifier identifier '::'
342dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///         nested-name-specifier 'template'[opt] simple-template-id '::'
352dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
362dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed
382dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty)
392dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following
412dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the
422dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed.
43eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
442dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of
452dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it.
462dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \returns true if a scope specifier was parsed.
48495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
492dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                            Action::TypeTy *ObjectType,
50495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                            bool EnteringContext) {
514bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  assert(getLang().CPlusPlus &&
527452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Call sites of this function should be guarded by checking for C++");
531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::annot_cxxscope)) {
553507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    SS.setScopeRep(Tok.getAnnotationValue());
56eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    SS.setRange(Tok.getAnnotationRange());
57eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    ConsumeToken();
584bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis    return true;
59eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
60e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner
6139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  bool HasScopeSpecifier = false;
6239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
635b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner  if (Tok.is(tok::coloncolon)) {
645b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    // ::new and ::delete aren't nested-name-specifiers.
655b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    tok::TokenKind NextKind = NextToken().getKind();
665b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
675b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner      return false;
681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6955a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    // '::' - Global scope qualifier.
70357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner    SourceLocation CCLoc = ConsumeToken();
71357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner    SS.setBeginLoc(CCLoc);
723507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
73357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner    SS.setEndLoc(CCLoc);
7439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    HasScopeSpecifier = true;
75eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
76eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
7739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  while (true) {
782dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (HasScopeSpecifier) {
792dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p5:
802dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the qualified-id has the form
813b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
822dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //       ::class-name-or-namespace-name::...
833b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
842dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the class-name-or-namespace-name is looked up in global scope as a
852dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name.
862dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
872dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // To implement this, we clear out the object type as soon as we've
882dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // seen a leading '::' or part of a nested-name-specifier.
892dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      ObjectType = 0;
9081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
9181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      if (Tok.is(tok::code_completion)) {
9281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // Code completion for a nested-name-specifier, where the code
9381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // code completion token follows the '::'.
9481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
9581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        ConsumeToken();
9681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      }
972dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier:
10077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    //   nested-name-specifier 'template'[opt] simple-template-id '::'
10177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner
10277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // Parse the optional 'template' keyword, then make sure we have
10377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // 'identifier <' after it.
10477cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    if (Tok.is(tok::kw_template)) {
1052dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // If we don't have a scope specifier or an object type, this isn't a
106eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // nested-name-specifier, since they aren't allowed to start with
107eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // 'template'.
1082dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      if (!HasScopeSpecifier && !ObjectType)
109eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
110eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman
11177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      SourceLocation TemplateKWLoc = ConsumeToken();
1121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      if (Tok.isNot(tok::identifier)) {
1141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Diag(Tok.getLocation(),
11577cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner             diag::err_id_after_template_in_nested_name_spec)
11677cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner          << SourceRange(TemplateKWLoc);
11777cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        break;
11877cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      }
1191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      if (NextToken().isNot(tok::less)) {
12177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        Diag(NextToken().getLocation(),
12277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner             diag::err_less_after_template_name_in_nested_name_spec)
12377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner          << Tok.getIdentifierInfo()->getName()
12477cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner          << SourceRange(TemplateKWLoc, Tok.getLocation());
12577cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        break;
12677cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      }
1271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
128014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
129014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateTy Template
131014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor        = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
1322dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                             ObjectType);
133eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      if (!Template)
134eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
135c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner      if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
136c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner                                  &SS, TemplateKWLoc, false))
137c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner        break;
1381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      continue;
14077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    }
1411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We have
14439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
14539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //   simple-template-id '::'
14639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
14739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // So we need to check whether the simple-template-id is of the
148c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // right kind (it should name a type or be dependent), and then
149c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // convert it into a type within the nested-name-specifier.
1501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateIdAnnotation *TemplateId
15139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1524bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis
1531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateId->Kind == TNK_Type_template ||
154c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor          TemplateId->Kind == TNK_Dependent_template_name) {
15531a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor        AnnotateTemplateIdTokenAsType(&SS);
15639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        assert(Tok.is(tok::annot_typename) &&
15839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor               "AnnotateTemplateIdTokenAsType isn't working");
15939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        Token TypeToken = Tok;
16039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        ConsumeToken();
16139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
16239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        SourceLocation CCLoc = ConsumeToken();
1631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        if (!HasScopeSpecifier) {
16539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor          SS.setBeginLoc(TypeToken.getLocation());
16639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor          HasScopeSpecifier = true;
16739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        }
1681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16931a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor        if (TypeToken.getAnnotationValue())
17031a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor          SS.setScopeRep(
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
17231a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor                                                TypeToken.getAnnotationValue(),
17331a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor                                                TypeToken.getAnnotationRange(),
17431a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor                                                CCLoc));
17531a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor        else
17631a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor          SS.setScopeRep(0);
17739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        SS.setEndLoc(CCLoc);
17839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        continue;
17967b9e831943300ce54e564e601971828ce4def15Chris Lattner      }
1801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18167b9e831943300ce54e564e601971828ce4def15Chris Lattner      assert(false && "FIXME: Only type template names supported here");
18239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
18339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
1845c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
1855c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // The rest of the nested-name-specifier possibilities start with
1865c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // tok::identifier.
1875c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Tok.isNot(tok::identifier))
1885c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      break;
1895c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
1905c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    IdentifierInfo &II = *Tok.getIdentifierInfo();
1915c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
1925c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
1935c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '::'
1945c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   namespace-name '::'
1955c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   nested-name-specifier identifier '::'
1965c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    Token Next = NextToken();
1975c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::coloncolon)) {
1985c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // We have an identifier followed by a '::'. Lookup this name
1995c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // as the name in a nested-name-specifier.
2005c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation IdLoc = ConsumeToken();
2015c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
2025c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation CCLoc = ConsumeToken();
2031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2045c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      if (!HasScopeSpecifier) {
2055c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        SS.setBeginLoc(IdLoc);
2065c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        HasScopeSpecifier = true;
2075c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
2081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2095c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      if (SS.isInvalid())
2105c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
2111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2125c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SS.setScopeRep(
213495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
2142dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                            ObjectType, EnteringContext));
2155c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SS.setEndLoc(CCLoc);
2165c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      continue;
2175c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2195c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
2205c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '<'
2215c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::less)) {
2225c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      TemplateTy Template;
223014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
224014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(&II, Tok.getLocation());
225014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
226014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                                        TemplateName,
2272dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                                        ObjectType,
228495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        EnteringContext,
229495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        Template)) {
2305c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // We have found a template name, so annotate this this token
2315c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // with a template-id annotation. We do not permit the
2325c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // template-id to be translated into a type annotation,
2335c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // because some clients (e.g., the parsing of class template
2345c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // specializations) still want to see the original template-id
2355c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // token.
236c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner        if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(),
237c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner                                    false))
238c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          break;
2395c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
2405c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
2415c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
2425c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
24339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // We don't have any tokens that form the beginning of a
24439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier, so we're done.
24539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    break;
24639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
2471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  return HasScopeSpecifier;
249eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
250eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
251eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression.
252eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
253eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       id-expression:
254eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         unqualified-id
255eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         qualified-id
256eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
257eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
258eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
259eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' identifier
260eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' operator-function-id
261edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor///         '::' template-id
262eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
263eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not
264eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect:
265eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
266eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' conversion-function-id
267eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' '~' class-name
268eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
269eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics:
270eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
271eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {};
272eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {}
273eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() {
274eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: A :: ~ C(); // Some Sema error about using destructor with a
275eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///                  // namespace.
276eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: ~ C(); // Some Parser error like 'unexpected ~'.
277eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// }
278eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
279eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like:
280eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
281eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
282eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
283eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' unqualified-id
284eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
285eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the
286eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope.
287eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
288ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a
289ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts,
290ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may
291ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear.
292ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl///
293ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian RedlParser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
294eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  // qualified-id:
295eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
296eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::' unqualified-id
297eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //
298eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
2992dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
30002a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
30102a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  UnqualifiedId Name;
30202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  if (ParseUnqualifiedId(SS,
30302a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*EnteringContext=*/false,
30402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*AllowDestructorName=*/false,
30502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*AllowConstructorName=*/false,
3062d1c21414199a7452f122598189363a3922605b1Douglas Gregor                         /*ObjectType=*/0,
30702a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         Name))
30802a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor    return ExprError();
30902a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
31002a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
31102a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                                   isAddressOfOperand);
31202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
313eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
314eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another
3165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type.
3175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
3185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       postfix-expression: [C++ 5.2p1]
3195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'dynamic_cast' '<' type-name '>' '(' expression ')'
3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'static_cast' '<' type-name '>' '(' expression ')'
3215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'const_cast' '<' type-name '>' '(' expression ')'
3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
32420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXCasts() {
3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CastName = 0;     // For error messages
3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Kind) {
3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown C++ cast!"); abort();
3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_const_cast:       CastName = "const_cast";       break;
3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_static_cast:      CastName = "static_cast";      break;
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation OpLoc = ConsumeToken();
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LAngleBracketLoc = Tok.getLocation();
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
34020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
342809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult CastTy = ParseTypeName();
3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation RAngleBracketLoc = Tok.getLocation();
3445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3451ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
34620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
3475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
3495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
35021e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
35121e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    return ExprError();
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
35321e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  OwningExprResult Result = ParseExpression();
3541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35521e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  // Match the ')'.
35621e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  if (Result.isInvalid())
35721e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    SkipUntil(tok::r_paren);
3581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35921e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  if (Tok.is(tok::r_paren))
36021e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    RParenLoc = ConsumeParen();
36121e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  else
36221e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
364809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (!Result.isInvalid() && !CastTy.isInvalid())
36549badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
366f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                       LAngleBracketLoc, CastTy.get(),
367809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                       RAngleBracketLoc,
368f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                       LParenLoc, move(Result), RParenLoc);
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
37020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
3715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
373c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression.
374c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
375c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///       postfix-expression: [C++ 5.2p1]
376c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' expression ')'
377c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' type-id ')'
378c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
37920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXTypeid() {
380c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
381c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
382c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation OpLoc = ConsumeToken();
383c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation LParenLoc = Tok.getLocation();
384c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation RParenLoc;
385c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
386c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  // typeid expressions are always parenthesized.
387c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
388c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      "typeid"))
38920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
390c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
39115faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningExprResult Result(Actions);
392c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
393c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (isTypeIdInParens()) {
394809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult Ty = ParseTypeName();
395c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
396c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
397c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    MatchRHSPunctuation(tok::r_paren, LParenLoc);
398c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
399809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (Ty.isInvalid())
40020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
401c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
402c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
403809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                    Ty.get(), RParenLoc);
404c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  } else {
405e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // C++0x [expr.typeid]p3:
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   When typeid is applied to an expression other than an lvalue of a
4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   polymorphic class type [...] The expression is an unevaluated
408e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //   operand (Clause 5).
409e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Note that we can't tell whether the expression is an lvalue of a
411e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // polymorphic class type until after we've parsed the expression, so
412ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // we the expression is potentially potentially evaluated.
413ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(Actions,
414ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                       Action::PotentiallyPotentiallyEvaluated);
415c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = ParseExpression();
416c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
417c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
4180e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Result.isInvalid())
419c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      SkipUntil(tok::r_paren);
420c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    else {
421c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      MatchRHSPunctuation(tok::r_paren, LParenLoc);
422c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
423c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
424effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl                                      Result.release(), RParenLoc);
425c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    }
426c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  }
427c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
42820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
429c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
430c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
4315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
4325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       boolean-literal: [C++ 2.13.5]
4345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'true'
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'false'
43620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXBoolLiteral() {
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
438f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
44050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
44150dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression.
44250dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///
44350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///       throw-expression: [C++ 15]
44450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///         'throw' assignment-expression[opt]
44520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseThrowExpression() {
44650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  assert(Tok.is(tok::kw_throw) && "Not throw!");
44750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
44820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl
4492a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // If the current token isn't the start of an assignment-expression,
4502a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // then the expression is not present.  This handles things like:
4512a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  //   "C ? throw : (void)42", which is crazy but legal.
4522a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
4532a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::semi:
4542a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_paren:
4552a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_square:
4562a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_brace:
4572a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::colon:
4582a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::comma:
459f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl    return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
46050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
4612a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  default:
4622f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    OwningExprResult Expr(ParseAssignmentExpression());
46320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    if (Expr.isInvalid()) return move(Expr);
464f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl    return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
4652a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  }
46650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
4674cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
4684cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer.
4694cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
4704cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
4714cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which
4724cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called.
47320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXThis() {
4744cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  assert(Tok.is(tok::kw_this) && "Not 'this'!");
4754cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  SourceLocation ThisLoc = ConsumeToken();
476f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXThis(ThisLoc);
4774cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
478987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
479987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
480987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)")
481987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)")
482987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()").
483987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
484987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       postfix-expression: [C++ 5.2p1]
485987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         simple-type-specifier '(' expression-list[opt] ')'      [C++ 5.2.3]
486987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typename-specifier '(' expression-list[opt] ')'         [TODO]
487987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
48820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult
48920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
490987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
4915ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor  TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
492987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
493987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  assert(Tok.is(tok::l_paren) && "Expected '('!");
494987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation LParenLoc = ConsumeParen();
495987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
496a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector Exprs(Actions);
497987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  CommaLocsTy CommaLocs;
498987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
499987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  if (Tok.isNot(tok::r_paren)) {
500987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    if (ParseExpressionList(Exprs, CommaLocs)) {
501987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis      SkipUntil(tok::r_paren);
50220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
503987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    }
504987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
505987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
506987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // Match the ')'.
507987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
508987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
509ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl  // TypeRep could be null, if it references an invalid typedef.
510ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl  if (!TypeRep)
511ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl    return ExprError();
512ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl
513987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
514987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis         "Unexpected number of commas!");
515f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
516f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                           LParenLoc, move_arg(Exprs),
517beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                           CommaLocs.data(), RParenLoc);
518987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
519987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
52071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// ParseCXXCondition - if/switch/while/for condition expression.
52171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
52271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///       condition:
52371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         expression
52471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         type-specifier-seq declarator '=' assignment-expression
52571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
52671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///             '=' assignment-expression
52771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
5282f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian RedlParser::OwningExprResult Parser::ParseCXXCondition() {
529a8a4598b6f2a07339ab8a1248295a07d771a2b2aArgyrios Kyrtzidis  if (!isCXXConditionDeclaration())
53071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    return ParseExpression(); // expression
53171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
53271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  SourceLocation StartLoc = Tok.getLocation();
53371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
53471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // type-specifier-seq
53571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  DeclSpec DS;
53671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
53771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
53871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // declarator
53971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
54071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
54171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
54271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // simple-asm-expr[opt]
54371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.is(tok::kw_asm)) {
544ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
545ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
5460e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (AsmLabel.isInvalid()) {
54771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis      SkipUntil(tok::semi);
5482f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      return ExprError();
54971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    }
550effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    DeclaratorInfo.setAsmLabel(AsmLabel.release());
551ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.SetRangeEnd(Loc);
55271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  }
55371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
55471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // If attributes are present, parse them.
555ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Tok.is(tok::kw___attribute)) {
556ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
557ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    AttributeList *AttrList = ParseAttributes(&Loc);
558ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.AddAttributes(AttrList, Loc);
559ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
56071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
56171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // '=' assignment-expression
56271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.isNot(tok::equal))
5632f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
56471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  SourceLocation EqualLoc = ConsumeToken();
5652f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult AssignExpr(ParseAssignmentExpression());
5660e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (AssignExpr.isInvalid())
5672f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    return ExprError();
5682f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
569f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
570f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                                  DeclaratorInfo,EqualLoc,
571f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                                  move(AssignExpr));
57271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis}
57371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
574987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
575987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of
576987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier.
577987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
578987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       simple-type-specifier:
579eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier[opt] type-name
580987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
581987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         char
582987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         wchar_t
583987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         bool
584987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         short
585987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         int
586987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         long
587987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         signed
588987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         unsigned
589987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         float
590987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         double
591987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         void
592987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU]   typeof-specifier
593987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto               [TODO]
594987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
595987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       type-name:
596987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         class-name
597987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         enum-name
598987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typedef-name
599987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
600987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
601987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  DS.SetRangeStart(Tok.getLocation());
602987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  const char *PrevSpec;
603fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
604987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation Loc = Tok.getLocation();
6051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
606987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  switch (Tok.getKind()) {
60755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::identifier:   // foo::bar
60855a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::coloncolon:   // ::foo::bar
60955a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    assert(0 && "Annotation token should already be formed!");
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
611987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    assert(0 && "Not a simple-type-specifier token!");
612987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    abort();
61355a7cefc846765ac7d142a63f773747a20518d71Chris Lattner
614987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // type-name
615b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  case tok::annot_typename: {
616fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
617eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis                       Tok.getAnnotationValue());
618987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
619987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
621987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // builtin types
622987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_short:
623fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
624987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
625987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_long:
626fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
627987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
628987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_signed:
629fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
630987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
631987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_unsigned:
632fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
633987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
634987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_void:
635fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
636987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
637987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_char:
638fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
639987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
640987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_int:
641fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
642987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
643987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_float:
644fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
645987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
646987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_double:
647fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
648987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
649987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_wchar_t:
650fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
651987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
652f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char16_t:
653fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
654f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
655f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char32_t:
656fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
657f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
658987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_bool:
659fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
660987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
6611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
662987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // GNU typeof support.
663987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_typeof:
664987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    ParseTypeofSpecifier(DS);
6659b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor    DS.Finish(Diags, PP);
666987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    return;
667987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
668b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  if (Tok.is(tok::annot_typename))
669eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
670eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  else
671eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getLocation());
672987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  ConsumeToken();
6739b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor  DS.Finish(Diags, PP);
674987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
6751cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
6762f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
6772f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers,
6782f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished
6792f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are
6802f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and
6812f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false
6822f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise.
6832f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
6842f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///   type-specifier-seq: [C++ 8.1]
6852f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///     type-specifier type-specifier-seq[opt]
6862f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
6872f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
6882f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  DS.SetRangeStart(Tok.getLocation());
6892f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  const char *PrevSpec = 0;
690fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
691fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  bool isInvalid = 0;
6922f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
6932f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // Parse one or more of the type specifiers.
694fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
6951ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_operator_missing_type_specifier);
6962f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor    return true;
6972f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  }
6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
699fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
7002f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
7012f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  return false;
7022f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
7032f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
7043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of
7053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form.
7063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or
7083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
7093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will
7103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to
7113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller.
7123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if
7143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id.
7153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual
7173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name.
7183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or
7203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor.
7213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the
7233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
7243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
72546df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
72646df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
72746df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
7283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id
7293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully,
7303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id.
7313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
7323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise.
7333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
7343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          IdentifierInfo *Name,
7353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          SourceLocation NameLoc,
7363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          bool EnteringContext,
7372d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                          TypeTy *ObjectType,
7383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          UnqualifiedId &Id) {
7393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
7403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
7413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateTy Template;
7423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateNameKind TNK = TNK_Non_template;
7433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (Id.getKind()) {
7443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
745014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
746014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
747014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                 Template);
7483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
7493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
750014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_ConstructorName: {
751014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
752014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
753014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
7542d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                 EnteringContext, Template);
7553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
756014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
7573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
758014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_DestructorName: {
759014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
760014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
7612d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (ObjectType) {
762014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
763014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                                    TemplateName, ObjectType);
7642d1c21414199a7452f122598189363a3922605b1Douglas Gregor      TNK = TNK_Dependent_template_name;
7652d1c21414199a7452f122598189363a3922605b1Douglas Gregor      if (!Template.get())
7662d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
7672d1c21414199a7452f122598189363a3922605b1Douglas Gregor    } else {
768014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
7692d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                   EnteringContext, Template);
7702d1c21414199a7452f122598189363a3922605b1Douglas Gregor
7712d1c21414199a7452f122598189363a3922605b1Douglas Gregor      if (TNK == TNK_Non_template && Id.DestructorName == 0) {
7722d1c21414199a7452f122598189363a3922605b1Douglas Gregor        // The identifier following the destructor did not refer to a template
7732d1c21414199a7452f122598189363a3922605b1Douglas Gregor        // or to a type. Complain.
7742d1c21414199a7452f122598189363a3922605b1Douglas Gregor        if (ObjectType)
7752d1c21414199a7452f122598189363a3922605b1Douglas Gregor          Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
7762d1c21414199a7452f122598189363a3922605b1Douglas Gregor            << Name;
7772d1c21414199a7452f122598189363a3922605b1Douglas Gregor        else
7782d1c21414199a7452f122598189363a3922605b1Douglas Gregor          Diag(NameLoc, diag::err_destructor_class_name);
7792d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
7802d1c21414199a7452f122598189363a3922605b1Douglas Gregor      }
7812d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
7823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
783014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
7843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
7853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  default:
7863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
7873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
7883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
7893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (TNK == TNK_Non_template)
7903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
7913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
7923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Parse the enclosed template argument list.
7933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
7943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgList TemplateArgs;
7953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgIsTypeList TemplateArgIsType;
7963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgLocationList TemplateArgLocations;
7973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
7983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       &SS, true, LAngleLoc,
7993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgs,
8003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgIsType,
8013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgLocations,
8023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       RAngleLoc))
8033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
8043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
8063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) {
8073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Form a parsed representation of the template-id to be stored in the
8083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // UnqualifiedId.
8093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateIdAnnotation *TemplateId
8103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      = TemplateIdAnnotation::Allocate(TemplateArgs.size());
8113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
8133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->Name = Id.Identifier;
814014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = OO_None;
8153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
8163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
817014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Name = 0;
818014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = Id.OperatorFunctionId.Operator;
819014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
8203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
8213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Template = Template.getAs<void*>();
8233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Kind = TNK;
8243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->LAngleLoc = LAngleLoc;
8253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->RAngleLoc = RAngleLoc;
8263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    void **Args = TemplateId->getTemplateArgs();
8273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    bool *ArgIsType = TemplateId->getTemplateArgIsType();
8283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations();
8293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
8303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor         Arg != ArgEnd; ++Arg) {
8313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Args[Arg] = TemplateArgs[Arg];
8323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      ArgIsType[Arg] = TemplateArgIsType[Arg];
8333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      ArgLocs[Arg] = TemplateArgLocations[Arg];
8343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
8353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setTemplateId(TemplateId);
8373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
8383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
8393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Bundle the template arguments together.
8413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
8423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                     TemplateArgIsType.data(),
8433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                     TemplateArgs.size());
8443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Constructor and destructor names.
8463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  Action::TypeResult Type
8473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    = Actions.ActOnTemplateIdType(Template, NameLoc,
8483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  LAngleLoc, TemplateArgsPtr,
8493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  &TemplateArgLocations[0],
8503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  RAngleLoc);
8513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Type.isInvalid())
8523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
8533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
8553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
8563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  else
8573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
8583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return false;
8603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
8613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
8623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
8633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// name of an entity.
8643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \code
8663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       unqualified-id: [C++ expr.prim.general]
8673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         identifier
8683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator-function-id
8693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         conversion-function-id
8703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// [C++0x] literal-operator-id [TODO]
8713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ~ class-name
8723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         template-id
8733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       operator-function-id: [C++ 13.5]
8753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         'operator' operator
8763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator: one of
8783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            new   delete  new[]   delete[]
8793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            +     -    *  /    %  ^    &   |   ~
8803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            !     =    <  >    += -=   *=  /=  %=
8813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
8823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
8833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ()    []
8843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-function-id: [C++ 12.3.2]
8863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator conversion-type-id
8873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-type-id:
8893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         type-specifier-seq conversion-declarator[opt]
8903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-declarator:
8923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ptr-operator conversion-declarator[opt]
8933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode
8943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
8963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
8973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the
8993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
9003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
9013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name.
9023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
9033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name.
9043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
90546df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
90646df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
90746df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
9083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
9093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
9103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise.
9113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
9123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowDestructorName,
9133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowConstructorName,
9142d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                TypeTy *ObjectType,
9153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                UnqualifiedId &Result) {
9163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
9173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   identifier
9183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (when it hasn't already been annotated)
9193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::identifier)) {
9203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the identifier.
9213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *Id = Tok.getIdentifierInfo();
9223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation IdLoc = ConsumeToken();
9233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (AllowConstructorName &&
9253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        Actions.isCurrentClassName(*Id, CurScope, &SS)) {
9263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed a constructor name.
9273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
9283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                                    &SS, false),
9293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                IdLoc, IdLoc);
9303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
9313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an identifier.
9323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setIdentifier(Id, IdLoc);
9333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
9343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // If the next token is a '<', we may have a template.
9363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.is(tok::less))
9373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
9382d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                          ObjectType, Result);
9393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
9413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
9423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
9443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (already parsed and annotated)
9453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::annot_template_id)) {
9463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // FIXME: Could this be a constructor name???
9473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // We have already parsed a template-id; consume the annotation token as
9493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // our unqualified-id.
9503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setTemplateId(
9513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                  static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
9523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ConsumeToken();
9533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
9543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
9553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
9573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   operator-function-id
9583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   conversion-function-id
9593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::kw_operator)) {
9603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the 'operator' keyword.
9613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation KeywordLoc = ConsumeToken();
9623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Determine what kind of operator name we have.
9643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    unsigned SymbolIdx = 0;
9653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation SymbolLocations[3];
9663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    OverloadedOperatorKind Op = OO_None;
9673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    switch (Tok.getKind()) {
9683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      case tok::kw_new:
9693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      case tok::kw_delete: {
9703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        bool isNew = Tok.getKind() == tok::kw_new;
9713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        // Consume the 'new' or 'delete'.
9723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        SymbolLocations[SymbolIdx++] = ConsumeToken();
9733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        if (Tok.is(tok::l_square)) {
9743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          // Consume the '['.
9753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          SourceLocation LBracketLoc = ConsumeBracket();
9763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          // Consume the ']'.
9773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
9783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                                           LBracketLoc);
9793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          if (RBracketLoc.isInvalid())
9803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor            return true;
9813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          SymbolLocations[SymbolIdx++] = LBracketLoc;
9833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          SymbolLocations[SymbolIdx++] = RBracketLoc;
9843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          Op = isNew? OO_Array_New : OO_Array_Delete;
9853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        } else {
9863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          Op = isNew? OO_New : OO_Delete;
9873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        }
9883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        break;
9893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      }
9903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
9923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      case tok::Token:                                                     \
9933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
9943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        Op = OO_##Name;                                                    \
9953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        break;
9963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
9973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  #include "clang/Basic/OperatorKinds.def"
9983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      case tok::l_paren: {
10003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        // Consume the '('.
10013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        SourceLocation LParenLoc = ConsumeParen();
10023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        // Consume the ')'.
10033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
10043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                                       LParenLoc);
10053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        if (RParenLoc.isInvalid())
10063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          return true;
10073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        SymbolLocations[SymbolIdx++] = LParenLoc;
10093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        SymbolLocations[SymbolIdx++] = RParenLoc;
10103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        Op = OO_Call;
10113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        break;
10123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      }
10133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      case tok::l_square: {
10153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        // Consume the '['.
10163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        SourceLocation LBracketLoc = ConsumeBracket();
10173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        // Consume the ']'.
10183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
10193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                                         LBracketLoc);
10203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        if (RBracketLoc.isInvalid())
10213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor          return true;
10223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        SymbolLocations[SymbolIdx++] = LBracketLoc;
10243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        SymbolLocations[SymbolIdx++] = RBracketLoc;
10253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        Op = OO_Subscript;
10263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        break;
10273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      }
10283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      case tok::code_completion: {
10303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        // Code completion for the operator name.
10313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        Actions.CodeCompleteOperatorName(CurScope);
10323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        // Consume the operator token.
10343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        ConsumeToken();
10353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        // Don't try to parse any further.
10373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        return true;
10383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      }
10393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      default:
10413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        break;
10423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
10433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Op != OO_None) {
10453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an operator-function-id.
10463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
10473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // If the next token is a '<', we may have a template.
10493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      if (Tok.is(tok::less))
10503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
10512d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                            EnteringContext, ObjectType,
10522d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                            Result);
10533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return false;
10553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
10563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse a conversion-function-id.
10583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //
10593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   conversion-function-id: [C++ 12.3.2]
10603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //     operator conversion-type-id
10613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //
10623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   conversion-type-id:
10633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //     type-specifier-seq conversion-declarator[opt]
10643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //
10653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   conversion-declarator:
10663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //     ptr-operator conversion-declarator[opt]
10673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the type-specifier-seq.
10693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    DeclSpec DS;
10703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (ParseCXXTypeSpecifierSeq(DS))
10713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
10723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the conversion-declarator, which is merely a sequence of
10743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // ptr-operators.
10753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Declarator D(DS, Declarator::TypeNameContext);
10763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
10773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Finish up the type.
10793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
10803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Ty.isInvalid())
10813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
10823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a conversion-function-id.
10843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setConversionFunctionId(KeywordLoc, Ty.get(),
10853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                   D.getSourceRange().getEnd());
10863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
10873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
10883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
10903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // C++ [expr.unary.op]p10:
10913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   There is an ambiguity in the unary-expression ~X(), where X is a
10923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   class-name. The ambiguity is resolved in favor of treating ~ as a
10933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //    unary complement rather than treating ~X as referring to a destructor.
10943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the '~'.
10963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation TildeLoc = ConsumeToken();
10973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name.
10993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.isNot(tok::identifier)) {
11003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Diag(Tok, diag::err_destructor_class_name);
11013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
11023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
11033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name (or template-name in a simple-template-id).
11053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
11063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation ClassNameLoc = ConsumeToken();
11073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11082d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (Tok.is(tok::less)) {
11092d1c21414199a7452f122598189363a3922605b1Douglas Gregor      Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
11102d1c21414199a7452f122598189363a3922605b1Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
11112d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                          EnteringContext, ObjectType, Result);
11122d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
11132d1c21414199a7452f122598189363a3922605b1Douglas Gregor
11143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a destructor name.
11153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
11163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                             CurScope, &SS);
11173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (!Ty) {
11182d1c21414199a7452f122598189363a3922605b1Douglas Gregor      if (ObjectType)
11192d1c21414199a7452f122598189363a3922605b1Douglas Gregor        Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
11202d1c21414199a7452f122598189363a3922605b1Douglas Gregor          << ClassName;
11212d1c21414199a7452f122598189363a3922605b1Douglas Gregor      else
11222d1c21414199a7452f122598189363a3922605b1Douglas Gregor        Diag(ClassNameLoc, diag::err_destructor_class_name);
11233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
11243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
11253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
11273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
11283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
11293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11302d1c21414199a7452f122598189363a3922605b1Douglas Gregor  Diag(Tok, diag::err_expected_unqualified_id)
11312d1c21414199a7452f122598189363a3922605b1Douglas Gregor    << getLang().CPlusPlus;
11323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return true;
11333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
11343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
113543c7bad105f742988e7ca40564285c83bea854a5Douglas Gregor/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
11361cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// operator name (C++ [over.oper]). If successful, returns the
11371cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// predefined identifier that corresponds to that overloaded
11381cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// operator. Otherwise, returns NULL and does not consume any tokens.
11391cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor///
11401cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor///       operator-function-id: [C++ 13.5]
11411cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor///         'operator' operator
11421cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor///
11431cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// operator: one of
11441cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor///            new   delete  new[]   delete[]
11451cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor///            +     -    *  /    %  ^    &   |   ~
11461cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor///            !     =    <  >    += -=   *=  /=  %=
11471cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
11481cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
11491cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor///            ()    []
1150ab197baec16bacade82325fb274cf6b992ac5d8aSebastian RedlOverloadedOperatorKind
1151ab197baec16bacade82325fb274cf6b992ac5d8aSebastian RedlParser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
11529057a81efaf15c543aab1c5c8488e8a9ed2c0ff4Argyrios Kyrtzidis  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1153ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  SourceLocation Loc;
11541cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
11551cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  OverloadedOperatorKind Op = OO_None;
11561cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  switch (NextToken().getKind()) {
11571cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  case tok::kw_new:
11581cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    ConsumeToken(); // 'operator'
1159ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    Loc = ConsumeToken(); // 'new'
11601cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    if (Tok.is(tok::l_square)) {
11611cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor      ConsumeBracket(); // '['
1162ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      Loc = Tok.getLocation();
11631cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor      ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
11641cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor      Op = OO_Array_New;
11651cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    } else {
11661cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor      Op = OO_New;
11671cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    }
1168ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1169ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Loc;
1170e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return Op;
11711cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
11721cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  case tok::kw_delete:
11731cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    ConsumeToken(); // 'operator'
1174ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    Loc = ConsumeToken(); // 'delete'
11751cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    if (Tok.is(tok::l_square)) {
11761cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor      ConsumeBracket(); // '['
1177ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      Loc = Tok.getLocation();
11781cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor      ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
11791cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor      Op = OO_Array_Delete;
11801cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    } else {
11811cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor      Op = OO_Delete;
11821cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    }
1183ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1184ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Loc;
1185e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return Op;
11861cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
118702bcd4cd1a19121da12884aa4943226f72a81e6cDouglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly)  \
11881cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    case tok::Token:  Op = OO_##Name; break;
118902bcd4cd1a19121da12884aa4943226f72a81e6cDouglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
11901cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor#include "clang/Basic/OperatorKinds.def"
11911cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
11921cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  case tok::l_paren:
11931cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    ConsumeToken(); // 'operator'
11941cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    ConsumeParen(); // '('
1195ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    Loc = Tok.getLocation();
11961cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
1197ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1198ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Loc;
1199e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return OO_Call;
12001cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
12011cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  case tok::l_square:
12021cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    ConsumeToken(); // 'operator'
12031cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    ConsumeBracket(); // '['
1204ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    Loc = Tok.getLocation();
12051cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
1206ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1207ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Loc;
1208e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return OO_Subscript;
12091cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1210ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor  case tok::code_completion: {
1211ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor    // Code completion for the operator name.
1212ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor    Actions.CodeCompleteOperatorName(CurScope);
1213ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor
1214ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor    // Consume the 'operator' token, then replace the code-completion token
1215ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor    // with an 'operator' token and try again.
1216ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor    SourceLocation OperatorLoc = ConsumeToken();
1217ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor    Tok.setLocation(OperatorLoc);
1218ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor    Tok.setKind(tok::kw_operator);
1219ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor    return TryParseOperatorFunctionId(EndLoc);
1220ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor  }
1221ed8d322b4086b8b1e1f78b21389cabb23457dcb3Douglas Gregor
12221cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  default:
1223e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return OO_None;
12241cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  }
122543c7bad105f742988e7ca40564285c83bea854a5Douglas Gregor
122643c7bad105f742988e7ca40564285c83bea854a5Douglas Gregor  ConsumeToken(); // 'operator'
1227ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  Loc = ConsumeAnyToken(); // the operator itself
1228ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (EndLoc)
1229ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    *EndLoc = Loc;
1230e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  return Op;
12311cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
12322f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
12332f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
12342f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// which expresses the name of a user-defined conversion operator
12352f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// (C++ [class.conv.fct]p1). Returns the type that this operator is
12362f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// specifying a conversion for, or NULL if there was an error.
12372f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
12382f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///        conversion-function-id: [C++ 12.3.2]
12392f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///                   operator conversion-type-id
12402f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
12412f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///        conversion-type-id:
12422f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///                   type-specifier-seq conversion-declarator[opt]
12432f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
12442f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///        conversion-declarator:
12452f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///                   ptr-operator conversion-declarator[opt]
1246ab197baec16bacade82325fb274cf6b992ac5d8aSebastian RedlParser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
12472f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
12482f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  ConsumeToken(); // 'operator'
12492f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
12502f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // Parse the type-specifier-seq.
12512f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  DeclSpec DS;
12522f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  if (ParseCXXTypeSpecifierSeq(DS))
12532f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor    return 0;
12542f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
12552f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // Parse the conversion-declarator, which is merely a sequence of
12562f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // ptr-operators.
12572f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  Declarator D(DS, Declarator::TypeNameContext);
12584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1259ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (EndLoc)
1260ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    *EndLoc = D.getSourceRange().getEnd();
12612f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
12622f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // Finish up the type.
12632f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
12645ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor  if (Result.isInvalid())
12652f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor    return 0;
12662f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  else
12675ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor    return Result.get();
12682f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
12694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
12704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
12714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors.
12721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
127359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has
127459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
127559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location.  Otherwise, "Start" is the location of the 'new' token.
12764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
12774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
12784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] new-type-id
12794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
12804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
12814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
12824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
12834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
12844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
12854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1286cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-type-id:
1287cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   type-specifier-seq new-declarator[opt]
1288cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
1289cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-declarator:
1290cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   ptr-operator new-declarator[opt]
1291cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   direct-new-declarator
1292cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
12934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-initializer:
12944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list[opt] ')'
12954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// [C++0x]           braced-init-list                                   [TODO]
12964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
129759232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::OwningExprResult
129859232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
129959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_new) && "expected 'new' token");
130059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken();   // Consume 'new'
13014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
13024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
13034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // second form of new-expression. It can't be a new-type-id.
13044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1305a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector PlacementArgs(Actions);
13064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation PlacementLParen, PlacementRParen;
13074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
13084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ParenTypeId;
1309cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  DeclSpec DS;
1310cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
13114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
13124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // If it turns out to be a placement, we change the type location.
13134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementLParen = ConsumeParen();
1314cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1315cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
131620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1317cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
13184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
13194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
1320cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementRParen.isInvalid()) {
1321cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
132220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1323cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
13244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1325cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementArgs.empty()) {
13264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Reset the placement locations. There was no placement.
13274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      PlacementLParen = PlacementRParen = SourceLocation();
13284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      ParenTypeId = true;
13294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    } else {
13304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // We still need the type.
13314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      if (Tok.is(tok::l_paren)) {
1332cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SourceLocation LParen = ConsumeParen();
1333cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseSpecifierQualifierList(DS);
1334ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1335cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseDeclarator(DeclaratorInfo);
1336cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        MatchRHSPunctuation(tok::r_paren, LParen);
13374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl        ParenTypeId = true;
13384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      } else {
1339cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        if (ParseCXXTypeSpecifierSeq(DS))
1340cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          DeclaratorInfo.setInvalidType(true);
1341ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        else {
1342ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1343cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          ParseDeclaratorInternal(DeclaratorInfo,
1344cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                                  &Parser::ParseDirectNewDeclarator);
1345ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        }
13464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl        ParenTypeId = false;
13474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      }
13484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
13494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  } else {
1350cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // A new-type-id is a simplified type-id, where essentially the
1351cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // direct-declarator is replaced by a direct-new-declarator.
1352cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseCXXTypeSpecifierSeq(DS))
1353cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      DeclaratorInfo.setInvalidType(true);
1354ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    else {
1355ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1356cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      ParseDeclaratorInternal(DeclaratorInfo,
1357cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                              &Parser::ParseDirectNewDeclarator);
1358ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    }
13594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ParenTypeId = false;
13604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
1361eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (DeclaratorInfo.isInvalidType()) {
1362cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
136320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
1364cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
13654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1366a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector ConstructorArgs(Actions);
13674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation ConstructorLParen, ConstructorRParen;
13684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
13694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
13704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorLParen = ConsumeParen();
13714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (Tok.isNot(tok::r_paren)) {
13724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      CommaLocsTy CommaLocs;
1373cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1374cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
137520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl        return ExprError();
1376cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      }
13774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
13784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
1379cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ConstructorRParen.isInvalid()) {
1380cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
138120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1382cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
13834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
13844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1385f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1386f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(PlacementArgs), PlacementRParen,
1387f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             ParenTypeId, DeclaratorInfo, ConstructorLParen,
1388f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(ConstructorArgs), ConstructorRParen);
13894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
13904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
13914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
13924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal.
13934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
13944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        direct-new-declarator:
13954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '[' expression ']'
13964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   direct-new-declarator '[' constant-expression ']'
13974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
139859232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) {
13994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Parse the array dimensions.
14004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool first = true;
14014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  while (Tok.is(tok::l_square)) {
14024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LLoc = ConsumeBracket();
14032f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    OwningExprResult Size(first ? ParseExpression()
14042f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl                                : ParseConstantExpression());
14050e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Size.isInvalid()) {
14064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Recover
14074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      SkipUntil(tok::r_square);
14084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
14094c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
14104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    first = false;
14114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1412ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
14134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
14147e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            Size.release(), LLoc, RLoc),
1415ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl                  RLoc);
14164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1417ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (RLoc.isInvalid())
14184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
14194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
14204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
14214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
14224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
14234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator.
14244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
14254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
14264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
14274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
14284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
14294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
14304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
14314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1432cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redlbool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
143359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner                                         Declarator &D) {
14344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // The '(' was already consumed.
14354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (isTypeIdInParens()) {
1436cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseSpecifierQualifierList(D.getMutableDeclSpec());
1437ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetSourceRange(D.getDeclSpec().getSourceRange());
1438cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseDeclarator(D);
1439eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    return D.isInvalidType();
14404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
14414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
14424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // It's not a type, it has to be an expression list.
14434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Discard the comma locations - ActOnCXXNew has enough parameters.
14444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  CommaLocsTy CommaLocs;
14454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return ParseExpressionList(PlacementArgs, CommaLocs);
14464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
14474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
14484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
14494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new.
14504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
145159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional
145259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
145359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location.  Otherwise, "Start" is the location of the
145459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token.
145559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner///
14564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        delete-expression:
14574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' cast-expression
14584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' '[' ']' cast-expression
145959232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::OwningExprResult
146059232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
146159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
146259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken(); // Consume 'delete'
14634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
14644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Array delete?
14654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ArrayDelete = false;
14664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_square)) {
14674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ArrayDelete = true;
14684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LHS = ConsumeBracket();
14694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
14704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (RHS.isInvalid())
147120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
14724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
14734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
14742f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Operand(ParseCastExpression(false));
14750e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Operand.isInvalid())
147620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return move(Operand);
14774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1478f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
14794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
148064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
14811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
148264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch(kind) {
148364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  default: assert(false && "Not a known unary type trait.");
148464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign;
148564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_copy:        return UTT_HasNothrowCopy;
148664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
148764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign;
148864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_copy:        return UTT_HasTrivialCopy;
148964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
149064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor;
149164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor;
149264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_abstract:             return UTT_IsAbstract;
149364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_class:                return UTT_IsClass;
149464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_empty:                return UTT_IsEmpty;
149564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_enum:                 return UTT_IsEnum;
149664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_pod:                  return UTT_IsPOD;
149764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_polymorphic:          return UTT_IsPolymorphic;
149864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_union:                return UTT_IsUnion;
149964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
150064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
150164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
150264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
150364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits
150464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates.
150564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
150664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///       primary-expression:
150764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU]             unary-type-trait '(' type-id ')'
150864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
15091eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParser::OwningExprResult Parser::ParseUnaryTypeTrait() {
151064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
151164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation Loc = ConsumeToken();
151264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
151364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation LParen = Tok.getLocation();
151464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
151564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return ExprError();
151664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
151764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
151864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // there will be cryptic errors about mismatched parentheses and missing
151964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // specifiers.
1520809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
152164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
152264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
152364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
1524809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
1525809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
1526809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
1527809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
152864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
1529f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1530f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1531f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1532f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens.
1533f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::OwningExprResult
1534f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1535f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         TypeTy *&CastTy,
1536f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation LParenLoc,
1537f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation &RParenLoc) {
1538f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(getLang().CPlusPlus && "Should only be called for C++!");
1539f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1540f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(isTypeIdInParens() && "Not a type-id!");
1541f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1542f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  OwningExprResult Result(Actions, true);
1543f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  CastTy = 0;
1544f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1545f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // We need to disambiguate a very ugly part of the C++ syntax:
1546f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1547f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())x;  - type-id
1548f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())*x; - type-id
1549f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())/x; - expression
1550f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T());   - expression
1551f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1552f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The bad news is that we cannot use the specialized tentative parser, since
1553f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // it can only verify that the thing inside the parens can be parsed as
1554f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // type-id, it is not useful for determining the context past the parens.
1555f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1556f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The good news is that the parser can disambiguate this part without
1557a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis  // making any unnecessary Action calls.
1558f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  //
1559f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // It uses a scheme similar to parsing inline methods. The parenthesized
1560f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // tokens are cached, the context that follows is determined (possibly by
1561f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parsing a cast-expression), and then we re-introduce the cached tokens
1562f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // into the token stream and parse them appropriately.
1563f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParenParseOption ParseAs;
1565f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  CachedTokens Toks;
1566f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1567f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Store the tokens of the parentheses. We will parse them after we determine
1568f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // the context that follows them.
1569f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1570f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We didn't find the ')' we expected.
1571f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
1572f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
1573f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
1574f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1575f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::l_brace)) {
1576f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = CompoundLiteral;
1577f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  } else {
1578f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    bool NotCastExpr;
1579b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1580b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1581b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      NotCastExpr = true;
1582b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    } else {
1583b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // Try parsing the cast-expression that may follow.
1584b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // If it is not a cast-expression, NotCastExpr will be true and no token
1585b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // will be consumed.
1586b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      Result = ParseCastExpression(false/*isUnaryExpression*/,
1587b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman                                   false/*isAddressofOperand*/,
15882ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                                   NotCastExpr, false);
1589b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    }
1590f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1591f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // If we parsed a cast-expression, it's really a type-id, otherwise it's
1592f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // an expression.
1593f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
1594f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
1595f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
15961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The current token should go after the cached tokens.
1597f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Toks.push_back(Tok);
1598f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Re-enter the stored parenthesized tokens into the token stream, so we may
1599f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parse them now.
1600f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  PP.EnterTokenStream(Toks.data(), Toks.size(),
1601f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1602f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Drop the current token and bring the first cached one. It's the same token
1603f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // as when we entered this function.
1604f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  ConsumeAnyToken();
1605f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1606f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (ParseAs >= CompoundLiteral) {
1607f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    TypeResult Ty = ParseTypeName();
1608f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1609f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Match the ')'.
1610f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Tok.is(tok::r_paren))
1611f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      RParenLoc = ConsumeParen();
1612f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    else
1613f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      MatchRHSPunctuation(tok::r_paren, LParenLoc);
1614f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1615f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (ParseAs == CompoundLiteral) {
1616f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      ExprType = CompoundLiteral;
1617f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1618f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    }
16191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1620f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1621f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    assert(ParseAs == CastExpr);
1622f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1623f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Ty.isInvalid())
1624f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ExprError();
1625f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1626f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    CastTy = Ty.get();
1627f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1628f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Result is what ParseCastExpression returned earlier.
1629f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    if (!Result.isInvalid())
16301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
16312ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                                     move(Result));
1632f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return move(Result);
1633f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
16341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1635f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Not a compound literal, and not followed by a cast-expression.
1636f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  assert(ParseAs == SimpleExpr);
1637f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1638f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  ExprType = SimpleExpr;
1639f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Result = ParseExpression();
1640f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (!Result.isInvalid() && Tok.is(tok::r_paren))
1641f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1642f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1643f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // Match the ')'.
1644f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Result.isInvalid()) {
1645f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    SkipUntil(tok::r_paren);
1646f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
1647f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
16481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1649f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::r_paren))
1650f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    RParenLoc = ConsumeParen();
1651f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  else
1652f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
1653f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1654f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  return move(Result);
1655f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis}
1656