ParseExprCXX.cpp revision 059101f922de6eb765601459925f4c8914420b23
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements the Expression parsing implementation for C++.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h"
16bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor#include "RAIIObjectsForParser.h"
1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
1819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h"
193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h"
203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present.
242dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
252dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or
272dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream.
28eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
29eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'[opt] nested-name-specifier
30eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'
31eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
32eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       nested-name-specifier:
33eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         type-name '::'
34eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         namespace-name '::'
35eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         nested-name-specifier identifier '::'
362dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///         nested-name-specifier 'template'[opt] simple-template-id '::'
372dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
382dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed
402dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty)
412dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following
432dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the
442dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed.
45eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
462dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of
472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it.
482dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
49d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param MayBePseudoDestructor When non-NULL, points to a flag that
50d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// indicates whether this nested-name-specifier may be part of a
51d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// pseudo-destructor name. In this case, the flag will be set false
52d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we don't actually end up parsing a destructor name. Moreorover,
53d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we do end up determining that we are parsing a destructor name,
54d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// the last component of the nested-name-specifier is not parsed as
55d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// part of the scope specifier.
56d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
57b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor/// member access expression, e.g., the \p T:: in \p p->T::m.
58b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor///
599ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// \returns true if there was an error parsing a scope specifier
60495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
61b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType ObjectType,
62b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            bool EnteringContext,
63d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                            bool *MayBePseudoDestructor) {
644bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  assert(getLang().CPlusPlus &&
657452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Call sites of this function should be guarded by checking for C++");
661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
67eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::annot_cxxscope)) {
68c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
69c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 Tok.getAnnotationRange(),
70c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 SS);
71eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    ConsumeToken();
729ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
73eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
74e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner
7539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  bool HasScopeSpecifier = false;
7639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
775b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner  if (Tok.is(tok::coloncolon)) {
785b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    // ::new and ::delete aren't nested-name-specifiers.
795b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    tok::TokenKind NextKind = NextToken().getKind();
805b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
815b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner      return false;
821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8355a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    // '::' - Global scope qualifier.
842e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
852e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
862e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
8739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    HasScopeSpecifier = true;
88eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
89eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
90d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  bool CheckForDestructor = false;
91d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (MayBePseudoDestructor && *MayBePseudoDestructor) {
92d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CheckForDestructor = true;
93d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = false;
94d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
95d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
9639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  while (true) {
972dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (HasScopeSpecifier) {
982dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p5:
992dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the qualified-id has the form
1003b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
1012dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //       ::class-name-or-namespace-name::...
1023b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
1032dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the class-name-or-namespace-name is looked up in global scope as a
1042dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name.
1052dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
1062dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // To implement this, we clear out the object type as soon as we've
1072dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // seen a leading '::' or part of a nested-name-specifier.
108b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      ObjectType = ParsedType();
10981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
11081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      if (Tok.is(tok::code_completion)) {
11181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // Code completion for a nested-name-specifier, where the code
11281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // code completion token follows the '::'.
11323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
114dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor        ConsumeCodeCompletionToken();
11581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      }
1162dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
1171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier:
11977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    //   nested-name-specifier 'template'[opt] simple-template-id '::'
12077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner
12177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // Parse the optional 'template' keyword, then make sure we have
12277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // 'identifier <' after it.
12377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    if (Tok.is(tok::kw_template)) {
1242dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // If we don't have a scope specifier or an object type, this isn't a
125eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // nested-name-specifier, since they aren't allowed to start with
126eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // 'template'.
1272dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      if (!HasScopeSpecifier && !ObjectType)
128eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
129eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman
1307bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TentativeParsingAction TPA(*this);
13177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      SourceLocation TemplateKWLoc = ConsumeToken();
132ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
133ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      UnqualifiedId TemplateName;
134ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::identifier)) {
135ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
1367bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
137ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
138ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else if (Tok.is(tok::kw_operator)) {
139ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
1407bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor                                       TemplateName)) {
1417bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
142ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
1437bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        }
144ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
145e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt        if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
146e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt            TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
147ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          Diag(TemplateName.getSourceRange().getBegin(),
148ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor               diag::err_id_after_template_in_nested_name_spec)
149ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor            << TemplateName.getSourceRange();
1507bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
151ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
152ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        }
153ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
1547bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
15577cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        break;
15677cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      }
1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1587bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // If the next token is not '<', we have a qualified-id that refers
1597bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // to a template name, such as T::template apply, but is not a
1607bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // template-id.
1617bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      if (Tok.isNot(tok::less)) {
1627bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
1637bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        break;
1647bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      }
1657bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor
1667bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // Commit to parsing the template-id.
1677bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TPA.Commit();
168d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      TemplateTy Template;
16923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
170d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                TemplateKWLoc,
171d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                    SS,
172d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                  TemplateName,
173d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                    ObjectType,
174d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                EnteringContext,
175d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                    Template)) {
176059101f922de6eb765601459925f4c8914420b23Douglas Gregor        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
177d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                    TemplateKWLoc, false))
178d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          return true;
179d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      } else
1809ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      continue;
18377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    }
1841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We have
18739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
18839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //   simple-template-id '::'
18939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
19039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // So we need to check whether the simple-template-id is of the
191c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // right kind (it should name a type or be dependent), and then
192c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // convert it into a type within the nested-name-specifier.
1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateIdAnnotation *TemplateId
19439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
195d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
196d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
1979ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
198d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
199d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateId->Kind == TNK_Type_template ||
201c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor          TemplateId->Kind == TNK_Dependent_template_name) {
202aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor        // Consume the template-id token.
20339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        ConsumeToken();
204aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
20539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
20639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        SourceLocation CCLoc = ConsumeToken();
2071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
208c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor        if (!HasScopeSpecifier)
20939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor          HasScopeSpecifier = true;
2102e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
211aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor        ASTTemplateArgsPtr TemplateArgsPtr(Actions,
212aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                           TemplateId->getTemplateArgs(),
213aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                           TemplateId->NumArgs);
214aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
215aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor        if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
216aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                /*FIXME:*/SourceLocation(),
217aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                SS,
218aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                TemplateId->Template,
219aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                TemplateId->TemplateNameLoc,
220aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                TemplateId->LAngleLoc,
221aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                TemplateArgsPtr,
222aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                TemplateId->RAngleLoc,
223aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                CCLoc,
224640519e971af2bfe82d093d555146bdeecc8cc39Douglas Gregor                                                EnteringContext)) {
225640519e971af2bfe82d093d555146bdeecc8cc39Douglas Gregor          SourceLocation StartLoc
226640519e971af2bfe82d093d555146bdeecc8cc39Douglas Gregor            = SS.getBeginLoc().isValid()? SS.getBeginLoc()
227640519e971af2bfe82d093d555146bdeecc8cc39Douglas Gregor                                        : TemplateId->TemplateNameLoc;
228640519e971af2bfe82d093d555146bdeecc8cc39Douglas Gregor          SS.SetInvalid(SourceRange(StartLoc, CCLoc));
229640519e971af2bfe82d093d555146bdeecc8cc39Douglas Gregor        }
230aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
231aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor        TemplateId->Destroy();
23239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        continue;
23367b9e831943300ce54e564e601971828ce4def15Chris Lattner      }
2341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23567b9e831943300ce54e564e601971828ce4def15Chris Lattner      assert(false && "FIXME: Only type template names supported here");
23639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
23739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
2385c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2395c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // The rest of the nested-name-specifier possibilities start with
2405c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // tok::identifier.
2415c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Tok.isNot(tok::identifier))
2425c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      break;
2435c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2445c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    IdentifierInfo &II = *Tok.getIdentifierInfo();
2455c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2465c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
2475c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '::'
2485c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   namespace-name '::'
2495c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   nested-name-specifier identifier '::'
2505c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    Token Next = NextToken();
25146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
25246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // If we get foo:bar, this is almost certainly a typo for foo::bar.  Recover
25346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // and emit a fixit hint for it.
254b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor    if (Next.is(tok::colon) && !ColonIsSacred) {
2552e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
2562e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Tok.getLocation(),
2572e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Next.getLocation(), ObjectType,
258b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            EnteringContext) &&
259b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // If the token after the colon isn't an identifier, it's still an
260b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // error, but they probably meant something else strange so don't
261b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // recover like this.
262b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          PP.LookAhead(1).is(tok::identifier)) {
263b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
264849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateReplacement(Next.getLocation(), "::");
265b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor
266b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        // Recover as if the user wrote '::'.
267b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Next.setKind(tok::coloncolon);
268b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor      }
26946646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    }
27046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
2715c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::coloncolon)) {
27277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
27323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
27477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                                II, ObjectType)) {
275d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
2769ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
277d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
278d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
2795c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // We have an identifier followed by a '::'. Lookup this name
2805c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // as the name in a nested-name-specifier.
2815c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation IdLoc = ConsumeToken();
28246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner      assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
28346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner             "NextToken() not working properly!");
2845c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation CCLoc = ConsumeToken();
2851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2862e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      HasScopeSpecifier = true;
2872e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
2882e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                              ObjectType, EnteringContext, SS))
2892e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        SS.SetInvalid(SourceRange(IdLoc, CCLoc));
2902e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
2915c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      continue;
2925c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
2931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2945c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
2955c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '<'
2965c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::less)) {
2975c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      TemplateTy Template;
298014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
299014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(&II, Tok.getLocation());
3001fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
30123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
3027c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                              /*hasTemplateKeyword=*/false,
303014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                                        TemplateName,
3042dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                                        ObjectType,
305495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        EnteringContext,
3061fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                                        Template,
3071fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                              MemberOfUnknownSpecialization)) {
3085c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // We have found a template name, so annotate this this token
3095c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // with a template-id annotation. We do not permit the
3105c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // template-id to be translated into a type annotation,
3115c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // because some clients (e.g., the parsing of class template
3125c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // specializations) still want to see the original template-id
3135c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // token.
314ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
315059101f922de6eb765601459925f4c8914420b23Douglas Gregor        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
316ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                    SourceLocation(), false))
3179ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
3185c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
319d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      }
320d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
321d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
322d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          IsTemplateArgumentList(1)) {
323d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // We have something like t::getAs<T>, where getAs is a
324d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // member of an unknown specialization. However, this will only
325d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
326d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
327d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
328d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << II.getName()
329d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
330d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
331d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TemplateNameKind TNK
33223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor              = Actions.ActOnDependentTemplateName(getCurScope(),
333d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   Tok.getLocation(), SS,
334d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   TemplateName, ObjectType,
335d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   EnteringContext, Template)) {
336d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          // Consume the identifier.
337d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          ConsumeToken();
338059101f922de6eb765601459925f4c8914420b23Douglas Gregor          if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
339d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                      SourceLocation(), false))
340d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor            return true;
341d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        }
342d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        else
343d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          return true;
344d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor
345d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        continue;
3465c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
3475c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
3485c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
34939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // We don't have any tokens that form the beginning of a
35039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier, so we're done.
35139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    break;
35239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
3531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
354d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Even if we didn't see any pieces of a nested-name-specifier, we
355d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // still check whether there is a tilde in this position, which
356d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // indicates a potential pseudo-destructor.
357d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (CheckForDestructor && Tok.is(tok::tilde))
358d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = true;
359d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
3609ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
361eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
362eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
363eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression.
364eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
365eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       id-expression:
366eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         unqualified-id
367eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         qualified-id
368eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
369eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
370eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
371eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' identifier
372eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' operator-function-id
373edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor///         '::' template-id
374eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
375eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not
376eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect:
377eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
378eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' conversion-function-id
379eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' '~' class-name
380eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
381eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics:
382eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
383eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {};
384eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {}
385eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() {
386eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: A :: ~ C(); // Some Sema error about using destructor with a
387eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///                  // namespace.
388eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: ~ C(); // Some Parser error like 'unexpected ~'.
389eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// }
390eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
391eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like:
392eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
393eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
394eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
395eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' unqualified-id
396eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
397eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the
398eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope.
399eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
400ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a
401ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts,
402ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may
403ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear.
404ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl///
40560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
406eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  // qualified-id:
407eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
408eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::' unqualified-id
409eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //
410eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
411b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
41202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
41302a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  UnqualifiedId Name;
41402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  if (ParseUnqualifiedId(SS,
41502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*EnteringContext=*/false,
41602a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*AllowDestructorName=*/false,
41702a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*AllowConstructorName=*/false,
418b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                         /*ObjectType=*/ ParsedType(),
41902a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         Name))
42002a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor    return ExprError();
421b681b61fea36618778b8030360e90e3f4641233bJohn McCall
422b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // This is only the direct operand of an & operator if it is not
423b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // followed by a postfix-expression suffix.
4249c72c6088d591ace8503b842d39448c2040f3033John McCall  if (isAddressOfOperand && isPostfixExpressionSuffixStart())
4259c72c6088d591ace8503b842d39448c2040f3033John McCall    isAddressOfOperand = false;
42602a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
42723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
42802a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                                   isAddressOfOperand);
42902a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
430eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
431eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
4325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another
4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type.
4345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       postfix-expression: [C++ 5.2p1]
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'dynamic_cast' '<' type-name '>' '(' expression ')'
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'static_cast' '<' type-name '>' '(' expression ')'
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'const_cast' '<' type-name '>' '(' expression ')'
4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
44160d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXCasts() {
4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
4435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CastName = 0;     // For error messages
4445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Kind) {
4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown C++ cast!"); abort();
4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_const_cast:       CastName = "const_cast";       break;
4485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_static_cast:      CastName = "static_cast";      break;
4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation OpLoc = ConsumeToken();
4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LAngleBracketLoc = Tok.getLocation();
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
45720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
459809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult CastTy = ParseTypeName();
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation RAngleBracketLoc = Tok.getLocation();
4615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4621ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
46320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
4665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
46721e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
46821e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    return ExprError();
4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
47060d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = ParseExpression();
4711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47221e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  // Match the ')'.
47327591ff4fc64600fd67c5d81899e3efe5ef41a80Douglas Gregor  RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
475809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (!Result.isInvalid() && !CastTy.isInvalid())
47649badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
477f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                       LAngleBracketLoc, CastTy.get(),
478809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                       RAngleBracketLoc,
4799ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                       LParenLoc, Result.take(), RParenLoc);
4805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
48120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
484c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression.
485c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
486c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///       postfix-expression: [C++ 5.2p1]
487c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' expression ')'
488c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' type-id ')'
489c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
49060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXTypeid() {
491c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
492c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
493c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation OpLoc = ConsumeToken();
494c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation LParenLoc = Tok.getLocation();
495c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation RParenLoc;
496c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
497c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  // typeid expressions are always parenthesized.
498c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
499c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      "typeid"))
50020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
501c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
50260d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result;
503c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
504c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (isTypeIdInParens()) {
505809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult Ty = ParseTypeName();
506c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
507c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
5084eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
509c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
5104eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    if (Ty.isInvalid() || RParenLoc.isInvalid())
51120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
512c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
513c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
514b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                    Ty.get().getAsOpaquePtr(), RParenLoc);
515c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  } else {
516e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // C++0x [expr.typeid]p3:
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   When typeid is applied to an expression other than an lvalue of a
5181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   polymorphic class type [...] The expression is an unevaluated
519e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //   operand (Clause 5).
520e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //
5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Note that we can't tell whether the expression is an lvalue of a
522e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // polymorphic class type until after we've parsed the expression, so
523ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // we the expression is potentially potentially evaluated.
524ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(Actions,
525f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                       Sema::PotentiallyPotentiallyEvaluated);
526c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = ParseExpression();
527c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
528c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
5290e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Result.isInvalid())
530c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      SkipUntil(tok::r_paren);
531c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    else {
5324eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor      RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
5334eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor      if (RParenLoc.isInvalid())
5344eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor        return ExprError();
5354eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor
536c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
537effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl                                      Result.release(), RParenLoc);
538c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    }
539c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  }
540c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
54120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
542c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
543c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
54401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
54501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
54601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' expression ')'
54701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' type-id ')'
54801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
54901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetExprResult Parser::ParseCXXUuidof() {
55001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
55101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
55201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  SourceLocation OpLoc = ConsumeToken();
55301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  SourceLocation LParenLoc = Tok.getLocation();
55401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  SourceLocation RParenLoc;
55501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
55601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  // __uuidof expressions are always parenthesized.
55701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
55801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      "__uuidof"))
55901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    return ExprError();
56001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
56101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  ExprResult Result;
56201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
56301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  if (isTypeIdInParens()) {
56401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    TypeResult Ty = ParseTypeName();
56501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
56601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
56701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
56801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
56901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Ty.isInvalid())
57001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      return ExprError();
57101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
57201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
57301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                    Ty.get().getAsOpaquePtr(), RParenLoc);
57401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  } else {
57501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
57601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    Result = ParseExpression();
57701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
57801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
57901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Result.isInvalid())
58001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      SkipUntil(tok::r_paren);
58101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    else {
58201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
58301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
58401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
58501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                      Result.release(), RParenLoc);
58601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    }
58701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  }
58801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
58901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  return move(Result);
59001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet}
59101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
592d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \brief Parse a C++ pseudo-destructor expression after the base,
593d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// . or -> operator, and nested-name-specifier have already been
594d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// parsed.
595d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
596d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       postfix-expression: [C++ 5.2]
597d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression . pseudo-destructor-name
598d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression -> pseudo-destructor-name
599d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
600d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       pseudo-destructor-name:
601d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] type-name :: ~type-name
602d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier template simple-template-id ::
603d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///                 ~type-name
604d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] ~type-name
605d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
60660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
607d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
608d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 tok::TokenKind OpKind,
609d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 CXXScopeSpec &SS,
610b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                 ParsedType ObjectType) {
611d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // We're parsing either a pseudo-destructor-name or a dependent
612d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // member access that has the same form as a
613d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // pseudo-destructor-name. We parse both in the same way and let
614d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // the action model sort them out.
615d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  //
616d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Note that the ::[opt] nested-name-specifier[opt] has already
617d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been parsed, and if there was a simple-template-id, it has
618d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been coalesced into a template-id annotation token.
619d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId FirstTypeName;
620d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation CCLoc;
621d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::identifier)) {
622d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
623d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
624d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
625d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
626d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else if (Tok.is(tok::annot_template_id)) {
627d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setTemplateId(
628d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                              (TemplateIdAnnotation *)Tok.getAnnotationValue());
629d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
630d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
631d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
632d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else {
633d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(0, SourceLocation());
634d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
635d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
636d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the tilde.
637d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
638d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation TildeLoc = ConsumeToken();
639d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (!Tok.is(tok::identifier)) {
640d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    Diag(Tok, diag::err_destructor_tilde_identifier);
641d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
642d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
643d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
644d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the second type.
645d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId SecondTypeName;
646d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  IdentifierInfo *Name = Tok.getIdentifierInfo();
647d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation NameLoc = ConsumeToken();
648d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SecondTypeName.setIdentifier(Name, NameLoc);
649d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
650d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // If there is a '<', the second type name is a template-id. Parse
651d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // it as such.
652d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::less) &&
653d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
6540278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                   SecondTypeName, /*AssumeTemplateName=*/true,
6550278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                   /*TemplateKWLoc*/SourceLocation()))
656d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
657d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
6589ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
6599ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                           OpLoc, OpKind,
660d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           SS, FirstTypeName, CCLoc,
661d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           TildeLoc, SecondTypeName,
662d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           Tok.is(tok::l_paren));
663d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor}
664d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
6665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       boolean-literal: [C++ 2.13.5]
6685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'true'
6695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'false'
67060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXBoolLiteral() {
6715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
672f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
6735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
67450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
67550dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression.
67650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///
67750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///       throw-expression: [C++ 15]
67850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///         'throw' assignment-expression[opt]
67960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseThrowExpression() {
68050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  assert(Tok.is(tok::kw_throw) && "Not throw!");
68150dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
68220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl
6832a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // If the current token isn't the start of an assignment-expression,
6842a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // then the expression is not present.  This handles things like:
6852a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  //   "C ? throw : (void)42", which is crazy but legal.
6862a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
6872a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::semi:
6882a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_paren:
6892a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_square:
6902a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_brace:
6912a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::colon:
6922a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::comma:
6939ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return Actions.ActOnCXXThrow(ThrowLoc, 0);
69450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
6952a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  default:
69660d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Expr(ParseAssignmentExpression());
69720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    if (Expr.isInvalid()) return move(Expr);
6989ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
6992a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  }
70050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
7014cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
7024cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer.
7034cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
7044cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
7054cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which
7064cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called.
70760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXThis() {
7084cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  assert(Tok.is(tok::kw_this) && "Not 'this'!");
7094cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  SourceLocation ThisLoc = ConsumeToken();
710f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXThis(ThisLoc);
7114cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
712987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
713987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
714987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)")
715987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)")
716987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()").
717987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
718987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       postfix-expression: [C++ 5.2p1]
719987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         simple-type-specifier '(' expression-list[opt] ')'      [C++ 5.2.3]
720987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typename-specifier '(' expression-list[opt] ')'         [TODO]
721987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
72260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
72320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
724987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
725b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
726987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
727987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  assert(Tok.is(tok::l_paren) && "Expected '('!");
728bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor  GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
729bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor
730987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation LParenLoc = ConsumeParen();
731987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
732a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector Exprs(Actions);
733987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  CommaLocsTy CommaLocs;
734987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
735987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  if (Tok.isNot(tok::r_paren)) {
736987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    if (ParseExpressionList(Exprs, CommaLocs)) {
737987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis      SkipUntil(tok::r_paren);
73820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
739987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    }
740987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
741987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
742987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // Match the ')'.
743987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
744987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
745ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl  // TypeRep could be null, if it references an invalid typedef.
746ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl  if (!TypeRep)
747ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl    return ExprError();
748ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl
749987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
750987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis         "Unexpected number of commas!");
751ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
752a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                                           RParenLoc);
753987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
754987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
75599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// ParseCXXCondition - if/switch/while condition expression.
75671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
75771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///       condition:
75871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         expression
75971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         type-specifier-seq declarator '=' assignment-expression
76071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
76171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///             '=' assignment-expression
76271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
76399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param ExprResult if the condition was parsed as an expression, the
76499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed expression.
76599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
76699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param DeclResult if the condition was parsed as a declaration, the
76799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed declaration.
76899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
769586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param Loc The location of the start of the statement that requires this
770586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// condition, e.g., the "for" in a for loop.
771586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
772586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param ConvertToBoolean Whether the condition expression should be
773586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// converted to a boolean value.
774586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
77599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \returns true if there was a parsing, false otherwise.
77660d7b3a319d84d688752be3870615ac0f111fb16John McCallbool Parser::ParseCXXCondition(ExprResult &ExprOut,
77760d7b3a319d84d688752be3870615ac0f111fb16John McCall                               Decl *&DeclOut,
778586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               SourceLocation Loc,
779586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               bool ConvertToBoolean) {
78001dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  if (Tok.is(tok::code_completion)) {
781f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
782dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
78301dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  }
78401dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor
78599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!isCXXConditionDeclaration()) {
786586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // Parse the expression.
78760d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprOut = ParseExpression(); // expression
78860d7b3a319d84d688752be3870615ac0f111fb16John McCall    DeclOut = 0;
78960d7b3a319d84d688752be3870615ac0f111fb16John McCall    if (ExprOut.isInvalid())
790586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return true;
791586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
792586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // If required, convert to a boolean value.
793586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    if (ConvertToBoolean)
79460d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprOut
79560d7b3a319d84d688752be3870615ac0f111fb16John McCall        = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
79660d7b3a319d84d688752be3870615ac0f111fb16John McCall    return ExprOut.isInvalid();
79799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
79871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
79971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // type-specifier-seq
80071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  DeclSpec DS;
80171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
80271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
80371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // declarator
80471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
80571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
80671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
80771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // simple-asm-expr[opt]
80871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.is(tok::kw_asm)) {
809ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
81060d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult AsmLabel(ParseSimpleAsm(&Loc));
8110e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (AsmLabel.isInvalid()) {
81271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis      SkipUntil(tok::semi);
81399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return true;
81471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    }
815effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    DeclaratorInfo.setAsmLabel(AsmLabel.release());
816ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.SetRangeEnd(Loc);
81771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  }
81871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
81971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // If attributes are present, parse them.
8207f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseGNUAttributes(DeclaratorInfo);
82171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
82299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  // Type-check the declaration itself.
82360d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
8247f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                        DeclaratorInfo);
82560d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclOut = Dcl.get();
82660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprOut = ExprError();
827a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
82871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // '=' assignment-expression
829a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis  if (isTokenEqualOrMistypedEqualEqual(
830a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis                               diag::err_invalid_equalequal_after_declarator)) {
831dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeToken();
83260d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult AssignExpr(ParseAssignmentExpression());
83399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (!AssignExpr.isInvalid())
83434b41d939a1328f484511c6002ba2456db879a29Richard Smith      Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
83534b41d939a1328f484511c6002ba2456db879a29Richard Smith                                   DS.getTypeSpecType() == DeclSpec::TST_auto);
83699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  } else {
83799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // FIXME: C++0x allows a braced-init-list
83899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    Diag(Tok, diag::err_expected_equal_after_declarator);
83999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
84099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
841586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // FIXME: Build a reference to this declaration? Convert it to bool?
842586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // (This is currently handled by Sema).
843483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
844483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Actions.FinalizeDeclaration(DeclOut);
845586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
84699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return false;
84771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis}
84871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
8496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brief Determine whether the current token starts a C++
8506aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// simple-type-specifier.
8516aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::isCXXSimpleTypeSpecifier() const {
8526aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  switch (Tok.getKind()) {
8536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::annot_typename:
8546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_short:
8556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_long:
8566aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_signed:
8576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_unsigned:
8586aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_void:
8596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char:
8606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_int:
8616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_float:
8626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_double:
8636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_wchar_t:
8646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char16_t:
8656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char32_t:
8666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_bool:
8676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: C++0x decltype support.
8686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // GNU typeof support.
8696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_typeof:
8706aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
8716aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
8726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  default:
8736aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    break;
8746aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
8756aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
8766aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
8776aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
8786aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
879987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
880987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of
881987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier.
882987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
883987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       simple-type-specifier:
884eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier[opt] type-name
885987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
886987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         char
887987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         wchar_t
888987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         bool
889987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         short
890987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         int
891987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         long
892987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         signed
893987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         unsigned
894987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         float
895987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         double
896987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         void
897987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU]   typeof-specifier
898987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto               [TODO]
899987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
900987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       type-name:
901987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         class-name
902987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         enum-name
903987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typedef-name
904987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
905987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
906987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  DS.SetRangeStart(Tok.getLocation());
907987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  const char *PrevSpec;
908fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
909987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation Loc = Tok.getLocation();
9101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
911987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  switch (Tok.getKind()) {
91255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::identifier:   // foo::bar
91355a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::coloncolon:   // ::foo::bar
91455a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    assert(0 && "Annotation token should already be formed!");
9151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
916987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    assert(0 && "Not a simple-type-specifier token!");
917987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    abort();
91855a7cefc846765ac7d142a63f773747a20518d71Chris Lattner
919987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // type-name
920b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  case tok::annot_typename: {
9216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (getTypeAnnotation(Tok))
9226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
9236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                         getTypeAnnotation(Tok));
9246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    else
9256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecError();
9269bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
9279bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
9289bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    ConsumeToken();
9299bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
9309bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
9319bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
9329bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C interface.  If we don't have Objective-C or a '<', this is
9339bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // just a normal reference to a typedef name.
9349bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    if (Tok.is(tok::less) && getLang().ObjC1)
9359bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor      ParseObjCProtocolQualifiers(DS);
9369bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
9379bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.Finish(Diags, PP);
9389bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    return;
939987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
9401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
941987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // builtin types
942987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_short:
943fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
944987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
945987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_long:
946fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
947987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
948987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_signed:
949fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
950987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
951987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_unsigned:
952fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
953987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
954987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_void:
955fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
956987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
957987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_char:
958fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
959987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
960987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_int:
961fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
962987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
963987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_float:
964fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
965987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
966987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_double:
967fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
968987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
969987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_wchar_t:
970fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
971987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
972f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char16_t:
973fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
974f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
975f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char32_t:
976fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
977f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
978987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_bool:
979fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
980987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
9811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9826aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: C++0x decltype support.
983987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // GNU typeof support.
984987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_typeof:
985987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    ParseTypeofSpecifier(DS);
9869b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor    DS.Finish(Diags, PP);
987987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    return;
988987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
989b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  if (Tok.is(tok::annot_typename))
990eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
991eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  else
992eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getLocation());
993987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  ConsumeToken();
9949b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor  DS.Finish(Diags, PP);
995987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
9961cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
9972f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
9982f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers,
9992f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished
10002f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are
10012f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and
10022f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false
10032f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise.
10042f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
10052f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///   type-specifier-seq: [C++ 8.1]
10062f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///     type-specifier type-specifier-seq[opt]
10072f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
10082f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
10092f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  DS.SetRangeStart(Tok.getLocation());
10102f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  const char *PrevSpec = 0;
1011fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
1012fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  bool isInvalid = 0;
10132f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
10142f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // Parse one or more of the type specifiers.
1015d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1016d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl      ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
10179fa8e569407e02148888136609431a3fe083096dNick Lewycky    Diag(Tok, diag::err_expected_type);
10182f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor    return true;
10192f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  }
10201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1021d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1022d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl         ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1023d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  {}
10242f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
1025396a9f235e160093b5f803f7a6a18fad7b68bdbeDouglas Gregor  DS.Finish(Diags, PP);
10262f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  return false;
10272f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
10282f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
10293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of
10303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form.
10313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or
10333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
10343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will
10353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to
10363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller.
10373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if
10393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id.
10403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual
10423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name.
10433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or
10453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor.
10463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the
10483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
10493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
105046df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
105146df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
105246df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
10533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id
10543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully,
10553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id.
10563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1057d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param AssumeTemplateId When true, this routine will assume that the name
1058d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// refers to a template without performing name lookup to verify.
1059d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
10603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise.
10613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
10623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          IdentifierInfo *Name,
10633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          SourceLocation NameLoc,
10643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          bool EnteringContext,
1065b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          ParsedType ObjectType,
1066d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                          UnqualifiedId &Id,
10670278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          bool AssumeTemplateId,
10680278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          SourceLocation TemplateKWLoc) {
10690278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  assert((AssumeTemplateId || Tok.is(tok::less)) &&
10700278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor         "Expected '<' to finish parsing a template-id");
10713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateTy Template;
10733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateNameKind TNK = TNK_Non_template;
10743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (Id.getKind()) {
10753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
1076014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
1077e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
1078d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    if (AssumeTemplateId) {
107923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
1080d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Id, ObjectType, EnteringContext,
1081d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Template);
1082d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
1083d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        return true;
10841fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    } else {
10851fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
10867c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS,
10877c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateKWLoc.isValid(), Id,
10887c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   ObjectType, EnteringContext, Template,
10891fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
10901fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor
10911fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
10921fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          ObjectType && IsTemplateArgumentList()) {
10931fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // We have something like t->getAs<T>(), where getAs is a
10941fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // member of an unknown specialization. However, this will only
10951fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
10961fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
10971fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        std::string Name;
10981fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        if (Id.getKind() == UnqualifiedId::IK_Identifier)
10991fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = Id.Identifier->getName();
11001fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        else {
11011fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = "operator ";
11021fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
11031fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
11041fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          else
11051fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += Id.Identifier->getName();
11061fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        }
11071fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
11081fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << Name
11091fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << FixItHint::CreateInsertion(Id.StartLocation, "template ");
111023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
1111d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                 SS, Id, ObjectType,
1112d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                 EnteringContext, Template);
1113d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TNK == TNK_Non_template)
11141fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          return true;
11151fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      }
11161fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    }
11173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
11183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1119014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_ConstructorName: {
1120014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
11211fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1122014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
11237c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara    TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
11247c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                 TemplateName, ObjectType,
11251fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 EnteringContext, Template,
11261fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 MemberOfUnknownSpecialization);
11273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1128014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
11293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1130014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_DestructorName: {
1131014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
11321fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1133014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
11342d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (ObjectType) {
113523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
1136d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               TemplateName, ObjectType,
1137d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               EnteringContext, Template);
1138d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
11392d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
11402d1c21414199a7452f122598189363a3922605b1Douglas Gregor    } else {
11417c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
11427c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateName, ObjectType,
11431fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   EnteringContext, Template,
11441fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
11452d1c21414199a7452f122598189363a3922605b1Douglas Gregor
1146b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
1147124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        Diag(NameLoc, diag::err_destructor_template_id)
1148124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          << Name << SS.getRange();
11492d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
11502d1c21414199a7452f122598189363a3922605b1Douglas Gregor      }
11512d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
11523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1153014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
11543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  default:
11563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
11573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
11583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (TNK == TNK_Non_template)
11603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
11613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Parse the enclosed template argument list.
11633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
11643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgList TemplateArgs;
11650278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (Tok.is(tok::less) &&
11660278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
1167059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                       SS, true, LAngleLoc,
11683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgs,
11693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       RAngleLoc))
11703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
11713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
1173e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1174e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
11753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Form a parsed representation of the template-id to be stored in the
11763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // UnqualifiedId.
11773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateIdAnnotation *TemplateId
11783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      = TemplateIdAnnotation::Allocate(TemplateArgs.size());
11793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
11813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->Name = Id.Identifier;
1182014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = OO_None;
11833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
11843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
1185014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Name = 0;
1186014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = Id.OperatorFunctionId.Operator;
1187014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
11883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
11893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1190059101f922de6eb765601459925f4c8914420b23Douglas Gregor    TemplateId->SS = SS;
11912b5289b6fd7e3d9899868410a498c081c9595662John McCall    TemplateId->Template = Template;
11923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Kind = TNK;
11933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->LAngleLoc = LAngleLoc;
11943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->RAngleLoc = RAngleLoc;
1195314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
11963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
1197314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor         Arg != ArgEnd; ++Arg)
11983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Args[Arg] = TemplateArgs[Arg];
11993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setTemplateId(TemplateId);
12013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
12023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
12033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Bundle the template arguments together.
12053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
12063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                     TemplateArgs.size());
12073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Constructor and destructor names.
1209f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Type
1210059101f922de6eb765601459925f4c8914420b23Douglas Gregor    = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
12113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  LAngleLoc, TemplateArgsPtr,
12123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  RAngleLoc);
12133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Type.isInvalid())
12143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
12153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
12173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
12183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  else
12193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
12203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return false;
12223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
12233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1224ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part
1225ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id.
12263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1227ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or
1228ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way.
12293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1230ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
12313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       operator-function-id: [C++ 13.5]
12323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         'operator' operator
12333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1234ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       operator: one of
12353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            new   delete  new[]   delete[]
12363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            +     -    *  /    %  ^    &   |   ~
12373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            !     =    <  >    += -=   *=  /=  %=
12383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
12393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
12403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ()    []
12413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
12423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-function-id: [C++ 12.3.2]
12433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator conversion-type-id
12443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
12453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-type-id:
12463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         type-specifier-seq conversion-declarator[opt]
12473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
12483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-declarator:
12493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ptr-operator conversion-declarator[opt]
12503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode
12513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
12523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
12533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
12543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
12553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the
12563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
12573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1258ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
1259ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed.
1260ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1261ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
1262ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1263ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise.
1264ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1265b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType ObjectType,
1266ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        UnqualifiedId &Result) {
1267ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1268ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1269ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Consume the 'operator' keyword.
1270ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation KeywordLoc = ConsumeToken();
1271ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1272ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Determine what kind of operator name we have.
1273ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  unsigned SymbolIdx = 0;
1274ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation SymbolLocations[3];
1275ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  OverloadedOperatorKind Op = OO_None;
1276ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  switch (Tok.getKind()) {
1277ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_new:
1278ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_delete: {
1279ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      bool isNew = Tok.getKind() == tok::kw_new;
1280ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the 'new' or 'delete'.
1281ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();
1282ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::l_square)) {
1283ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the '['.
1284ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SourceLocation LBracketLoc = ConsumeBracket();
1285ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the ']'.
1286ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1287ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                         LBracketLoc);
1288ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (RBracketLoc.isInvalid())
1289ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          return true;
1290ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1291ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SymbolLocations[SymbolIdx++] = LBracketLoc;
1292ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SymbolLocations[SymbolIdx++] = RBracketLoc;
1293ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_Array_New : OO_Array_Delete;
1294ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
1295ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_New : OO_Delete;
1296ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
1297ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1298ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1299ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1300ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1301ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::Token:                                                     \
1302ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
1303ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_##Name;                                                    \
1304ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1305ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1306ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def"
1307ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1308ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_paren: {
1309ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the '('.
1310ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation LParenLoc = ConsumeParen();
1311ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the ')'.
1312ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1313ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                     LParenLoc);
1314ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (RParenLoc.isInvalid())
1315ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1316ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1317ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = LParenLoc;
1318ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = RParenLoc;
1319ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Call;
1320ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1321ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1322ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1323ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_square: {
1324ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the '['.
1325ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation LBracketLoc = ConsumeBracket();
1326ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the ']'.
1327ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1328ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                       LBracketLoc);
1329ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (RBracketLoc.isInvalid())
1330ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1331ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1332ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = LBracketLoc;
1333ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = RBracketLoc;
1334ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Subscript;
1335ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1336ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1337ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1338ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::code_completion: {
1339ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Code completion for the operator name.
134023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOperatorName(getCurScope());
1341ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1342ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the operator token.
1343dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
1344ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1345ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Don't try to parse any further.
1346ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return true;
1347ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1348ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1349ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    default:
1350ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1351ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
1352ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1353ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Op != OO_None) {
1354ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // We have parsed an operator-function-id.
1355ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1356ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return false;
1357ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
13580486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
13590486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  // Parse a literal-operator-id.
13600486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //
13610486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //   literal-operator-id: [C++0x 13.5.8]
13620486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //     operator "" identifier
13630486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
13640486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
13650486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    if (Tok.getLength() != 2)
13660486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
13670486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    ConsumeStringToken();
13680486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
13690486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    if (Tok.isNot(tok::identifier)) {
13700486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_expected_ident);
13710486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      return true;
13720486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    }
13730486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
13740486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    IdentifierInfo *II = Tok.getIdentifierInfo();
13750486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
13763e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    return false;
13770486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  }
1378ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1379ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse a conversion-function-id.
1380ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1381ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-function-id: [C++ 12.3.2]
1382ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     operator conversion-type-id
1383ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1384ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-type-id:
1385ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     type-specifier-seq conversion-declarator[opt]
1386ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1387ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-declarator:
1388ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     ptr-operator conversion-declarator[opt]
1389ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1390ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the type-specifier-seq.
1391ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DeclSpec DS;
1392f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor  if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
1393ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1394ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1395ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the conversion-declarator, which is merely a sequence of
1396ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // ptr-operators.
1397ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Declarator D(DS, Declarator::TypeNameContext);
1398ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1399ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1400ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Finish up the type.
1401f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
1402ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Ty.isInvalid())
1403ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1404ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1405ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Note that this is a conversion-function-id.
1406ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1407ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                 D.getSourceRange().getEnd());
1408ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return false;
1409ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
1410ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1411ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1412ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity.
1413ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1414ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
1415ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       unqualified-id: [C++ expr.prim.general]
1416ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         identifier
1417ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         operator-function-id
1418ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         conversion-function-id
1419ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO]
1420ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         ~ class-name
1421ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         template-id
1422ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1423ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode
1424ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1425ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
1426ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1427ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1428ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the
1429ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier.
1430ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
14313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name.
14323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
14333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name.
14343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
143546df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
143646df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
143746df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
14383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
14393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
14403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise.
14413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
14423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowDestructorName,
14433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowConstructorName,
1444b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                ParsedType ObjectType,
14453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                UnqualifiedId &Result) {
14460278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
14470278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // Handle 'A::template B'. This is for template-ids which have not
14480278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // already been annotated by ParseOptionalCXXScopeSpecifier().
14490278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  bool TemplateSpecified = false;
14500278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  SourceLocation TemplateKWLoc;
14510278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
14520278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      (ObjectType || SS.isSet())) {
14530278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateSpecified = true;
14540278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateKWLoc = ConsumeToken();
14550278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  }
14560278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
14573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
14583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   identifier
14593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (when it hasn't already been annotated)
14603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::identifier)) {
14613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the identifier.
14623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *Id = Tok.getIdentifierInfo();
14633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation IdLoc = ConsumeToken();
14643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1465b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    if (!getLang().CPlusPlus) {
1466b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // If we're not in C++, only identifiers matter. Record the
1467b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // identifier and return.
1468b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      Result.setIdentifier(Id, IdLoc);
1469b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      return false;
1470b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    }
1471b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor
14723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (AllowConstructorName &&
147323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
14743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed a constructor name.
147523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
14769e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                                    &SS, false, false,
14779e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                                    ParsedType(),
14789e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            /*NonTrivialTypeSourceInfo=*/true),
14793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                IdLoc, IdLoc);
14803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
14813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an identifier.
14823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setIdentifier(Id, IdLoc);
14833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
14843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
14853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // If the next token is a '<', we may have a template.
14860278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less))
14873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
14880278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          ObjectType, Result,
14890278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          TemplateSpecified, TemplateKWLoc);
14903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
14913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
14923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
14933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
14943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
14953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (already parsed and annotated)
14963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::annot_template_id)) {
14970efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    TemplateIdAnnotation *TemplateId
14980efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
14990efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
15000efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    // If the template-name names the current class, then this is a constructor
15010efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    if (AllowConstructorName && TemplateId->Name &&
150223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
15030efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      if (SS.isSet()) {
15040efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // C++ [class.qual]p2 specifies that a qualified template-name
15050efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // is taken as the constructor name where a constructor can be
15060efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // declared. Thus, the template arguments are extraneous, so
15070efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // complain about them and remove them entirely.
15080efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Diag(TemplateId->TemplateNameLoc,
15090efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor             diag::err_out_of_line_constructor_template_id)
15100efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor          << TemplateId->Name
1511849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateRemoval(
15120efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                    SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
15130efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
15140efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                                  TemplateId->TemplateNameLoc,
151523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor                                                      getCurScope(),
15169e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                                      &SS, false, false,
15179e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                                      ParsedType(),
15189e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            /*NontrivialTypeSourceInfo=*/true),
15190efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->TemplateNameLoc,
15200efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->RAngleLoc);
15210efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        TemplateId->Destroy();
15220efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        ConsumeToken();
15230efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        return false;
15240efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      }
15250efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
15260efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      Result.setConstructorTemplateId(TemplateId);
15270efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      ConsumeToken();
15280efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      return false;
15290efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    }
15300efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
15313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // We have already parsed a template-id; consume the annotation token as
15323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // our unqualified-id.
15330efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    Result.setTemplateId(TemplateId);
15343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ConsumeToken();
15353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
15363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
15373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
15393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   operator-function-id
15403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   conversion-function-id
15413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::kw_operator)) {
1542ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
15433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
15443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1545e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // If we have an operator-function-id or a literal-operator-id and the next
1546e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // token is a '<', we may have a
1547ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //
1548ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //   template-id:
1549ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //     operator-function-id < template-argument-list[opt] >
1550e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1551e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt         Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
15520278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor        (TemplateSpecified || Tok.is(tok::less)))
1553ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1554ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                          EnteringContext, ObjectType,
15550278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          Result,
15560278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          TemplateSpecified, TemplateKWLoc);
15573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
15593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
15603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1561b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor  if (getLang().CPlusPlus &&
1562b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
15633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // C++ [expr.unary.op]p10:
15643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   There is an ambiguity in the unary-expression ~X(), where X is a
15653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   class-name. The ambiguity is resolved in favor of treating ~ as a
15663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //    unary complement rather than treating ~X as referring to a destructor.
15673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the '~'.
15693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation TildeLoc = ConsumeToken();
15703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name.
15723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.isNot(tok::identifier)) {
1573124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      Diag(Tok, diag::err_destructor_tilde_identifier);
15743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
15753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
15763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name (or template-name in a simple-template-id).
15783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
15793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation ClassNameLoc = ConsumeToken();
15803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15810278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less)) {
1582b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
15832d1c21414199a7452f122598189363a3922605b1Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
15840278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          EnteringContext, ObjectType, Result,
15850278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          TemplateSpecified, TemplateKWLoc);
15862d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
15872d1c21414199a7452f122598189363a3922605b1Douglas Gregor
15883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a destructor name.
1589b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1590b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              ClassNameLoc, getCurScope(),
1591b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              SS, ObjectType,
1592b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              EnteringContext);
1593124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (!Ty)
15943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
1595124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
15963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
15973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
15983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
15993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16002d1c21414199a7452f122598189363a3922605b1Douglas Gregor  Diag(Tok, diag::err_expected_unqualified_id)
16012d1c21414199a7452f122598189363a3922605b1Douglas Gregor    << getLang().CPlusPlus;
16023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return true;
16033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
16043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
16064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors.
16071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
160859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has
160959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
161059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location.  Otherwise, "Start" is the location of the 'new' token.
16114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
16124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
16134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] new-type-id
16144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
16154c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
16164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
16174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
16184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
16194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
16204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1621cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-type-id:
1622cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   type-specifier-seq new-declarator[opt]
1623cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
1624cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-declarator:
1625cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   ptr-operator new-declarator[opt]
1626cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   direct-new-declarator
1627cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
16284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-initializer:
16294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list[opt] ')'
16304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// [C++0x]           braced-init-list                                   [TODO]
16314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
163260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
163359232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
163459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_new) && "expected 'new' token");
163559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken();   // Consume 'new'
16364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
16374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
16384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // second form of new-expression. It can't be a new-type-id.
16394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1640a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector PlacementArgs(Actions);
16414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation PlacementLParen, PlacementRParen;
16424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
16434bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor  SourceRange TypeIdParens;
1644cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  DeclSpec DS;
1645cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
16464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
16474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // If it turns out to be a placement, we change the type location.
16484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementLParen = ConsumeParen();
1649cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1650cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
165120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1652cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
16534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
16544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
1655cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementRParen.isInvalid()) {
1656cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
165720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1658cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
16594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1660cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementArgs.empty()) {
16614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Reset the placement locations. There was no placement.
16624bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor      TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
16634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      PlacementLParen = PlacementRParen = SourceLocation();
16644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    } else {
16654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // We still need the type.
16664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      if (Tok.is(tok::l_paren)) {
16674bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor        TypeIdParens.setBegin(ConsumeParen());
1668cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseSpecifierQualifierList(DS);
1669ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1670cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseDeclarator(DeclaratorInfo);
16714bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor        TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
16724bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor                                                TypeIdParens.getBegin()));
16734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      } else {
1674cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        if (ParseCXXTypeSpecifierSeq(DS))
1675cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          DeclaratorInfo.setInvalidType(true);
1676ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        else {
1677ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1678cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          ParseDeclaratorInternal(DeclaratorInfo,
1679cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                                  &Parser::ParseDirectNewDeclarator);
1680ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        }
16814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      }
16824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
16834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  } else {
1684cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // A new-type-id is a simplified type-id, where essentially the
1685cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // direct-declarator is replaced by a direct-new-declarator.
1686cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseCXXTypeSpecifierSeq(DS))
1687cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      DeclaratorInfo.setInvalidType(true);
1688ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    else {
1689ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1690cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      ParseDeclaratorInternal(DeclaratorInfo,
1691cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                              &Parser::ParseDirectNewDeclarator);
1692ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    }
16934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
1694eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (DeclaratorInfo.isInvalidType()) {
1695cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
169620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
1697cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
16984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1699a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector ConstructorArgs(Actions);
17004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation ConstructorLParen, ConstructorRParen;
17014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
17034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorLParen = ConsumeParen();
17044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (Tok.isNot(tok::r_paren)) {
17054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      CommaLocsTy CommaLocs;
1706cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1707cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
170820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl        return ExprError();
1709cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      }
17104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
17114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
1712cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ConstructorRParen.isInvalid()) {
1713cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
171420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1715cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
17164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
17174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1718f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1719f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(PlacementArgs), PlacementRParen,
17204bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor                             TypeIdParens, DeclaratorInfo, ConstructorLParen,
1721f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(ConstructorArgs), ConstructorRParen);
17224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
17234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
17254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal.
17264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
17274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        direct-new-declarator:
17284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '[' expression ']'
17294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   direct-new-declarator '[' constant-expression ']'
17304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
173159232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) {
17324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Parse the array dimensions.
17334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool first = true;
17344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  while (Tok.is(tok::l_square)) {
17354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LLoc = ConsumeBracket();
173660d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Size(first ? ParseExpression()
17372f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl                                : ParseConstantExpression());
17380e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Size.isInvalid()) {
17394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Recover
17404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      SkipUntil(tok::r_square);
17414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
17424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
17434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    first = false;
17444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1745ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
17467f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    D.AddTypeInfo(DeclaratorChunk::getArray(0, ParsedAttributes(),
17477f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                            /*static=*/false, /*star=*/false,
17487e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            Size.release(), LLoc, RLoc),
1749ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl                  RLoc);
17504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1751ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (RLoc.isInvalid())
17524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
17534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
17544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
17554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
17574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator.
17584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
17594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
17604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
17614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
17624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
17634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
17644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
17654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1766ca0408fb49c1370430672acf2d770b7151cf71deJohn McCallbool Parser::ParseExpressionListOrTypeId(
1767ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall                                   llvm::SmallVectorImpl<Expr*> &PlacementArgs,
176859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner                                         Declarator &D) {
17694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // The '(' was already consumed.
17704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (isTypeIdInParens()) {
1771cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseSpecifierQualifierList(D.getMutableDeclSpec());
1772ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetSourceRange(D.getDeclSpec().getSourceRange());
1773cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseDeclarator(D);
1774eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    return D.isInvalidType();
17754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
17764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // It's not a type, it has to be an expression list.
17784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Discard the comma locations - ActOnCXXNew has enough parameters.
17794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  CommaLocsTy CommaLocs;
17804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return ParseExpressionList(PlacementArgs, CommaLocs);
17814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
17824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
17844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new.
17854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
178659232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional
178759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
178859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location.  Otherwise, "Start" is the location of the
178959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token.
179059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner///
17914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        delete-expression:
17924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' cast-expression
17934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' '[' ']' cast-expression
179460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
179559232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
179659232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
179759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken(); // Consume 'delete'
17984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Array delete?
18004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ArrayDelete = false;
18014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_square)) {
18024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ArrayDelete = true;
18034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LHS = ConsumeBracket();
18044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
18054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (RHS.isInvalid())
180620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
18074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
18084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
180960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Operand(ParseCastExpression(false));
18100e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Operand.isInvalid())
181120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return move(Operand);
18124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
18139ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
18144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
181564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
18161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
181764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch(kind) {
181838c2b730a8553fa1cf369d0c5567f8b5d0a3dda8Francois Pichet  default: llvm_unreachable("Not a known unary type trait");
181964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign;
182064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_copy:        return UTT_HasNothrowCopy;
182164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
182264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign;
182364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_copy:        return UTT_HasTrivialCopy;
182464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
182564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor;
182664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor;
182764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_abstract:             return UTT_IsAbstract;
182864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_class:                return UTT_IsClass;
182964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_empty:                return UTT_IsEmpty;
183064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_enum:                 return UTT_IsEnum;
183164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_pod:                  return UTT_IsPOD;
183264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_polymorphic:          return UTT_IsPolymorphic;
183364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_union:                return UTT_IsUnion;
1834ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl  case tok::kw___is_literal:              return UTT_IsLiteral;
183564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
18366ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
18376ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
18386ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichetstatic BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
18396ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  switch(kind) {
184038c2b730a8553fa1cf369d0c5567f8b5d0a3dda8Francois Pichet  default: llvm_unreachable("Not a known binary type trait");
1841f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case tok::kw___is_base_of:                 return BTT_IsBaseOf;
1842f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
18439f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor  case tok::kw___is_convertible_to:          return BTT_IsConvertibleTo;
18446ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
184564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
184664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
184764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
184864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits
184964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates.
185064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
185164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///       primary-expression:
185264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU]             unary-type-trait '(' type-id ')'
185364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
185460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseUnaryTypeTrait() {
185564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
185664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation Loc = ConsumeToken();
185764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
185864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation LParen = Tok.getLocation();
185964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
186064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return ExprError();
186164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
186264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
186364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // there will be cryptic errors about mismatched parentheses and missing
186464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // specifiers.
1865809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
186664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
186764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
186864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
1869809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
1870809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
1871809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
18723d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor  return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
187364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
1874f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
18756ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
18766ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// pseudo-functions that allow implementation of the TR1/C++0x type traits
18776ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// templates.
18786ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///
18796ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///       primary-expression:
18806ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// [GNU]             binary-type-trait '(' type-id ',' type-id ')'
18816ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///
18826ad6f2848d7652ab2991286eb48be440d3493b28Francois PichetExprResult Parser::ParseBinaryTypeTrait() {
18836ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
18846ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  SourceLocation Loc = ConsumeToken();
18856ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
18866ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  SourceLocation LParen = Tok.getLocation();
18876ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
18886ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
18896ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
18906ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeResult LhsTy = ParseTypeName();
18916ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (LhsTy.isInvalid()) {
18926ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
18936ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
18946ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
18956ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
18966ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
18976ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
18986ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
18996ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
19006ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
19016ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeResult RhsTy = ParseTypeName();
19026ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (RhsTy.isInvalid()) {
19036ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
19046ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
19056ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
19066ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
19076ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
19086ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
19096ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
19106ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
19116ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
1912f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1913f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1914f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens.
191560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
1916f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1917b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                         ParsedType &CastTy,
1918f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation LParenLoc,
1919f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation &RParenLoc) {
1920f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(getLang().CPlusPlus && "Should only be called for C++!");
1921f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1922f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(isTypeIdInParens() && "Not a type-id!");
1923f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
192460d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(true);
1925b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  CastTy = ParsedType();
1926f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1927f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // We need to disambiguate a very ugly part of the C++ syntax:
1928f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1929f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())x;  - type-id
1930f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())*x; - type-id
1931f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())/x; - expression
1932f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T());   - expression
1933f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1934f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The bad news is that we cannot use the specialized tentative parser, since
1935f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // it can only verify that the thing inside the parens can be parsed as
1936f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // type-id, it is not useful for determining the context past the parens.
1937f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1938f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The good news is that the parser can disambiguate this part without
1939a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis  // making any unnecessary Action calls.
1940f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  //
1941f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // It uses a scheme similar to parsing inline methods. The parenthesized
1942f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // tokens are cached, the context that follows is determined (possibly by
1943f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parsing a cast-expression), and then we re-introduce the cached tokens
1944f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // into the token stream and parse them appropriately.
1945f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
19461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParenParseOption ParseAs;
1947f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  CachedTokens Toks;
1948f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1949f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Store the tokens of the parentheses. We will parse them after we determine
1950f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // the context that follows them.
195114b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis  if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
1952f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We didn't find the ')' we expected.
1953f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
1954f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
1955f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
1956f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1957f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::l_brace)) {
1958f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = CompoundLiteral;
1959f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  } else {
1960f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    bool NotCastExpr;
1961b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1962b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1963b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      NotCastExpr = true;
1964b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    } else {
1965b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // Try parsing the cast-expression that may follow.
1966b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // If it is not a cast-expression, NotCastExpr will be true and no token
1967b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // will be consumed.
1968b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      Result = ParseCastExpression(false/*isUnaryExpression*/,
1969b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman                                   false/*isAddressofOperand*/,
1970b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   NotCastExpr,
1971b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   ParsedType()/*TypeOfCast*/);
1972b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    }
1973f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1974f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // If we parsed a cast-expression, it's really a type-id, otherwise it's
1975f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // an expression.
1976f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
1977f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
1978f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
19791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The current token should go after the cached tokens.
1980f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Toks.push_back(Tok);
1981f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Re-enter the stored parenthesized tokens into the token stream, so we may
1982f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parse them now.
1983f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  PP.EnterTokenStream(Toks.data(), Toks.size(),
1984f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1985f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Drop the current token and bring the first cached one. It's the same token
1986f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // as when we entered this function.
1987f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  ConsumeAnyToken();
1988f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1989f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (ParseAs >= CompoundLiteral) {
1990f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    TypeResult Ty = ParseTypeName();
1991f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1992f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Match the ')'.
1993f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Tok.is(tok::r_paren))
1994f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      RParenLoc = ConsumeParen();
1995f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    else
1996f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      MatchRHSPunctuation(tok::r_paren, LParenLoc);
1997f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1998f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (ParseAs == CompoundLiteral) {
1999f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      ExprType = CompoundLiteral;
2000f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2001f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    }
20021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2003f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2004f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    assert(ParseAs == CastExpr);
2005f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2006f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Ty.isInvalid())
2007f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ExprError();
2008f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2009f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    CastTy = Ty.get();
2010f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2011f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Result is what ParseCastExpression returned earlier.
2012f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    if (!Result.isInvalid())
201323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
20149ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     Result.take());
2015f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return move(Result);
2016f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
20171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2018f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Not a compound literal, and not followed by a cast-expression.
2019f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  assert(ParseAs == SimpleExpr);
2020f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2021f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  ExprType = SimpleExpr;
2022f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Result = ParseExpression();
2023f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (!Result.isInvalid() && Tok.is(tok::r_paren))
20249ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
2025f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2026f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // Match the ')'.
2027f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Result.isInvalid()) {
2028f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    SkipUntil(tok::r_paren);
2029f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
2030f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
20311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2032f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::r_paren))
2033f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    RParenLoc = ConsumeParen();
2034f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  else
2035f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
2036f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2037f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  return move(Result);
2038f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis}
2039