ParseExprCXX.cpp revision b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970c
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"
1619510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h"
183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h"
193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present.
232dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
242dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or
262dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream.
27eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
28eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'[opt] nested-name-specifier
29eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'
30eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
31eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       nested-name-specifier:
32eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         type-name '::'
33eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         namespace-name '::'
34eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         nested-name-specifier identifier '::'
352dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///         nested-name-specifier 'template'[opt] simple-template-id '::'
362dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
372dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed
392dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty)
402dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following
422dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the
432dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed.
44eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
452dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of
462dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it.
472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
48d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param MayBePseudoDestructor When non-NULL, points to a flag that
49d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// indicates whether this nested-name-specifier may be part of a
50d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// pseudo-destructor name. In this case, the flag will be set false
51d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we don't actually end up parsing a destructor name. Moreorover,
52d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we do end up determining that we are parsing a destructor name,
53d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// the last component of the nested-name-specifier is not parsed as
54d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// part of the scope specifier.
55d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
56b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor/// member access expression, e.g., the \p T:: in \p p->T::m.
57b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor///
589ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// \returns true if there was an error parsing a scope specifier
59495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
60b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType ObjectType,
61b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            bool EnteringContext,
62d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                            bool *MayBePseudoDestructor) {
634bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  assert(getLang().CPlusPlus &&
647452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Call sites of this function should be guarded by checking for C++");
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::annot_cxxscope)) {
67ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    SS.setScopeRep(static_cast<NestedNameSpecifier*>(Tok.getAnnotationValue()));
68eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    SS.setRange(Tok.getAnnotationRange());
69eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    ConsumeToken();
709ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
71eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
72e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner
7339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  bool HasScopeSpecifier = false;
7439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
755b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner  if (Tok.is(tok::coloncolon)) {
765b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    // ::new and ::delete aren't nested-name-specifiers.
775b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    tok::TokenKind NextKind = NextToken().getKind();
785b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
795b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner      return false;
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    // '::' - Global scope qualifier.
82357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner    SourceLocation CCLoc = ConsumeToken();
83357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner    SS.setBeginLoc(CCLoc);
8423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), CCLoc));
85357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner    SS.setEndLoc(CCLoc);
8639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    HasScopeSpecifier = true;
87eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
88eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
89d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  bool CheckForDestructor = false;
90d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (MayBePseudoDestructor && *MayBePseudoDestructor) {
91d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CheckForDestructor = true;
92d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = false;
93d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
94d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
9539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  while (true) {
962dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (HasScopeSpecifier) {
972dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p5:
982dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the qualified-id has the form
993b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
1002dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //       ::class-name-or-namespace-name::...
1013b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
1022dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the class-name-or-namespace-name is looked up in global scope as a
1032dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name.
1042dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
1052dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // To implement this, we clear out the object type as soon as we've
1062dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // seen a leading '::' or part of a nested-name-specifier.
107b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      ObjectType = ParsedType();
10881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
10981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      if (Tok.is(tok::code_completion)) {
11081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // Code completion for a nested-name-specifier, where the code
11181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // code completion token follows the '::'.
11223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
113dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor        ConsumeCodeCompletionToken();
11481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      }
1152dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier:
11877cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    //   nested-name-specifier 'template'[opt] simple-template-id '::'
11977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner
12077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // Parse the optional 'template' keyword, then make sure we have
12177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // 'identifier <' after it.
12277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    if (Tok.is(tok::kw_template)) {
1232dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // If we don't have a scope specifier or an object type, this isn't a
124eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // nested-name-specifier, since they aren't allowed to start with
125eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // 'template'.
1262dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      if (!HasScopeSpecifier && !ObjectType)
127eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
128eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman
1297bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TentativeParsingAction TPA(*this);
13077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      SourceLocation TemplateKWLoc = ConsumeToken();
131ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
132ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      UnqualifiedId TemplateName;
133ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::identifier)) {
134ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
1357bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
136ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
137ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else if (Tok.is(tok::kw_operator)) {
138ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
1397bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor                                       TemplateName)) {
1407bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
141ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
1427bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        }
143ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
144e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt        if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
145e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt            TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
146ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          Diag(TemplateName.getSourceRange().getBegin(),
147ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor               diag::err_id_after_template_in_nested_name_spec)
148ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor            << TemplateName.getSourceRange();
1497bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
150ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
151ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        }
152ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
1537bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
15477cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        break;
15577cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      }
1561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1577bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // If the next token is not '<', we have a qualified-id that refers
1587bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // to a template name, such as T::template apply, but is not a
1597bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // template-id.
1607bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      if (Tok.isNot(tok::less)) {
1617bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
1627bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        break;
1637bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      }
1647bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor
1657bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // Commit to parsing the template-id.
1667bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TPA.Commit();
167d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      TemplateTy Template;
16823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
169d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                TemplateKWLoc,
170d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                    SS,
171d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                  TemplateName,
172d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                    ObjectType,
173d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                EnteringContext,
174d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                    Template)) {
175d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
176d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                    TemplateKWLoc, false))
177d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          return true;
178d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      } else
1799ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
1801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      continue;
18277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    }
1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
1851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We have
18639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
18739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //   simple-template-id '::'
18839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
18939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // So we need to check whether the simple-template-id is of the
190c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // right kind (it should name a type or be dependent), and then
191c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // convert it into a type within the nested-name-specifier.
1921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateIdAnnotation *TemplateId
19339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
194d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
195d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
1969ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
197d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
198d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateId->Kind == TNK_Type_template ||
200c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor          TemplateId->Kind == TNK_Dependent_template_name) {
20131a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor        AnnotateTemplateIdTokenAsType(&SS);
20239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
2031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        assert(Tok.is(tok::annot_typename) &&
20439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor               "AnnotateTemplateIdTokenAsType isn't working");
20539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        Token TypeToken = Tok;
20639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        ConsumeToken();
20739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
20839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        SourceLocation CCLoc = ConsumeToken();
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        if (!HasScopeSpecifier) {
21139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor          SS.setBeginLoc(TypeToken.getLocation());
21239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor          HasScopeSpecifier = true;
21339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        }
2141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
215b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        if (ParsedType T = getTypeAnnotation(TypeToken)) {
216b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall          CXXScopeTy *Scope =
217b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall            Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, T,
21831a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor                                                TypeToken.getAnnotationRange(),
219b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                                CCLoc);
220b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall          SS.setScopeRep(Scope);
221b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        } else
22231a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor          SS.setScopeRep(0);
22339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        SS.setEndLoc(CCLoc);
22439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        continue;
22567b9e831943300ce54e564e601971828ce4def15Chris Lattner      }
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22767b9e831943300ce54e564e601971828ce4def15Chris Lattner      assert(false && "FIXME: Only type template names supported here");
22839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
22939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
2305c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2315c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // The rest of the nested-name-specifier possibilities start with
2325c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // tok::identifier.
2335c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Tok.isNot(tok::identifier))
2345c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      break;
2355c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2365c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    IdentifierInfo &II = *Tok.getIdentifierInfo();
2375c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2385c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
2395c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '::'
2405c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   namespace-name '::'
2415c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   nested-name-specifier identifier '::'
2425c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    Token Next = NextToken();
24346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
24446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // If we get foo:bar, this is almost certainly a typo for foo::bar.  Recover
24546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // and emit a fixit hint for it.
246b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor    if (Next.is(tok::colon) && !ColonIsSacred) {
24723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II, ObjectType,
248b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            EnteringContext) &&
249b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // If the token after the colon isn't an identifier, it's still an
250b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // error, but they probably meant something else strange so don't
251b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // recover like this.
252b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          PP.LookAhead(1).is(tok::identifier)) {
253b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
254849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateReplacement(Next.getLocation(), "::");
255b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor
256b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        // Recover as if the user wrote '::'.
257b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Next.setKind(tok::coloncolon);
258b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor      }
25946646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    }
26046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
2615c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::coloncolon)) {
26277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
26323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
26477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                                II, ObjectType)) {
265d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
2669ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
267d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
268d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
2695c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // We have an identifier followed by a '::'. Lookup this name
2705c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // as the name in a nested-name-specifier.
2715c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation IdLoc = ConsumeToken();
27246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner      assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
27346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner             "NextToken() not working properly!");
2745c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation CCLoc = ConsumeToken();
2751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2765c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      if (!HasScopeSpecifier) {
2775c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        SS.setBeginLoc(IdLoc);
2785c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        HasScopeSpecifier = true;
2795c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
2801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
281661c36b057d07c76c16fee95c01d96f3956265beArgyrios Kyrtzidis      if (!SS.isInvalid())
282661c36b057d07c76c16fee95c01d96f3956265beArgyrios Kyrtzidis        SS.setScopeRep(
28323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor            Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, IdLoc, CCLoc, II,
284661c36b057d07c76c16fee95c01d96f3956265beArgyrios Kyrtzidis                                                ObjectType, EnteringContext));
2855c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SS.setEndLoc(CCLoc);
2865c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      continue;
2875c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2895c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
2905c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '<'
2915c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::less)) {
2925c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      TemplateTy Template;
293014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
294014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(&II, Tok.getLocation());
2951fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
29623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
2977c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                              /*hasTemplateKeyword=*/false,
298014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                                        TemplateName,
2992dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                                        ObjectType,
300495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        EnteringContext,
3011fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                                        Template,
3021fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                              MemberOfUnknownSpecialization)) {
3035c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // We have found a template name, so annotate this this token
3045c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // with a template-id annotation. We do not permit the
3055c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // template-id to be translated into a type annotation,
3065c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // because some clients (e.g., the parsing of class template
3075c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // specializations) still want to see the original template-id
3085c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // token.
309ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
310ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
311ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                    SourceLocation(), false))
3129ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
3135c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
314d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      }
315d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
316d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
317d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          IsTemplateArgumentList(1)) {
318d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // We have something like t::getAs<T>, where getAs is a
319d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // member of an unknown specialization. However, this will only
320d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
321d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
322d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
323d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << II.getName()
324d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
325d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
326d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TemplateNameKind TNK
32723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor              = Actions.ActOnDependentTemplateName(getCurScope(),
328d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   Tok.getLocation(), SS,
329d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   TemplateName, ObjectType,
330d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   EnteringContext, Template)) {
331d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          // Consume the identifier.
332d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          ConsumeToken();
333d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
334d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                      SourceLocation(), false))
335d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor            return true;
336d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        }
337d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        else
338d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          return true;
339d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor
340d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        continue;
3415c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
3425c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
3435c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
34439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // We don't have any tokens that form the beginning of a
34539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier, so we're done.
34639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    break;
34739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
349d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Even if we didn't see any pieces of a nested-name-specifier, we
350d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // still check whether there is a tilde in this position, which
351d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // indicates a potential pseudo-destructor.
352d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (CheckForDestructor && Tok.is(tok::tilde))
353d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = true;
354d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
3559ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
356eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
357eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
358eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression.
359eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
360eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       id-expression:
361eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         unqualified-id
362eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         qualified-id
363eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
364eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
365eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
366eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' identifier
367eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' operator-function-id
368edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor///         '::' template-id
369eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
370eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not
371eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect:
372eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
373eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' conversion-function-id
374eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' '~' class-name
375eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
376eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics:
377eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
378eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {};
379eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {}
380eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() {
381eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: A :: ~ C(); // Some Sema error about using destructor with a
382eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///                  // namespace.
383eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: ~ C(); // Some Parser error like 'unexpected ~'.
384eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// }
385eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
386eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like:
387eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
388eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
389eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
390eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' unqualified-id
391eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
392eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the
393eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope.
394eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
395ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a
396ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts,
397ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may
398ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear.
399ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl///
400ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian RedlParser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
401eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  // qualified-id:
402eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
403eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::' unqualified-id
404eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //
405eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
406b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
40702a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
40802a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  UnqualifiedId Name;
40902a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  if (ParseUnqualifiedId(SS,
41002a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*EnteringContext=*/false,
41102a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*AllowDestructorName=*/false,
41202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*AllowConstructorName=*/false,
413b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                         /*ObjectType=*/ ParsedType(),
41402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         Name))
41502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor    return ExprError();
416b681b61fea36618778b8030360e90e3f4641233bJohn McCall
417b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // This is only the direct operand of an & operator if it is not
418b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // followed by a postfix-expression suffix.
419b681b61fea36618778b8030360e90e3f4641233bJohn McCall  if (isAddressOfOperand) {
420b681b61fea36618778b8030360e90e3f4641233bJohn McCall    switch (Tok.getKind()) {
421b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::l_square:
422b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::l_paren:
423b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::arrow:
424b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::period:
425b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::plusplus:
426b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::minusminus:
427b681b61fea36618778b8030360e90e3f4641233bJohn McCall      isAddressOfOperand = false;
428b681b61fea36618778b8030360e90e3f4641233bJohn McCall      break;
429b681b61fea36618778b8030360e90e3f4641233bJohn McCall
430b681b61fea36618778b8030360e90e3f4641233bJohn McCall    default:
431b681b61fea36618778b8030360e90e3f4641233bJohn McCall      break;
432b681b61fea36618778b8030360e90e3f4641233bJohn McCall    }
433b681b61fea36618778b8030360e90e3f4641233bJohn McCall  }
43402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
43523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
43602a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                                   isAddressOfOperand);
43702a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
438eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
439eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another
4415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type.
4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       postfix-expression: [C++ 5.2p1]
4445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'dynamic_cast' '<' type-name '>' '(' expression ')'
4455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'static_cast' '<' type-name '>' '(' expression ')'
4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'const_cast' '<' type-name '>' '(' expression ')'
4485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
44920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXCasts() {
4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CastName = 0;     // For error messages
4525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Kind) {
4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown C++ cast!"); abort();
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_const_cast:       CastName = "const_cast";       break;
4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_static_cast:      CastName = "static_cast";      break;
4595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation OpLoc = ConsumeToken();
4625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LAngleBracketLoc = Tok.getLocation();
4635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
46520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
4665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
467809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult CastTy = ParseTypeName();
4685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation RAngleBracketLoc = Tok.getLocation();
4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4701ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
47120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
4725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
47521e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
47621e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    return ExprError();
4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
47821e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  OwningExprResult Result = ParseExpression();
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48021e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  // Match the ')'.
48127591ff4fc64600fd67c5d81899e3efe5ef41a80Douglas Gregor  RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
483809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (!Result.isInvalid() && !CastTy.isInvalid())
48449badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
485f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                       LAngleBracketLoc, CastTy.get(),
486809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                       RAngleBracketLoc,
4879ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                       LParenLoc, Result.take(), RParenLoc);
4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
48920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
4905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
492c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression.
493c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
494c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///       postfix-expression: [C++ 5.2p1]
495c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' expression ')'
496c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' type-id ')'
497c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
49820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXTypeid() {
499c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
500c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
501c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation OpLoc = ConsumeToken();
502c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation LParenLoc = Tok.getLocation();
503c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation RParenLoc;
504c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
505c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  // typeid expressions are always parenthesized.
506c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
507c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      "typeid"))
50820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
509c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
510ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  OwningExprResult Result;
511c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
512c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (isTypeIdInParens()) {
513809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult Ty = ParseTypeName();
514c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
515c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
516c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    MatchRHSPunctuation(tok::r_paren, LParenLoc);
517c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
518809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (Ty.isInvalid())
51920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
520c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
521c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
522b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                    Ty.get().getAsOpaquePtr(), RParenLoc);
523c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  } else {
524e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // C++0x [expr.typeid]p3:
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   When typeid is applied to an expression other than an lvalue of a
5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   polymorphic class type [...] The expression is an unevaluated
527e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //   operand (Clause 5).
528e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //
5291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Note that we can't tell whether the expression is an lvalue of a
530e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // polymorphic class type until after we've parsed the expression, so
531ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // we the expression is potentially potentially evaluated.
532ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(Actions,
533ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                       Action::PotentiallyPotentiallyEvaluated);
534c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = ParseExpression();
535c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
536c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
5370e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Result.isInvalid())
538c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      SkipUntil(tok::r_paren);
539c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    else {
540c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      MatchRHSPunctuation(tok::r_paren, LParenLoc);
541c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
542c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
543effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl                                      Result.release(), RParenLoc);
544c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    }
545c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  }
546c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
54720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
548c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
549c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
550d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \brief Parse a C++ pseudo-destructor expression after the base,
551d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// . or -> operator, and nested-name-specifier have already been
552d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// parsed.
553d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
554d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       postfix-expression: [C++ 5.2]
555d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression . pseudo-destructor-name
556d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression -> pseudo-destructor-name
557d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
558d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       pseudo-destructor-name:
559d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] type-name :: ~type-name
560d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier template simple-template-id ::
561d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///                 ~type-name
562d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] ~type-name
563d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
564d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::OwningExprResult
565d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
566d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 tok::TokenKind OpKind,
567d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 CXXScopeSpec &SS,
568b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                 ParsedType ObjectType) {
569d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // We're parsing either a pseudo-destructor-name or a dependent
570d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // member access that has the same form as a
571d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // pseudo-destructor-name. We parse both in the same way and let
572d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // the action model sort them out.
573d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  //
574d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Note that the ::[opt] nested-name-specifier[opt] has already
575d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been parsed, and if there was a simple-template-id, it has
576d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been coalesced into a template-id annotation token.
577d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId FirstTypeName;
578d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation CCLoc;
579d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::identifier)) {
580d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
581d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
582d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
583d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
584d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else if (Tok.is(tok::annot_template_id)) {
585d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setTemplateId(
586d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                              (TemplateIdAnnotation *)Tok.getAnnotationValue());
587d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
588d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
589d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
590d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else {
591d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(0, SourceLocation());
592d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
593d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
594d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the tilde.
595d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
596d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation TildeLoc = ConsumeToken();
597d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (!Tok.is(tok::identifier)) {
598d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    Diag(Tok, diag::err_destructor_tilde_identifier);
599d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
600d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
601d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
602d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the second type.
603d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId SecondTypeName;
604d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  IdentifierInfo *Name = Tok.getIdentifierInfo();
605d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation NameLoc = ConsumeToken();
606d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SecondTypeName.setIdentifier(Name, NameLoc);
607d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
608d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // If there is a '<', the second type name is a template-id. Parse
609d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // it as such.
610d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::less) &&
611d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
6120278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                   SecondTypeName, /*AssumeTemplateName=*/true,
6130278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                   /*TemplateKWLoc*/SourceLocation()))
614d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
615d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
6169ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
6179ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                           OpLoc, OpKind,
618d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           SS, FirstTypeName, CCLoc,
619d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           TildeLoc, SecondTypeName,
620d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           Tok.is(tok::l_paren));
621d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor}
622d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
6235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
6245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
6255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       boolean-literal: [C++ 2.13.5]
6265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'true'
6275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'false'
62820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXBoolLiteral() {
6295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
630f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
6315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
63250dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
63350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression.
63450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///
63550dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///       throw-expression: [C++ 15]
63650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///         'throw' assignment-expression[opt]
63720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseThrowExpression() {
63850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  assert(Tok.is(tok::kw_throw) && "Not throw!");
63950dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
64020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl
6412a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // If the current token isn't the start of an assignment-expression,
6422a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // then the expression is not present.  This handles things like:
6432a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  //   "C ? throw : (void)42", which is crazy but legal.
6442a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
6452a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::semi:
6462a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_paren:
6472a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_square:
6482a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_brace:
6492a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::colon:
6502a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::comma:
6519ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return Actions.ActOnCXXThrow(ThrowLoc, 0);
65250dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
6532a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  default:
6542f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    OwningExprResult Expr(ParseAssignmentExpression());
65520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    if (Expr.isInvalid()) return move(Expr);
6569ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
6572a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  }
65850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
6594cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
6604cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer.
6614cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
6624cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
6634cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which
6644cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called.
66520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXThis() {
6664cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  assert(Tok.is(tok::kw_this) && "Not 'this'!");
6674cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  SourceLocation ThisLoc = ConsumeToken();
668f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXThis(ThisLoc);
6694cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
670987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
671987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
672987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)")
673987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)")
674987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()").
675987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
676987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       postfix-expression: [C++ 5.2p1]
677987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         simple-type-specifier '(' expression-list[opt] ')'      [C++ 5.2.3]
678987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typename-specifier '(' expression-list[opt] ')'         [TODO]
679987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
68020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult
68120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
682987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
683b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
684987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
685987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  assert(Tok.is(tok::l_paren) && "Expected '('!");
686987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation LParenLoc = ConsumeParen();
687987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
688a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector Exprs(Actions);
689987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  CommaLocsTy CommaLocs;
690987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
691987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  if (Tok.isNot(tok::r_paren)) {
692987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    if (ParseExpressionList(Exprs, CommaLocs)) {
693987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis      SkipUntil(tok::r_paren);
69420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
695987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    }
696987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
697987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
698987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // Match the ')'.
699987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
700987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
701ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl  // TypeRep could be null, if it references an invalid typedef.
702ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl  if (!TypeRep)
703ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl    return ExprError();
704ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl
705987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
706987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis         "Unexpected number of commas!");
707f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
708f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                           LParenLoc, move_arg(Exprs),
709beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                           CommaLocs.data(), RParenLoc);
710987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
711987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
71299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// ParseCXXCondition - if/switch/while condition expression.
71371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
71471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///       condition:
71571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         expression
71671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         type-specifier-seq declarator '=' assignment-expression
71771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
71871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///             '=' assignment-expression
71971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
72099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param ExprResult if the condition was parsed as an expression, the
72199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed expression.
72299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
72399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param DeclResult if the condition was parsed as a declaration, the
72499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed declaration.
72599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
726586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param Loc The location of the start of the statement that requires this
727586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// condition, e.g., the "for" in a for loop.
728586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
729586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param ConvertToBoolean Whether the condition expression should be
730586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// converted to a boolean value.
731586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
73299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \returns true if there was a parsing, false otherwise.
73399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregorbool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
734d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                               Decl *&DeclResult,
735586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               SourceLocation Loc,
736586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               bool ConvertToBoolean) {
73701dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  if (Tok.is(tok::code_completion)) {
738e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    Actions.CodeCompleteOrdinaryName(getCurScope(), Action::PCC_Condition);
739dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
74001dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  }
74101dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor
74299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!isCXXConditionDeclaration()) {
743586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // Parse the expression.
74499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    ExprResult = ParseExpression(); // expression
745d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    DeclResult = 0;
746586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    if (ExprResult.isInvalid())
747586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return true;
748586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
749586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // If required, convert to a boolean value.
750586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    if (ConvertToBoolean)
751586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      ExprResult
7529ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.take());
75399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    return ExprResult.isInvalid();
75499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
75571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
75671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // type-specifier-seq
75771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  DeclSpec DS;
75871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
75971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
76071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // declarator
76171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
76271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
76371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
76471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // simple-asm-expr[opt]
76571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.is(tok::kw_asm)) {
766ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
767ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
7680e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (AsmLabel.isInvalid()) {
76971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis      SkipUntil(tok::semi);
77099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return true;
77171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    }
772effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    DeclaratorInfo.setAsmLabel(AsmLabel.release());
773ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.SetRangeEnd(Loc);
77471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  }
77571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
77671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // If attributes are present, parse them.
777ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Tok.is(tok::kw___attribute)) {
778ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
779bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    AttributeList *AttrList = ParseGNUAttributes(&Loc);
780ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.AddAttributes(AttrList, Loc);
781ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
78271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
78399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  // Type-check the declaration itself.
78423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
78599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                                                                DeclaratorInfo);
78699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  DeclResult = Dcl.get();
78799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  ExprResult = ExprError();
78899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
78971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // '=' assignment-expression
79099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (Tok.is(tok::equal)) {
79199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    SourceLocation EqualLoc = ConsumeToken();
79299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    OwningExprResult AssignExpr(ParseAssignmentExpression());
79399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (!AssignExpr.isInvalid())
7949ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.AddInitializerToDecl(DeclResult, AssignExpr.take());
79599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  } else {
79699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // FIXME: C++0x allows a braced-init-list
79799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    Diag(Tok, diag::err_expected_equal_after_declarator);
79899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
79999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
800586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // FIXME: Build a reference to this declaration? Convert it to bool?
801586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // (This is currently handled by Sema).
802586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
80399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return false;
80471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis}
80571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
8066aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brief Determine whether the current token starts a C++
8076aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// simple-type-specifier.
8086aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::isCXXSimpleTypeSpecifier() const {
8096aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  switch (Tok.getKind()) {
8106aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::annot_typename:
8116aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_short:
8126aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_long:
8136aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_signed:
8146aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_unsigned:
8156aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_void:
8166aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char:
8176aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_int:
8186aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_float:
8196aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_double:
8206aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_wchar_t:
8216aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char16_t:
8226aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char32_t:
8236aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_bool:
8246aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: C++0x decltype support.
8256aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // GNU typeof support.
8266aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_typeof:
8276aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
8286aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
8296aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  default:
8306aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    break;
8316aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
8326aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
8336aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
8346aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
8356aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
836987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
837987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of
838987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier.
839987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
840987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       simple-type-specifier:
841eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier[opt] type-name
842987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
843987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         char
844987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         wchar_t
845987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         bool
846987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         short
847987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         int
848987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         long
849987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         signed
850987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         unsigned
851987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         float
852987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         double
853987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         void
854987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU]   typeof-specifier
855987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto               [TODO]
856987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
857987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       type-name:
858987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         class-name
859987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         enum-name
860987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typedef-name
861987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
862987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
863987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  DS.SetRangeStart(Tok.getLocation());
864987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  const char *PrevSpec;
865fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
866987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation Loc = Tok.getLocation();
8671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
868987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  switch (Tok.getKind()) {
86955a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::identifier:   // foo::bar
87055a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::coloncolon:   // ::foo::bar
87155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    assert(0 && "Annotation token should already be formed!");
8721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
873987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    assert(0 && "Not a simple-type-specifier token!");
874987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    abort();
87555a7cefc846765ac7d142a63f773747a20518d71Chris Lattner
876987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // type-name
877b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  case tok::annot_typename: {
878fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
879b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                       getTypeAnnotation(Tok));
880987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
881987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
8821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
883987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // builtin types
884987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_short:
885fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
886987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
887987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_long:
888fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
889987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
890987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_signed:
891fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
892987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
893987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_unsigned:
894fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
895987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
896987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_void:
897fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
898987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
899987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_char:
900fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
901987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
902987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_int:
903fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
904987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
905987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_float:
906fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
907987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
908987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_double:
909fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
910987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
911987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_wchar_t:
912fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
913987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
914f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char16_t:
915fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
916f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
917f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char32_t:
918fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
919f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
920987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_bool:
921fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
922987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9246aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: C++0x decltype support.
925987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // GNU typeof support.
926987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_typeof:
927987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    ParseTypeofSpecifier(DS);
9289b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor    DS.Finish(Diags, PP);
929987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    return;
930987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
931b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  if (Tok.is(tok::annot_typename))
932eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
933eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  else
934eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getLocation());
935987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  ConsumeToken();
9369b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor  DS.Finish(Diags, PP);
937987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
9381cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
9392f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
9402f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers,
9412f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished
9422f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are
9432f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and
9442f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false
9452f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise.
9462f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
9472f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///   type-specifier-seq: [C++ 8.1]
9482f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///     type-specifier type-specifier-seq[opt]
9492f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
9502f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
9512f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  DS.SetRangeStart(Tok.getLocation());
9522f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  const char *PrevSpec = 0;
953fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
954fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  bool isInvalid = 0;
9552f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
9562f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // Parse one or more of the type specifiers.
957d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
958d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl      ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
9591ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_operator_missing_type_specifier);
9602f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor    return true;
9612f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  }
9621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
963d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
964d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl         ParsedTemplateInfo(), /*SuppressDeclarations*/true))
965d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  {}
9662f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
967396a9f235e160093b5f803f7a6a18fad7b68bdbeDouglas Gregor  DS.Finish(Diags, PP);
9682f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  return false;
9692f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
9702f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
9713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of
9723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form.
9733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
9743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or
9753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
9763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will
9773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to
9783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller.
9793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
9803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if
9813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id.
9823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
9833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual
9843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name.
9853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
9863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or
9873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor.
9883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
9893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the
9903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
9913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
99246df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
99346df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
99446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
9953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id
9963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully,
9973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id.
9983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
999d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param AssumeTemplateId When true, this routine will assume that the name
1000d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// refers to a template without performing name lookup to verify.
1001d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
10023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise.
10033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
10043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          IdentifierInfo *Name,
10053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          SourceLocation NameLoc,
10063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          bool EnteringContext,
1007b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          ParsedType ObjectType,
1008d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                          UnqualifiedId &Id,
10090278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          bool AssumeTemplateId,
10100278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          SourceLocation TemplateKWLoc) {
10110278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  assert((AssumeTemplateId || Tok.is(tok::less)) &&
10120278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor         "Expected '<' to finish parsing a template-id");
10133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateTy Template;
10153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateNameKind TNK = TNK_Non_template;
10163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (Id.getKind()) {
10173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
1018014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
1019e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
1020d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    if (AssumeTemplateId) {
102123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
1022d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Id, ObjectType, EnteringContext,
1023d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Template);
1024d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
1025d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        return true;
10261fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    } else {
10271fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
10287c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS,
10297c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateKWLoc.isValid(), Id,
10307c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   ObjectType, EnteringContext, Template,
10311fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
10321fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor
10331fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
10341fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          ObjectType && IsTemplateArgumentList()) {
10351fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // We have something like t->getAs<T>(), where getAs is a
10361fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // member of an unknown specialization. However, this will only
10371fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
10381fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
10391fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        std::string Name;
10401fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        if (Id.getKind() == UnqualifiedId::IK_Identifier)
10411fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = Id.Identifier->getName();
10421fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        else {
10431fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = "operator ";
10441fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
10451fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
10461fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          else
10471fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += Id.Identifier->getName();
10481fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        }
10491fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
10501fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << Name
10511fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << FixItHint::CreateInsertion(Id.StartLocation, "template ");
105223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
1053d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                 SS, Id, ObjectType,
1054d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                 EnteringContext, Template);
1055d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TNK == TNK_Non_template)
10561fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          return true;
10571fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      }
10581fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    }
10593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
10603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1061014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_ConstructorName: {
1062014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
10631fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1064014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
10657c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara    TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
10667c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                 TemplateName, ObjectType,
10671fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 EnteringContext, Template,
10681fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 MemberOfUnknownSpecialization);
10693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1070014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
10713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1072014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_DestructorName: {
1073014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
10741fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1075014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
10762d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (ObjectType) {
107723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
1078d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               TemplateName, ObjectType,
1079d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               EnteringContext, Template);
1080d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
10812d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
10822d1c21414199a7452f122598189363a3922605b1Douglas Gregor    } else {
10837c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
10847c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateName, ObjectType,
10851fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   EnteringContext, Template,
10861fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
10872d1c21414199a7452f122598189363a3922605b1Douglas Gregor
1088b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
1089124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        Diag(NameLoc, diag::err_destructor_template_id)
1090124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          << Name << SS.getRange();
10912d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
10922d1c21414199a7452f122598189363a3922605b1Douglas Gregor      }
10932d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
10943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1095014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
10963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  default:
10983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
10993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
11003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (TNK == TNK_Non_template)
11023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
11033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Parse the enclosed template argument list.
11053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
11063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgList TemplateArgs;
11070278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (Tok.is(tok::less) &&
11080278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
11093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       &SS, true, LAngleLoc,
11103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgs,
11113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       RAngleLoc))
11123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
11133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
1115e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1116e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
11173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Form a parsed representation of the template-id to be stored in the
11183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // UnqualifiedId.
11193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateIdAnnotation *TemplateId
11203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      = TemplateIdAnnotation::Allocate(TemplateArgs.size());
11213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
11233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->Name = Id.Identifier;
1124014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = OO_None;
11253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
11263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
1127014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Name = 0;
1128014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = Id.OperatorFunctionId.Operator;
1129014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
11303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
11313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11322b5289b6fd7e3d9899868410a498c081c9595662John McCall    TemplateId->Template = Template;
11333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Kind = TNK;
11343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->LAngleLoc = LAngleLoc;
11353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->RAngleLoc = RAngleLoc;
1136314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
11373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
1138314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor         Arg != ArgEnd; ++Arg)
11393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Args[Arg] = TemplateArgs[Arg];
11403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setTemplateId(TemplateId);
11423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
11433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
11443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Bundle the template arguments together.
11463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
11473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                     TemplateArgs.size());
11483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Constructor and destructor names.
11503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  Action::TypeResult Type
11513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    = Actions.ActOnTemplateIdType(Template, NameLoc,
11523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  LAngleLoc, TemplateArgsPtr,
11533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  RAngleLoc);
11543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Type.isInvalid())
11553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
11563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
11583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
11593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  else
11603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
11613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return false;
11633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
11643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1165ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part
1166ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id.
11673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1168ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or
1169ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way.
11703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1171ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
11723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       operator-function-id: [C++ 13.5]
11733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         'operator' operator
11743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1175ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       operator: one of
11763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            new   delete  new[]   delete[]
11773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            +     -    *  /    %  ^    &   |   ~
11783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            !     =    <  >    += -=   *=  /=  %=
11793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
11803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
11813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ()    []
11823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
11833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-function-id: [C++ 12.3.2]
11843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator conversion-type-id
11853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
11863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-type-id:
11873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         type-specifier-seq conversion-declarator[opt]
11883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
11893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-declarator:
11903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ptr-operator conversion-declarator[opt]
11913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode
11923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
11933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
11943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
11953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
11963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the
11973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
11983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1199ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
1200ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed.
1201ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1202ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
1203ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1204ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise.
1205ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1206b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType ObjectType,
1207ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        UnqualifiedId &Result) {
1208ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1209ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1210ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Consume the 'operator' keyword.
1211ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation KeywordLoc = ConsumeToken();
1212ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1213ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Determine what kind of operator name we have.
1214ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  unsigned SymbolIdx = 0;
1215ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation SymbolLocations[3];
1216ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  OverloadedOperatorKind Op = OO_None;
1217ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  switch (Tok.getKind()) {
1218ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_new:
1219ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_delete: {
1220ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      bool isNew = Tok.getKind() == tok::kw_new;
1221ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the 'new' or 'delete'.
1222ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();
1223ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::l_square)) {
1224ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the '['.
1225ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SourceLocation LBracketLoc = ConsumeBracket();
1226ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the ']'.
1227ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1228ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                         LBracketLoc);
1229ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (RBracketLoc.isInvalid())
1230ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          return true;
1231ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1232ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SymbolLocations[SymbolIdx++] = LBracketLoc;
1233ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SymbolLocations[SymbolIdx++] = RBracketLoc;
1234ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_Array_New : OO_Array_Delete;
1235ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
1236ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_New : OO_Delete;
1237ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
1238ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1239ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1240ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1241ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1242ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::Token:                                                     \
1243ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
1244ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_##Name;                                                    \
1245ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1246ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1247ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def"
1248ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1249ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_paren: {
1250ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the '('.
1251ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation LParenLoc = ConsumeParen();
1252ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the ')'.
1253ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1254ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                     LParenLoc);
1255ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (RParenLoc.isInvalid())
1256ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1257ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1258ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = LParenLoc;
1259ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = RParenLoc;
1260ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Call;
1261ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1262ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1263ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1264ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_square: {
1265ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the '['.
1266ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation LBracketLoc = ConsumeBracket();
1267ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the ']'.
1268ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1269ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                       LBracketLoc);
1270ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (RBracketLoc.isInvalid())
1271ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1272ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1273ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = LBracketLoc;
1274ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = RBracketLoc;
1275ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Subscript;
1276ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1277ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1278ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1279ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::code_completion: {
1280ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Code completion for the operator name.
128123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOperatorName(getCurScope());
1282ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1283ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the operator token.
1284dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
1285ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1286ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Don't try to parse any further.
1287ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return true;
1288ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1289ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1290ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    default:
1291ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1292ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
1293ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1294ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Op != OO_None) {
1295ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // We have parsed an operator-function-id.
1296ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1297ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return false;
1298ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
12990486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
13000486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  // Parse a literal-operator-id.
13010486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //
13020486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //   literal-operator-id: [C++0x 13.5.8]
13030486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //     operator "" identifier
13040486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
13050486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
13060486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    if (Tok.getLength() != 2)
13070486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
13080486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    ConsumeStringToken();
13090486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
13100486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    if (Tok.isNot(tok::identifier)) {
13110486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_expected_ident);
13120486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      return true;
13130486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    }
13140486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
13150486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    IdentifierInfo *II = Tok.getIdentifierInfo();
13160486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
13173e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    return false;
13180486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  }
1319ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1320ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse a conversion-function-id.
1321ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1322ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-function-id: [C++ 12.3.2]
1323ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     operator conversion-type-id
1324ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1325ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-type-id:
1326ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     type-specifier-seq conversion-declarator[opt]
1327ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1328ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-declarator:
1329ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     ptr-operator conversion-declarator[opt]
1330ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1331ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the type-specifier-seq.
1332ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DeclSpec DS;
1333f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor  if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
1334ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1335ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1336ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the conversion-declarator, which is merely a sequence of
1337ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // ptr-operators.
1338ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Declarator D(DS, Declarator::TypeNameContext);
1339ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1340ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1341ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Finish up the type.
134223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Action::TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
1343ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Ty.isInvalid())
1344ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1345ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1346ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Note that this is a conversion-function-id.
1347ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1348ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                 D.getSourceRange().getEnd());
1349ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return false;
1350ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
1351ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1352ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1353ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity.
1354ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1355ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
1356ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       unqualified-id: [C++ expr.prim.general]
1357ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         identifier
1358ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         operator-function-id
1359ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         conversion-function-id
1360ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO]
1361ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         ~ class-name
1362ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         template-id
1363ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1364ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode
1365ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1366ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
1367ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1368ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1369ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the
1370ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier.
1371ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
13723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name.
13733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
13743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name.
13753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
137646df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
137746df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
137846df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
13793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
13803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
13813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise.
13823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
13833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowDestructorName,
13843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowConstructorName,
1385b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                ParsedType ObjectType,
13863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                UnqualifiedId &Result) {
13870278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
13880278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // Handle 'A::template B'. This is for template-ids which have not
13890278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // already been annotated by ParseOptionalCXXScopeSpecifier().
13900278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  bool TemplateSpecified = false;
13910278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  SourceLocation TemplateKWLoc;
13920278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
13930278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      (ObjectType || SS.isSet())) {
13940278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateSpecified = true;
13950278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateKWLoc = ConsumeToken();
13960278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  }
13970278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
13983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
13993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   identifier
14003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (when it hasn't already been annotated)
14013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::identifier)) {
14023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the identifier.
14033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *Id = Tok.getIdentifierInfo();
14043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation IdLoc = ConsumeToken();
14053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1406b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    if (!getLang().CPlusPlus) {
1407b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // If we're not in C++, only identifiers matter. Record the
1408b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // identifier and return.
1409b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      Result.setIdentifier(Id, IdLoc);
1410b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      return false;
1411b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    }
1412b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor
14133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (AllowConstructorName &&
141423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
14153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed a constructor name.
141623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
14173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                                    &SS, false),
14183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                IdLoc, IdLoc);
14193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
14203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an identifier.
14213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setIdentifier(Id, IdLoc);
14223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
14233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
14243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // If the next token is a '<', we may have a template.
14250278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less))
14263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
14270278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          ObjectType, Result,
14280278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          TemplateSpecified, TemplateKWLoc);
14293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
14303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
14313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
14323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
14333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
14343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (already parsed and annotated)
14353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::annot_template_id)) {
14360efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    TemplateIdAnnotation *TemplateId
14370efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
14380efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
14390efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    // If the template-name names the current class, then this is a constructor
14400efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    if (AllowConstructorName && TemplateId->Name &&
144123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
14420efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      if (SS.isSet()) {
14430efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // C++ [class.qual]p2 specifies that a qualified template-name
14440efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // is taken as the constructor name where a constructor can be
14450efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // declared. Thus, the template arguments are extraneous, so
14460efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // complain about them and remove them entirely.
14470efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Diag(TemplateId->TemplateNameLoc,
14480efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor             diag::err_out_of_line_constructor_template_id)
14490efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor          << TemplateId->Name
1450849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateRemoval(
14510efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                    SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
14520efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
14530efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                                  TemplateId->TemplateNameLoc,
145423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor                                                      getCurScope(),
14550efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                                      &SS, false),
14560efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->TemplateNameLoc,
14570efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->RAngleLoc);
14580efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        TemplateId->Destroy();
14590efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        ConsumeToken();
14600efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        return false;
14610efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      }
14620efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
14630efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      Result.setConstructorTemplateId(TemplateId);
14640efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      ConsumeToken();
14650efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      return false;
14660efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    }
14670efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
14683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // We have already parsed a template-id; consume the annotation token as
14693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // our unqualified-id.
14700efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    Result.setTemplateId(TemplateId);
14713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ConsumeToken();
14723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
14733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
14743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
14753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
14763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   operator-function-id
14773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   conversion-function-id
14783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::kw_operator)) {
1479ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
14803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
14813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1482e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // If we have an operator-function-id or a literal-operator-id and the next
1483e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // token is a '<', we may have a
1484ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //
1485ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //   template-id:
1486ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //     operator-function-id < template-argument-list[opt] >
1487e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1488e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt         Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
14890278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor        (TemplateSpecified || Tok.is(tok::less)))
1490ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1491ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                          EnteringContext, ObjectType,
14920278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          Result,
14930278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          TemplateSpecified, TemplateKWLoc);
14943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
14953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
14963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
14973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1498b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor  if (getLang().CPlusPlus &&
1499b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
15003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // C++ [expr.unary.op]p10:
15013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   There is an ambiguity in the unary-expression ~X(), where X is a
15023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   class-name. The ambiguity is resolved in favor of treating ~ as a
15033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //    unary complement rather than treating ~X as referring to a destructor.
15043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the '~'.
15063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation TildeLoc = ConsumeToken();
15073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name.
15093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.isNot(tok::identifier)) {
1510124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      Diag(Tok, diag::err_destructor_tilde_identifier);
15113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
15123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
15133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name (or template-name in a simple-template-id).
15153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
15163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation ClassNameLoc = ConsumeToken();
15173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15180278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less)) {
1519b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
15202d1c21414199a7452f122598189363a3922605b1Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
15210278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          EnteringContext, ObjectType, Result,
15220278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          TemplateSpecified, TemplateKWLoc);
15232d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
15242d1c21414199a7452f122598189363a3922605b1Douglas Gregor
15253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a destructor name.
1526b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1527b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              ClassNameLoc, getCurScope(),
1528b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              SS, ObjectType,
1529b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              EnteringContext);
1530124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (!Ty)
15313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
1532124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
15333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
15343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
15353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
15363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15372d1c21414199a7452f122598189363a3922605b1Douglas Gregor  Diag(Tok, diag::err_expected_unqualified_id)
15382d1c21414199a7452f122598189363a3922605b1Douglas Gregor    << getLang().CPlusPlus;
15393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return true;
15403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
15413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
15434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors.
15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
154559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has
154659232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
154759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location.  Otherwise, "Start" is the location of the 'new' token.
15484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
15494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
15504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] new-type-id
15514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
15524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
15534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
15544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
15554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
15564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
15574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1558cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-type-id:
1559cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   type-specifier-seq new-declarator[opt]
1560cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
1561cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-declarator:
1562cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   ptr-operator new-declarator[opt]
1563cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   direct-new-declarator
1564cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
15654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-initializer:
15664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list[opt] ')'
15674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// [C++0x]           braced-init-list                                   [TODO]
15684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
156959232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::OwningExprResult
157059232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
157159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_new) && "expected 'new' token");
157259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken();   // Consume 'new'
15734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
15744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
15754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // second form of new-expression. It can't be a new-type-id.
15764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1577a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector PlacementArgs(Actions);
15784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation PlacementLParen, PlacementRParen;
15794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
15804bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor  SourceRange TypeIdParens;
1581cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  DeclSpec DS;
1582cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
15834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
15844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // If it turns out to be a placement, we change the type location.
15854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementLParen = ConsumeParen();
1586cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1587cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
158820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1589cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
15904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
15914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
1592cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementRParen.isInvalid()) {
1593cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
159420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1595cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
15964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1597cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementArgs.empty()) {
15984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Reset the placement locations. There was no placement.
15994bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor      TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
16004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      PlacementLParen = PlacementRParen = SourceLocation();
16014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    } else {
16024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // We still need the type.
16034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      if (Tok.is(tok::l_paren)) {
16044bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor        TypeIdParens.setBegin(ConsumeParen());
1605cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseSpecifierQualifierList(DS);
1606ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1607cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseDeclarator(DeclaratorInfo);
16084bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor        TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
16094bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor                                                TypeIdParens.getBegin()));
16104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      } else {
1611cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        if (ParseCXXTypeSpecifierSeq(DS))
1612cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          DeclaratorInfo.setInvalidType(true);
1613ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        else {
1614ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1615cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          ParseDeclaratorInternal(DeclaratorInfo,
1616cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                                  &Parser::ParseDirectNewDeclarator);
1617ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        }
16184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      }
16194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
16204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  } else {
1621cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // A new-type-id is a simplified type-id, where essentially the
1622cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // direct-declarator is replaced by a direct-new-declarator.
1623cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseCXXTypeSpecifierSeq(DS))
1624cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      DeclaratorInfo.setInvalidType(true);
1625ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    else {
1626ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1627cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      ParseDeclaratorInternal(DeclaratorInfo,
1628cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                              &Parser::ParseDirectNewDeclarator);
1629ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    }
16304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
1631eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (DeclaratorInfo.isInvalidType()) {
1632cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
163320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
1634cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
16354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1636a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector ConstructorArgs(Actions);
16374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation ConstructorLParen, ConstructorRParen;
16384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
16394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
16404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorLParen = ConsumeParen();
16414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (Tok.isNot(tok::r_paren)) {
16424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      CommaLocsTy CommaLocs;
1643cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1644cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
164520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl        return ExprError();
1646cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      }
16474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
16484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
1649cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ConstructorRParen.isInvalid()) {
1650cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
165120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1652cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
16534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
16544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1655f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1656f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(PlacementArgs), PlacementRParen,
16574bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor                             TypeIdParens, DeclaratorInfo, ConstructorLParen,
1658f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(ConstructorArgs), ConstructorRParen);
16594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
16604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
16614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
16624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal.
16634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
16644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        direct-new-declarator:
16654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '[' expression ']'
16664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   direct-new-declarator '[' constant-expression ']'
16674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
166859232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) {
16694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Parse the array dimensions.
16704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool first = true;
16714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  while (Tok.is(tok::l_square)) {
16724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LLoc = ConsumeBracket();
16732f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    OwningExprResult Size(first ? ParseExpression()
16742f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl                                : ParseConstantExpression());
16750e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Size.isInvalid()) {
16764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Recover
16774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      SkipUntil(tok::r_square);
16784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
16794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
16804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    first = false;
16814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1682ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
16834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
16847e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            Size.release(), LLoc, RLoc),
1685ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl                  RLoc);
16864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1687ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (RLoc.isInvalid())
16884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
16894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
16904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
16914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
16924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
16934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator.
16944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
16954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
16964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
16974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
16984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
16994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
17004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
17014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1702ca0408fb49c1370430672acf2d770b7151cf71deJohn McCallbool Parser::ParseExpressionListOrTypeId(
1703ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall                                   llvm::SmallVectorImpl<Expr*> &PlacementArgs,
170459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner                                         Declarator &D) {
17054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // The '(' was already consumed.
17064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (isTypeIdInParens()) {
1707cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseSpecifierQualifierList(D.getMutableDeclSpec());
1708ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetSourceRange(D.getDeclSpec().getSourceRange());
1709cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseDeclarator(D);
1710eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    return D.isInvalidType();
17114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
17124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // It's not a type, it has to be an expression list.
17144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Discard the comma locations - ActOnCXXNew has enough parameters.
17154c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  CommaLocsTy CommaLocs;
17164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return ParseExpressionList(PlacementArgs, CommaLocs);
17174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
17184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
17204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new.
17214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
172259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional
172359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
172459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location.  Otherwise, "Start" is the location of the
172559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token.
172659232d35f5820e334b6c8b007ae8006f4390055dChris Lattner///
17274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        delete-expression:
17284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' cast-expression
17294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' '[' ']' cast-expression
173059232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::OwningExprResult
173159232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
173259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
173359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken(); // Consume 'delete'
17344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Array delete?
17364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ArrayDelete = false;
17374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_square)) {
17384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ArrayDelete = true;
17394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LHS = ConsumeBracket();
17404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
17414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (RHS.isInvalid())
174220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
17434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
17444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17452f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Operand(ParseCastExpression(false));
17460e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Operand.isInvalid())
174720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return move(Operand);
17484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17499ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
17504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
175164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
17521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
175364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch(kind) {
175464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  default: assert(false && "Not a known unary type trait.");
175564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign;
175664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_copy:        return UTT_HasNothrowCopy;
175764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
175864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign;
175964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_copy:        return UTT_HasTrivialCopy;
176064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
176164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor;
176264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor;
176364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_abstract:             return UTT_IsAbstract;
176464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_class:                return UTT_IsClass;
176564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_empty:                return UTT_IsEmpty;
176664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_enum:                 return UTT_IsEnum;
176764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_pod:                  return UTT_IsPOD;
176864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_polymorphic:          return UTT_IsPolymorphic;
176964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_union:                return UTT_IsUnion;
1770ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl  case tok::kw___is_literal:              return UTT_IsLiteral;
177164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
177264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
177364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
177464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
177564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits
177664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates.
177764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
177864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///       primary-expression:
177964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU]             unary-type-trait '(' type-id ')'
178064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
17811eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParser::OwningExprResult Parser::ParseUnaryTypeTrait() {
178264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
178364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation Loc = ConsumeToken();
178464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
178564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation LParen = Tok.getLocation();
178664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
178764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return ExprError();
178864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
178964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
179064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // there will be cryptic errors about mismatched parentheses and missing
179164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // specifiers.
1792809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
179364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
179464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
179564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
1796809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
1797809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
1798809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
1799809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
180064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
1801f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1802f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1803f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1804f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens.
1805f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::OwningExprResult
1806f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1807b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                         ParsedType &CastTy,
1808f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation LParenLoc,
1809f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation &RParenLoc) {
1810f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(getLang().CPlusPlus && "Should only be called for C++!");
1811f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1812f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(isTypeIdInParens() && "Not a type-id!");
1813f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1814ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  OwningExprResult Result(true);
1815b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  CastTy = ParsedType();
1816f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1817f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // We need to disambiguate a very ugly part of the C++ syntax:
1818f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1819f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())x;  - type-id
1820f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())*x; - type-id
1821f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())/x; - expression
1822f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T());   - expression
1823f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1824f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The bad news is that we cannot use the specialized tentative parser, since
1825f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // it can only verify that the thing inside the parens can be parsed as
1826f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // type-id, it is not useful for determining the context past the parens.
1827f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1828f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The good news is that the parser can disambiguate this part without
1829a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis  // making any unnecessary Action calls.
1830f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  //
1831f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // It uses a scheme similar to parsing inline methods. The parenthesized
1832f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // tokens are cached, the context that follows is determined (possibly by
1833f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parsing a cast-expression), and then we re-introduce the cached tokens
1834f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // into the token stream and parse them appropriately.
1835f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
18361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParenParseOption ParseAs;
1837f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  CachedTokens Toks;
1838f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1839f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Store the tokens of the parentheses. We will parse them after we determine
1840f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // the context that follows them.
184114b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis  if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
1842f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We didn't find the ')' we expected.
1843f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
1844f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
1845f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
1846f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1847f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::l_brace)) {
1848f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = CompoundLiteral;
1849f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  } else {
1850f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    bool NotCastExpr;
1851b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1852b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1853b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      NotCastExpr = true;
1854b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    } else {
1855b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // Try parsing the cast-expression that may follow.
1856b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // If it is not a cast-expression, NotCastExpr will be true and no token
1857b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // will be consumed.
1858b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      Result = ParseCastExpression(false/*isUnaryExpression*/,
1859b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman                                   false/*isAddressofOperand*/,
1860b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   NotCastExpr,
1861b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   ParsedType()/*TypeOfCast*/);
1862b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    }
1863f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1864f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // If we parsed a cast-expression, it's really a type-id, otherwise it's
1865f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // an expression.
1866f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
1867f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
1868f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
18691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The current token should go after the cached tokens.
1870f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Toks.push_back(Tok);
1871f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Re-enter the stored parenthesized tokens into the token stream, so we may
1872f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parse them now.
1873f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  PP.EnterTokenStream(Toks.data(), Toks.size(),
1874f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1875f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Drop the current token and bring the first cached one. It's the same token
1876f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // as when we entered this function.
1877f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  ConsumeAnyToken();
1878f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1879f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (ParseAs >= CompoundLiteral) {
1880f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    TypeResult Ty = ParseTypeName();
1881f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1882f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Match the ')'.
1883f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Tok.is(tok::r_paren))
1884f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      RParenLoc = ConsumeParen();
1885f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    else
1886f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      MatchRHSPunctuation(tok::r_paren, LParenLoc);
1887f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1888f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (ParseAs == CompoundLiteral) {
1889f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      ExprType = CompoundLiteral;
1890f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1891f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    }
18921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1893f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1894f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    assert(ParseAs == CastExpr);
1895f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1896f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Ty.isInvalid())
1897f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ExprError();
1898f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1899f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    CastTy = Ty.get();
1900f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1901f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Result is what ParseCastExpression returned earlier.
1902f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    if (!Result.isInvalid())
190323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
19049ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     Result.take());
1905f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return move(Result);
1906f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
19071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1908f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Not a compound literal, and not followed by a cast-expression.
1909f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  assert(ParseAs == SimpleExpr);
1910f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1911f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  ExprType = SimpleExpr;
1912f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Result = ParseExpression();
1913f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (!Result.isInvalid() && Tok.is(tok::r_paren))
19149ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
1915f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1916f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // Match the ')'.
1917f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Result.isInvalid()) {
1918f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    SkipUntil(tok::r_paren);
1919f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
1920f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
19211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1922f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::r_paren))
1923f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    RParenLoc = ConsumeParen();
1924f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  else
1925f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
1926f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1927f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  return move(Result);
1928f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis}
1929