ParseExprCXX.cpp revision edc90500b1d2587bf0b698fada14537d6741fddf
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements the Expression parsing implementation for C++.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h"
16987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis#include "clang/Parse/DeclSpec.h"
17314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor#include "clang/Parse/Template.h"
183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h"
193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present.
232dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
242dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or
262dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream.
27eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
28eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'[opt] nested-name-specifier
29eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'
30eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
31eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       nested-name-specifier:
32eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         type-name '::'
33eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         namespace-name '::'
34eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         nested-name-specifier identifier '::'
352dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///         nested-name-specifier 'template'[opt] simple-template-id '::'
362dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
372dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed
392dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty)
402dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following
422dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the
432dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed.
44eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
452dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of
462dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it.
472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
48d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param MayBePseudoDestructor When non-NULL, points to a flag that
49d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// indicates whether this nested-name-specifier may be part of a
50d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// pseudo-destructor name. In this case, the flag will be set false
51d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we don't actually end up parsing a destructor name. Moreorover,
52d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we do end up determining that we are parsing a destructor name,
53d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// the last component of the nested-name-specifier is not parsed as
54d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// part of the scope specifier.
55d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
56b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor/// member access expression, e.g., the \p T:: in \p p->T::m.
57b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor///
582dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \returns true if a scope specifier was parsed.
59495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
602dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                            Action::TypeTy *ObjectType,
61b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            bool EnteringContext,
62d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                            bool *MayBePseudoDestructor) {
634bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  assert(getLang().CPlusPlus &&
647452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Call sites of this function should be guarded by checking for C++");
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::annot_cxxscope)) {
673507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    SS.setScopeRep(Tok.getAnnotationValue());
68eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    SS.setRange(Tok.getAnnotationRange());
69eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    ConsumeToken();
704bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis    return true;
71eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
72e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner
7339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  bool HasScopeSpecifier = false;
7439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
755b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner  if (Tok.is(tok::coloncolon)) {
765b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    // ::new and ::delete aren't nested-name-specifiers.
775b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    tok::TokenKind NextKind = NextToken().getKind();
785b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
795b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner      return false;
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    // '::' - Global scope qualifier.
82357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner    SourceLocation CCLoc = ConsumeToken();
83357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner    SS.setBeginLoc(CCLoc);
843507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
85357089dea05855e27f80f6f244f9c60fc77cee83Chris Lattner    SS.setEndLoc(CCLoc);
8639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    HasScopeSpecifier = true;
87eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
88eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
89d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  bool CheckForDestructor = false;
90d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (MayBePseudoDestructor && *MayBePseudoDestructor) {
91d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CheckForDestructor = true;
92d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = false;
93d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
94d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
9539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  while (true) {
962dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (HasScopeSpecifier) {
972dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p5:
982dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the qualified-id has the form
993b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
1002dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //       ::class-name-or-namespace-name::...
1013b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
1022dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the class-name-or-namespace-name is looked up in global scope as a
1032dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name.
1042dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
1052dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // To implement this, we clear out the object type as soon as we've
1062dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // seen a leading '::' or part of a nested-name-specifier.
1072dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      ObjectType = 0;
10881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
10981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      if (Tok.is(tok::code_completion)) {
11081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // Code completion for a nested-name-specifier, where the code
11181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // code completion token follows the '::'.
11281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
11381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        ConsumeToken();
11481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      }
1152dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier:
11877cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    //   nested-name-specifier 'template'[opt] simple-template-id '::'
11977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner
12077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // Parse the optional 'template' keyword, then make sure we have
12177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // 'identifier <' after it.
12277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    if (Tok.is(tok::kw_template)) {
1232dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // If we don't have a scope specifier or an object type, this isn't a
124eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // nested-name-specifier, since they aren't allowed to start with
125eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // 'template'.
1262dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      if (!HasScopeSpecifier && !ObjectType)
127eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
128eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman
1297bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TentativeParsingAction TPA(*this);
13077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      SourceLocation TemplateKWLoc = ConsumeToken();
131ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
132ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      UnqualifiedId TemplateName;
133ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::identifier)) {
134ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
1357bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
136ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
137ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else if (Tok.is(tok::kw_operator)) {
138ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
1397bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor                                       TemplateName)) {
1407bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
141ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
1427bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        }
143ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
144e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt        if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
145e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt            TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
146ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          Diag(TemplateName.getSourceRange().getBegin(),
147ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor               diag::err_id_after_template_in_nested_name_spec)
148ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor            << TemplateName.getSourceRange();
1497bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
150ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
151ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        }
152ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
1537bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
15477cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        break;
15577cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      }
1561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1577bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // If the next token is not '<', we have a qualified-id that refers
1587bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // to a template name, such as T::template apply, but is not a
1597bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // template-id.
1607bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      if (Tok.isNot(tok::less)) {
1617bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
1627bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        break;
1637bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      }
1647bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor
1657bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // Commit to parsing the template-id.
1667bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TPA.Commit();
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateTy Template
168014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor        = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
169a481edb1b11c956a46cb42cd0dc4dd9851c10801Douglas Gregor                                             ObjectType, EnteringContext);
170eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      if (!Template)
171eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
172c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner      if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
173ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                  &SS, TemplateName, TemplateKWLoc, false))
174c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner        break;
1751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17677cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      continue;
17777cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    }
1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
1801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We have
18139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
18239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //   simple-template-id '::'
18339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
18439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // So we need to check whether the simple-template-id is of the
185c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // right kind (it should name a type or be dependent), and then
186c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // convert it into a type within the nested-name-specifier.
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateIdAnnotation *TemplateId
18839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
189d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
190d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
191d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        return HasScopeSpecifier;
192d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
193d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateId->Kind == TNK_Type_template ||
195c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor          TemplateId->Kind == TNK_Dependent_template_name) {
19631a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor        AnnotateTemplateIdTokenAsType(&SS);
19739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
1981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        assert(Tok.is(tok::annot_typename) &&
19939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor               "AnnotateTemplateIdTokenAsType isn't working");
20039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        Token TypeToken = Tok;
20139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        ConsumeToken();
20239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
20339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        SourceLocation CCLoc = ConsumeToken();
2041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        if (!HasScopeSpecifier) {
20639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor          SS.setBeginLoc(TypeToken.getLocation());
20739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor          HasScopeSpecifier = true;
20839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        }
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21031a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor        if (TypeToken.getAnnotationValue())
21131a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor          SS.setScopeRep(
2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
21331a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor                                                TypeToken.getAnnotationValue(),
21431a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor                                                TypeToken.getAnnotationRange(),
215edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor                                                CCLoc));
21631a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor        else
21731a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor          SS.setScopeRep(0);
21839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        SS.setEndLoc(CCLoc);
21939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor        continue;
22067b9e831943300ce54e564e601971828ce4def15Chris Lattner      }
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22267b9e831943300ce54e564e601971828ce4def15Chris Lattner      assert(false && "FIXME: Only type template names supported here");
22339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
22439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
2255c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2265c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // The rest of the nested-name-specifier possibilities start with
2275c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // tok::identifier.
2285c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Tok.isNot(tok::identifier))
2295c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      break;
2305c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2315c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    IdentifierInfo &II = *Tok.getIdentifierInfo();
2325c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2335c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
2345c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '::'
2355c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   namespace-name '::'
2365c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   nested-name-specifier identifier '::'
2375c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    Token Next = NextToken();
23846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
23946646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // If we get foo:bar, this is almost certainly a typo for foo::bar.  Recover
24046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // and emit a fixit hint for it.
241b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor    if (Next.is(tok::colon) && !ColonIsSacred) {
242edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor      if (Actions.IsInvalidUnlessNestedName(CurScope, SS, II, ObjectType,
243b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            EnteringContext) &&
244b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // If the token after the colon isn't an identifier, it's still an
245b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // error, but they probably meant something else strange so don't
246b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // recover like this.
247b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          PP.LookAhead(1).is(tok::identifier)) {
248b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
249b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          << CodeModificationHint::CreateReplacement(Next.getLocation(), "::");
250b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor
251b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        // Recover as if the user wrote '::'.
252b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Next.setKind(tok::coloncolon);
253b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor      }
25446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    }
25546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
2565c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::coloncolon)) {
25777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
25877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor          !Actions.isNonTypeNestedNameSpecifier(CurScope, SS, Tok.getLocation(),
25977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                                II, ObjectType)) {
260d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
261d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        return HasScopeSpecifier;
262d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
263d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
2645c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // We have an identifier followed by a '::'. Lookup this name
2655c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // as the name in a nested-name-specifier.
2665c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation IdLoc = ConsumeToken();
26746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner      assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
26846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner             "NextToken() not working properly!");
2695c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation CCLoc = ConsumeToken();
2701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2715c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      if (!HasScopeSpecifier) {
2725c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        SS.setBeginLoc(IdLoc);
2735c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        HasScopeSpecifier = true;
2745c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
2751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2765c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      if (SS.isInvalid())
2775c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2795c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SS.setScopeRep(
280495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor        Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
281edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor                                            ObjectType, EnteringContext));
2825c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SS.setEndLoc(CCLoc);
2835c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      continue;
2845c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
2851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2865c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
2875c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '<'
2885c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::less)) {
2895c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      TemplateTy Template;
290014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
291014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(&II, Tok.getLocation());
292014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
293014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                                        TemplateName,
2942dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                                        ObjectType,
295495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        EnteringContext,
296495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        Template)) {
2975c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // We have found a template name, so annotate this this token
2985c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // with a template-id annotation. We do not permit the
2995c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // template-id to be translated into a type annotation,
3005c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // because some clients (e.g., the parsing of class template
3015c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // specializations) still want to see the original template-id
3025c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // token.
303ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
304ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
305ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                    SourceLocation(), false))
306c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          break;
3075c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
3085c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
3095c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
3105c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
31139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // We don't have any tokens that form the beginning of a
31239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier, so we're done.
31339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    break;
31439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
316d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Even if we didn't see any pieces of a nested-name-specifier, we
317d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // still check whether there is a tilde in this position, which
318d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // indicates a potential pseudo-destructor.
319d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (CheckForDestructor && Tok.is(tok::tilde))
320d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = true;
321d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
32239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  return HasScopeSpecifier;
323eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
324eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
325eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression.
326eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
327eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       id-expression:
328eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         unqualified-id
329eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         qualified-id
330eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
331eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
332eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
333eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' identifier
334eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' operator-function-id
335edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor///         '::' template-id
336eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
337eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not
338eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect:
339eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
340eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' conversion-function-id
341eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' '~' class-name
342eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
343eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics:
344eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
345eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {};
346eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {}
347eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() {
348eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: A :: ~ C(); // Some Sema error about using destructor with a
349eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///                  // namespace.
350eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: ~ C(); // Some Parser error like 'unexpected ~'.
351eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// }
352eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
353eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like:
354eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
355eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
356eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
357eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' unqualified-id
358eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
359eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the
360eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope.
361eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
362ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a
363ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts,
364ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may
365ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear.
366ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl///
367ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian RedlParser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
368eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  // qualified-id:
369eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
370eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::' unqualified-id
371eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //
372eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
3732dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
37402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
37502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  UnqualifiedId Name;
37602a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  if (ParseUnqualifiedId(SS,
37702a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*EnteringContext=*/false,
37802a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*AllowDestructorName=*/false,
37902a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*AllowConstructorName=*/false,
3802d1c21414199a7452f122598189363a3922605b1Douglas Gregor                         /*ObjectType=*/0,
38102a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         Name))
38202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor    return ExprError();
383b681b61fea36618778b8030360e90e3f4641233bJohn McCall
384b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // This is only the direct operand of an & operator if it is not
385b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // followed by a postfix-expression suffix.
386b681b61fea36618778b8030360e90e3f4641233bJohn McCall  if (isAddressOfOperand) {
387b681b61fea36618778b8030360e90e3f4641233bJohn McCall    switch (Tok.getKind()) {
388b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::l_square:
389b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::l_paren:
390b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::arrow:
391b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::period:
392b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::plusplus:
393b681b61fea36618778b8030360e90e3f4641233bJohn McCall    case tok::minusminus:
394b681b61fea36618778b8030360e90e3f4641233bJohn McCall      isAddressOfOperand = false;
395b681b61fea36618778b8030360e90e3f4641233bJohn McCall      break;
396b681b61fea36618778b8030360e90e3f4641233bJohn McCall
397b681b61fea36618778b8030360e90e3f4641233bJohn McCall    default:
398b681b61fea36618778b8030360e90e3f4641233bJohn McCall      break;
399b681b61fea36618778b8030360e90e3f4641233bJohn McCall    }
400b681b61fea36618778b8030360e90e3f4641233bJohn McCall  }
40102a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
40202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
40302a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                                   isAddressOfOperand);
40402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
405eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
406eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
4075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another
4085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type.
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       postfix-expression: [C++ 5.2p1]
4115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'dynamic_cast' '<' type-name '>' '(' expression ')'
4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'static_cast' '<' type-name '>' '(' expression ')'
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'const_cast' '<' type-name '>' '(' expression ')'
4155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
41620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXCasts() {
4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CastName = 0;     // For error messages
4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Kind) {
4215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown C++ cast!"); abort();
4225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_const_cast:       CastName = "const_cast";       break;
4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
4245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
4255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_static_cast:      CastName = "static_cast";      break;
4265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation OpLoc = ConsumeToken();
4295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LAngleBracketLoc = Tok.getLocation();
4305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
43220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
434809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult CastTy = ParseTypeName();
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation RAngleBracketLoc = Tok.getLocation();
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4371ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
43820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
4415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
44221e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
44321e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    return ExprError();
4445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
44521e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  OwningExprResult Result = ParseExpression();
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44721e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  // Match the ')'.
44827591ff4fc64600fd67c5d81899e3efe5ef41a80Douglas Gregor  RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
450809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (!Result.isInvalid() && !CastTy.isInvalid())
45149badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
452f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                       LAngleBracketLoc, CastTy.get(),
453809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                       RAngleBracketLoc,
454f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                       LParenLoc, move(Result), RParenLoc);
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
45620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
459c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression.
460c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
461c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///       postfix-expression: [C++ 5.2p1]
462c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' expression ')'
463c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' type-id ')'
464c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
46520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXTypeid() {
466c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
467c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
468c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation OpLoc = ConsumeToken();
469c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation LParenLoc = Tok.getLocation();
470c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation RParenLoc;
471c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
472c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  // typeid expressions are always parenthesized.
473c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
474c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      "typeid"))
47520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
476c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
47715faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningExprResult Result(Actions);
478c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
479c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (isTypeIdInParens()) {
480809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult Ty = ParseTypeName();
481c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
482c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
483c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    MatchRHSPunctuation(tok::r_paren, LParenLoc);
484c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
485809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (Ty.isInvalid())
48620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
487c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
488c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
489809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                    Ty.get(), RParenLoc);
490c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  } else {
491e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // C++0x [expr.typeid]p3:
4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   When typeid is applied to an expression other than an lvalue of a
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   polymorphic class type [...] The expression is an unevaluated
494e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //   operand (Clause 5).
495e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //
4961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Note that we can't tell whether the expression is an lvalue of a
497e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // polymorphic class type until after we've parsed the expression, so
498ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // we the expression is potentially potentially evaluated.
499ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(Actions,
500ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                       Action::PotentiallyPotentiallyEvaluated);
501c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = ParseExpression();
502c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
503c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
5040e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Result.isInvalid())
505c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      SkipUntil(tok::r_paren);
506c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    else {
507c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      MatchRHSPunctuation(tok::r_paren, LParenLoc);
508c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
509c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
510effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl                                      Result.release(), RParenLoc);
511c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    }
512c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  }
513c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
51420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
515c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
516c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
517d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \brief Parse a C++ pseudo-destructor expression after the base,
518d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// . or -> operator, and nested-name-specifier have already been
519d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// parsed.
520d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
521d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       postfix-expression: [C++ 5.2]
522d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression . pseudo-destructor-name
523d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression -> pseudo-destructor-name
524d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
525d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       pseudo-destructor-name:
526d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] type-name :: ~type-name
527d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier template simple-template-id ::
528d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///                 ~type-name
529d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] ~type-name
530d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
531d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::OwningExprResult
532d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
533d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 tok::TokenKind OpKind,
534d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 CXXScopeSpec &SS,
535d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 Action::TypeTy *ObjectType) {
536d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // We're parsing either a pseudo-destructor-name or a dependent
537d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // member access that has the same form as a
538d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // pseudo-destructor-name. We parse both in the same way and let
539d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // the action model sort them out.
540d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  //
541d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Note that the ::[opt] nested-name-specifier[opt] has already
542d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been parsed, and if there was a simple-template-id, it has
543d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been coalesced into a template-id annotation token.
544d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId FirstTypeName;
545d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation CCLoc;
546d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::identifier)) {
547d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
548d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
549d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
550d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
551d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else if (Tok.is(tok::annot_template_id)) {
552d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setTemplateId(
553d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                              (TemplateIdAnnotation *)Tok.getAnnotationValue());
554d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
555d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
556d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
557d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else {
558d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(0, SourceLocation());
559d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
560d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
561d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the tilde.
562d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
563d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation TildeLoc = ConsumeToken();
564d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (!Tok.is(tok::identifier)) {
565d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    Diag(Tok, diag::err_destructor_tilde_identifier);
566d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
567d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
568d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
569d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the second type.
570d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId SecondTypeName;
571d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  IdentifierInfo *Name = Tok.getIdentifierInfo();
572d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation NameLoc = ConsumeToken();
573d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SecondTypeName.setIdentifier(Name, NameLoc);
574d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
575d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // If there is a '<', the second type name is a template-id. Parse
576d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // it as such.
577d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::less) &&
578d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
579d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                   SecondTypeName, /*AssumeTemplateName=*/true))
580d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
581d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
582d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  return Actions.ActOnPseudoDestructorExpr(CurScope, move(Base), OpLoc, OpKind,
583d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           SS, FirstTypeName, CCLoc,
584d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           TildeLoc, SecondTypeName,
585d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           Tok.is(tok::l_paren));
586d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor}
587d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
5885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       boolean-literal: [C++ 2.13.5]
5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'true'
5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'false'
59320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXBoolLiteral() {
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
595f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
59750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
59850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression.
59950dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///
60050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///       throw-expression: [C++ 15]
60150dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///         'throw' assignment-expression[opt]
60220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseThrowExpression() {
60350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  assert(Tok.is(tok::kw_throw) && "Not throw!");
60450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
60520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl
6062a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // If the current token isn't the start of an assignment-expression,
6072a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // then the expression is not present.  This handles things like:
6082a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  //   "C ? throw : (void)42", which is crazy but legal.
6092a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
6102a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::semi:
6112a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_paren:
6122a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_square:
6132a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_brace:
6142a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::colon:
6152a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::comma:
616f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl    return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
61750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
6182a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  default:
6192f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    OwningExprResult Expr(ParseAssignmentExpression());
62020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    if (Expr.isInvalid()) return move(Expr);
621f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl    return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
6222a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  }
62350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
6244cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
6254cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer.
6264cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
6274cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
6284cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which
6294cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called.
63020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult Parser::ParseCXXThis() {
6314cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  assert(Tok.is(tok::kw_this) && "Not 'this'!");
6324cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  SourceLocation ThisLoc = ConsumeToken();
633f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXThis(ThisLoc);
6344cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
635987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
636987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
637987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)")
638987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)")
639987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()").
640987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
641987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       postfix-expression: [C++ 5.2p1]
642987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         simple-type-specifier '(' expression-list[opt] ')'      [C++ 5.2.3]
643987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typename-specifier '(' expression-list[opt] ')'         [TODO]
644987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
64520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::OwningExprResult
64620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
647987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
6485ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor  TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
649987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
650987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  assert(Tok.is(tok::l_paren) && "Expected '('!");
651987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation LParenLoc = ConsumeParen();
652987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
653a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector Exprs(Actions);
654987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  CommaLocsTy CommaLocs;
655987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
656987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  if (Tok.isNot(tok::r_paren)) {
657987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    if (ParseExpressionList(Exprs, CommaLocs)) {
658987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis      SkipUntil(tok::r_paren);
65920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
660987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    }
661987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
662987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
663987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // Match the ')'.
664987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
665987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
666ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl  // TypeRep could be null, if it references an invalid typedef.
667ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl  if (!TypeRep)
668ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl    return ExprError();
669ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl
670987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
671987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis         "Unexpected number of commas!");
672f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
673f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                           LParenLoc, move_arg(Exprs),
674beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                           CommaLocs.data(), RParenLoc);
675987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
676987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
67799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// ParseCXXCondition - if/switch/while condition expression.
67871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
67971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///       condition:
68071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         expression
68171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         type-specifier-seq declarator '=' assignment-expression
68271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
68371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///             '=' assignment-expression
68471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
68599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param ExprResult if the condition was parsed as an expression, the
68699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed expression.
68799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
68899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param DeclResult if the condition was parsed as a declaration, the
68999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed declaration.
69099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
69199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \returns true if there was a parsing, false otherwise.
69299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregorbool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
69399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                               DeclPtrTy &DeclResult) {
69401dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  if (Tok.is(tok::code_completion)) {
69501dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Condition);
69601dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    ConsumeToken();
69701dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  }
69801dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor
69999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!isCXXConditionDeclaration()) {
70099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    ExprResult = ParseExpression(); // expression
70199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    DeclResult = DeclPtrTy();
70299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    return ExprResult.isInvalid();
70399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
70471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
70571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // type-specifier-seq
70671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  DeclSpec DS;
70771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
70871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
70971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // declarator
71071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
71171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
71271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
71371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // simple-asm-expr[opt]
71471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.is(tok::kw_asm)) {
715ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
716ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
7170e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (AsmLabel.isInvalid()) {
71871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis      SkipUntil(tok::semi);
71999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return true;
72071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    }
721effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    DeclaratorInfo.setAsmLabel(AsmLabel.release());
722ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.SetRangeEnd(Loc);
72371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  }
72471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
72571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // If attributes are present, parse them.
726ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Tok.is(tok::kw___attribute)) {
727ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
728bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    AttributeList *AttrList = ParseGNUAttributes(&Loc);
729ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.AddAttributes(AttrList, Loc);
730ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
73171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
73299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  // Type-check the declaration itself.
73399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope,
73499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                                                                DeclaratorInfo);
73599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  DeclResult = Dcl.get();
73699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  ExprResult = ExprError();
73799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
73871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // '=' assignment-expression
73999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (Tok.is(tok::equal)) {
74099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    SourceLocation EqualLoc = ConsumeToken();
74199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    OwningExprResult AssignExpr(ParseAssignmentExpression());
74299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (!AssignExpr.isInvalid())
74399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
74499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  } else {
74599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // FIXME: C++0x allows a braced-init-list
74699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    Diag(Tok, diag::err_expected_equal_after_declarator);
74799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
74899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
74999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return false;
75071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis}
75171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
752987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
753987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of
754987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier.
755987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
756987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       simple-type-specifier:
757eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier[opt] type-name
758987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
759987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         char
760987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         wchar_t
761987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         bool
762987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         short
763987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         int
764987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         long
765987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         signed
766987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         unsigned
767987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         float
768987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         double
769987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         void
770987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU]   typeof-specifier
771987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto               [TODO]
772987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
773987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       type-name:
774987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         class-name
775987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         enum-name
776987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typedef-name
777987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
778987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
779987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  DS.SetRangeStart(Tok.getLocation());
780987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  const char *PrevSpec;
781fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
782987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation Loc = Tok.getLocation();
7831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
784987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  switch (Tok.getKind()) {
78555a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::identifier:   // foo::bar
78655a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::coloncolon:   // ::foo::bar
78755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    assert(0 && "Annotation token should already be formed!");
7881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
789987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    assert(0 && "Not a simple-type-specifier token!");
790987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    abort();
79155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner
792987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // type-name
793b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  case tok::annot_typename: {
794fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
795eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis                       Tok.getAnnotationValue());
796987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
797987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
7981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
799987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // builtin types
800987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_short:
801fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
802987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
803987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_long:
804fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
805987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
806987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_signed:
807fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
808987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
809987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_unsigned:
810fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
811987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
812987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_void:
813fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
814987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
815987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_char:
816fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
817987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
818987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_int:
819fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
820987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
821987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_float:
822fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
823987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
824987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_double:
825fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
826987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
827987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_wchar_t:
828fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
829987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
830f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char16_t:
831fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
832f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
833f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char32_t:
834fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
835f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
836987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_bool:
837fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
838987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
8391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
840987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // GNU typeof support.
841987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_typeof:
842987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    ParseTypeofSpecifier(DS);
8439b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor    DS.Finish(Diags, PP);
844987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    return;
845987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
846b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  if (Tok.is(tok::annot_typename))
847eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
848eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  else
849eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getLocation());
850987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  ConsumeToken();
8519b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor  DS.Finish(Diags, PP);
852987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
8531cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
8542f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
8552f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers,
8562f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished
8572f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are
8582f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and
8592f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false
8602f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise.
8612f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
8622f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///   type-specifier-seq: [C++ 8.1]
8632f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///     type-specifier type-specifier-seq[opt]
8642f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
8652f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
8662f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  DS.SetRangeStart(Tok.getLocation());
8672f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  const char *PrevSpec = 0;
868fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
869fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  bool isInvalid = 0;
8702f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
8712f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // Parse one or more of the type specifiers.
872d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
873d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl      ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
8741ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_operator_missing_type_specifier);
8752f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor    return true;
8762f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  }
8771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
878d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
879d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl         ParsedTemplateInfo(), /*SuppressDeclarations*/true))
880d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  {}
8812f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
882396a9f235e160093b5f803f7a6a18fad7b68bdbeDouglas Gregor  DS.Finish(Diags, PP);
8832f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  return false;
8842f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
8852f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
8863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of
8873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form.
8883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or
8903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
8913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will
8923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to
8933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller.
8943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if
8963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id.
8973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
8983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual
8993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name.
9003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
9013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or
9023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor.
9033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
9043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the
9053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
9063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
90746df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
90846df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
90946df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
9103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id
9113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully,
9123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id.
9133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
914d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param AssumeTemplateId When true, this routine will assume that the name
915d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// refers to a template without performing name lookup to verify.
916d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
9173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise.
9183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
9193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          IdentifierInfo *Name,
9203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          SourceLocation NameLoc,
9213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          bool EnteringContext,
9222d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                          TypeTy *ObjectType,
923d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                          UnqualifiedId &Id,
924d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                          bool AssumeTemplateId) {
9253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
9263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateTy Template;
9283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateNameKind TNK = TNK_Non_template;
9293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (Id.getKind()) {
9303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
931014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
932e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
933d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    if (AssumeTemplateId) {
934d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
935d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                                    Id, ObjectType,
936d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                                    EnteringContext);
937d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      TNK = TNK_Dependent_template_name;
938d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      if (!Template.get())
939d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        return true;
940d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    } else
941d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType,
942d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                   EnteringContext, Template);
9433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
9443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
945014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_ConstructorName: {
946014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
947014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
948014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
9492d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                 EnteringContext, Template);
9503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
951014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
9523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
953014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_DestructorName: {
954014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
955014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
9562d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (ObjectType) {
957014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
958a481edb1b11c956a46cb42cd0dc4dd9851c10801Douglas Gregor                                                    TemplateName, ObjectType,
959a481edb1b11c956a46cb42cd0dc4dd9851c10801Douglas Gregor                                                    EnteringContext);
9602d1c21414199a7452f122598189363a3922605b1Douglas Gregor      TNK = TNK_Dependent_template_name;
9612d1c21414199a7452f122598189363a3922605b1Douglas Gregor      if (!Template.get())
9622d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
9632d1c21414199a7452f122598189363a3922605b1Douglas Gregor    } else {
964014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
9652d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                   EnteringContext, Template);
9662d1c21414199a7452f122598189363a3922605b1Douglas Gregor
9672d1c21414199a7452f122598189363a3922605b1Douglas Gregor      if (TNK == TNK_Non_template && Id.DestructorName == 0) {
968124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        Diag(NameLoc, diag::err_destructor_template_id)
969124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          << Name << SS.getRange();
9702d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
9712d1c21414199a7452f122598189363a3922605b1Douglas Gregor      }
9722d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
9733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
974014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
9753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  default:
9773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
9783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
9793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (TNK == TNK_Non_template)
9813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
9823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Parse the enclosed template argument list.
9843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
9853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgList TemplateArgs;
9863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
9873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       &SS, true, LAngleLoc,
9883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgs,
9893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       RAngleLoc))
9903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
9913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
9923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
993e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
994e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
9953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Form a parsed representation of the template-id to be stored in the
9963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // UnqualifiedId.
9973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateIdAnnotation *TemplateId
9983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      = TemplateIdAnnotation::Allocate(TemplateArgs.size());
9993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
10013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->Name = Id.Identifier;
1002014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = OO_None;
10033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
10043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
1005014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Name = 0;
1006014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = Id.OperatorFunctionId.Operator;
1007014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
10083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
10093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Template = Template.getAs<void*>();
10113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Kind = TNK;
10123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->LAngleLoc = LAngleLoc;
10133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->RAngleLoc = RAngleLoc;
1014314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
10153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
1016314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor         Arg != ArgEnd; ++Arg)
10173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Args[Arg] = TemplateArgs[Arg];
10183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setTemplateId(TemplateId);
10203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
10213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
10223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Bundle the template arguments together.
10243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
10253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                     TemplateArgs.size());
10263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Constructor and destructor names.
10283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  Action::TypeResult Type
10293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    = Actions.ActOnTemplateIdType(Template, NameLoc,
10303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  LAngleLoc, TemplateArgsPtr,
10313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  RAngleLoc);
10323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Type.isInvalid())
10333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
10343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
10363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
10373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  else
10383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
10393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
10403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return false;
10413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
10423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1043ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part
1044ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id.
10453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1046ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or
1047ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way.
10483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1049ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
10503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       operator-function-id: [C++ 13.5]
10513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         'operator' operator
10523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1053ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       operator: one of
10543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            new   delete  new[]   delete[]
10553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            +     -    *  /    %  ^    &   |   ~
10563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            !     =    <  >    += -=   *=  /=  %=
10573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
10583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
10593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ()    []
10603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-function-id: [C++ 12.3.2]
10623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator conversion-type-id
10633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-type-id:
10653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         type-specifier-seq conversion-declarator[opt]
10663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-declarator:
10683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ptr-operator conversion-declarator[opt]
10693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode
10703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
10723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
10733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
10743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the
10753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
10763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1077ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
1078ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed.
1079ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1080ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
1081ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1082ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise.
1083ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1084ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        TypeTy *ObjectType,
1085ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        UnqualifiedId &Result) {
1086ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1087ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1088ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Consume the 'operator' keyword.
1089ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation KeywordLoc = ConsumeToken();
1090ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1091ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Determine what kind of operator name we have.
1092ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  unsigned SymbolIdx = 0;
1093ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation SymbolLocations[3];
1094ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  OverloadedOperatorKind Op = OO_None;
1095ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  switch (Tok.getKind()) {
1096ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_new:
1097ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_delete: {
1098ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      bool isNew = Tok.getKind() == tok::kw_new;
1099ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the 'new' or 'delete'.
1100ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();
1101ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::l_square)) {
1102ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the '['.
1103ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SourceLocation LBracketLoc = ConsumeBracket();
1104ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the ']'.
1105ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1106ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                         LBracketLoc);
1107ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (RBracketLoc.isInvalid())
1108ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          return true;
1109ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1110ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SymbolLocations[SymbolIdx++] = LBracketLoc;
1111ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SymbolLocations[SymbolIdx++] = RBracketLoc;
1112ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_Array_New : OO_Array_Delete;
1113ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
1114ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_New : OO_Delete;
1115ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
1116ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1117ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1118ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1119ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1120ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::Token:                                                     \
1121ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
1122ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_##Name;                                                    \
1123ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1124ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1125ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def"
1126ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1127ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_paren: {
1128ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the '('.
1129ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation LParenLoc = ConsumeParen();
1130ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the ')'.
1131ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1132ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                     LParenLoc);
1133ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (RParenLoc.isInvalid())
1134ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1135ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1136ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = LParenLoc;
1137ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = RParenLoc;
1138ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Call;
1139ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1140ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1141ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1142ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_square: {
1143ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the '['.
1144ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation LBracketLoc = ConsumeBracket();
1145ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the ']'.
1146ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1147ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                       LBracketLoc);
1148ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (RBracketLoc.isInvalid())
1149ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1150ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1151ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = LBracketLoc;
1152ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = RBracketLoc;
1153ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Subscript;
1154ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1155ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1156ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1157ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::code_completion: {
1158ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Code completion for the operator name.
1159ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Actions.CodeCompleteOperatorName(CurScope);
1160ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1161ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the operator token.
1162ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      ConsumeToken();
1163ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1164ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Don't try to parse any further.
1165ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return true;
1166ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1167ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1168ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    default:
1169ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1170ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
1171ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1172ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Op != OO_None) {
1173ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // We have parsed an operator-function-id.
1174ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1175ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return false;
1176ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
11770486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
11780486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  // Parse a literal-operator-id.
11790486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //
11800486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //   literal-operator-id: [C++0x 13.5.8]
11810486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //     operator "" identifier
11820486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
11830486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
11840486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    if (Tok.getLength() != 2)
11850486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
11860486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    ConsumeStringToken();
11870486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
11880486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    if (Tok.isNot(tok::identifier)) {
11890486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_expected_ident);
11900486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      return true;
11910486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    }
11920486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
11930486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    IdentifierInfo *II = Tok.getIdentifierInfo();
11940486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
11953e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    return false;
11960486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  }
1197ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1198ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse a conversion-function-id.
1199ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1200ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-function-id: [C++ 12.3.2]
1201ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     operator conversion-type-id
1202ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1203ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-type-id:
1204ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     type-specifier-seq conversion-declarator[opt]
1205ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1206ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-declarator:
1207ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     ptr-operator conversion-declarator[opt]
1208ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1209ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the type-specifier-seq.
1210ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DeclSpec DS;
1211f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor  if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
1212ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1213ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1214ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the conversion-declarator, which is merely a sequence of
1215ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // ptr-operators.
1216ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Declarator D(DS, Declarator::TypeNameContext);
1217ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1218ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1219ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Finish up the type.
1220ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1221ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Ty.isInvalid())
1222ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1223ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1224ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Note that this is a conversion-function-id.
1225ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1226ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                 D.getSourceRange().getEnd());
1227ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return false;
1228ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
1229ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1230ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1231ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity.
1232ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1233ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
1234ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       unqualified-id: [C++ expr.prim.general]
1235ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         identifier
1236ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         operator-function-id
1237ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         conversion-function-id
1238ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO]
1239ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         ~ class-name
1240ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         template-id
1241ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1242ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode
1243ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1244ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
1245ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1246ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1247ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the
1248ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier.
1249ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
12503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name.
12513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
12523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name.
12533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
125446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
125546df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
125646df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
12573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
12583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
12593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise.
12603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
12613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowDestructorName,
12623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowConstructorName,
12632d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                TypeTy *ObjectType,
12643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                UnqualifiedId &Result) {
12653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
12663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   identifier
12673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (when it hasn't already been annotated)
12683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::identifier)) {
12693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the identifier.
12703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *Id = Tok.getIdentifierInfo();
12713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation IdLoc = ConsumeToken();
12723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1273b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    if (!getLang().CPlusPlus) {
1274b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // If we're not in C++, only identifiers matter. Record the
1275b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // identifier and return.
1276b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      Result.setIdentifier(Id, IdLoc);
1277b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      return false;
1278b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    }
1279b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor
12803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (AllowConstructorName &&
12813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor        Actions.isCurrentClassName(*Id, CurScope, &SS)) {
12823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed a constructor name.
12833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
12843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                                    &SS, false),
12853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                IdLoc, IdLoc);
12863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
12873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an identifier.
12883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setIdentifier(Id, IdLoc);
12893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
12903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // If the next token is a '<', we may have a template.
12923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.is(tok::less))
12933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
12942d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                          ObjectType, Result);
12953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
12973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
12983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
13003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (already parsed and annotated)
13013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::annot_template_id)) {
13020efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    TemplateIdAnnotation *TemplateId
13030efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
13040efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
13050efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    // If the template-name names the current class, then this is a constructor
13060efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    if (AllowConstructorName && TemplateId->Name &&
13070efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
13080efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      if (SS.isSet()) {
13090efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // C++ [class.qual]p2 specifies that a qualified template-name
13100efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // is taken as the constructor name where a constructor can be
13110efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // declared. Thus, the template arguments are extraneous, so
13120efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // complain about them and remove them entirely.
13130efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Diag(TemplateId->TemplateNameLoc,
13140efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor             diag::err_out_of_line_constructor_template_id)
13150efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor          << TemplateId->Name
13160efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor          << CodeModificationHint::CreateRemoval(
13170efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                    SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
13180efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
13190efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                                  TemplateId->TemplateNameLoc,
13200efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                                      CurScope,
13210efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                                      &SS, false),
13220efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->TemplateNameLoc,
13230efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->RAngleLoc);
13240efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        TemplateId->Destroy();
13250efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        ConsumeToken();
13260efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        return false;
13270efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      }
13280efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
13290efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      Result.setConstructorTemplateId(TemplateId);
13300efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      ConsumeToken();
13310efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      return false;
13320efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    }
13330efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
13343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // We have already parsed a template-id; consume the annotation token as
13353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // our unqualified-id.
13360efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    Result.setTemplateId(TemplateId);
13373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ConsumeToken();
13383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
13393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
13403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
13423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   operator-function-id
13433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   conversion-function-id
13443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::kw_operator)) {
1345ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
13463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
13473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1348e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // If we have an operator-function-id or a literal-operator-id and the next
1349e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // token is a '<', we may have a
1350ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //
1351ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //   template-id:
1352ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //     operator-function-id < template-argument-list[opt] >
1353e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1354e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt         Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
1355ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Tok.is(tok::less))
1356ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1357ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                          EnteringContext, ObjectType,
1358ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                          Result);
13593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
13613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
13623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1363b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor  if (getLang().CPlusPlus &&
1364b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
13653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // C++ [expr.unary.op]p10:
13663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   There is an ambiguity in the unary-expression ~X(), where X is a
13673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   class-name. The ambiguity is resolved in favor of treating ~ as a
13683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //    unary complement rather than treating ~X as referring to a destructor.
13693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the '~'.
13713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation TildeLoc = ConsumeToken();
13723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name.
13743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.isNot(tok::identifier)) {
1375124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      Diag(Tok, diag::err_destructor_tilde_identifier);
13763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
13773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
13783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name (or template-name in a simple-template-id).
13803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
13813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation ClassNameLoc = ConsumeToken();
13823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13832d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (Tok.is(tok::less)) {
13842d1c21414199a7452f122598189363a3922605b1Douglas Gregor      Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
13852d1c21414199a7452f122598189363a3922605b1Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
13862d1c21414199a7452f122598189363a3922605b1Douglas Gregor                                          EnteringContext, ObjectType, Result);
13872d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
13882d1c21414199a7452f122598189363a3922605b1Douglas Gregor
13893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a destructor name.
1390124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    Action::TypeTy *Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1391124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor                                                   ClassNameLoc, CurScope,
1392124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor                                                   SS, ObjectType,
1393124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor                                                   EnteringContext);
1394124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (!Ty)
13953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
1396124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
13973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
13983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
13993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
14003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
14012d1c21414199a7452f122598189363a3922605b1Douglas Gregor  Diag(Tok, diag::err_expected_unqualified_id)
14022d1c21414199a7452f122598189363a3922605b1Douglas Gregor    << getLang().CPlusPlus;
14033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return true;
14043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
14053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
14064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
14074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors.
14081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
140959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has
141059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
141159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location.  Otherwise, "Start" is the location of the 'new' token.
14124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
14134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
14144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] new-type-id
14154c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
14164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
14174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
14184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
14194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
14204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
14214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1422cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-type-id:
1423cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   type-specifier-seq new-declarator[opt]
1424cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
1425cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-declarator:
1426cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   ptr-operator new-declarator[opt]
1427cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   direct-new-declarator
1428cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
14294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-initializer:
14304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list[opt] ')'
14314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// [C++0x]           braced-init-list                                   [TODO]
14324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
143359232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::OwningExprResult
143459232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
143559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_new) && "expected 'new' token");
143659232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken();   // Consume 'new'
14374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
14384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
14394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // second form of new-expression. It can't be a new-type-id.
14404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1441a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector PlacementArgs(Actions);
14424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation PlacementLParen, PlacementRParen;
14434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
14444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ParenTypeId;
1445cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  DeclSpec DS;
1446cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
14474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
14484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // If it turns out to be a placement, we change the type location.
14494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementLParen = ConsumeParen();
1450cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1451cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
145220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1453cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
14544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
14554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
1456cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementRParen.isInvalid()) {
1457cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
145820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1459cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
14604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1461cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementArgs.empty()) {
14624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Reset the placement locations. There was no placement.
14634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      PlacementLParen = PlacementRParen = SourceLocation();
14644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      ParenTypeId = true;
14654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    } else {
14664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // We still need the type.
14674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      if (Tok.is(tok::l_paren)) {
1468cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SourceLocation LParen = ConsumeParen();
1469cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseSpecifierQualifierList(DS);
1470ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1471cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseDeclarator(DeclaratorInfo);
1472cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        MatchRHSPunctuation(tok::r_paren, LParen);
14734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl        ParenTypeId = true;
14744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      } else {
1475cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        if (ParseCXXTypeSpecifierSeq(DS))
1476cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          DeclaratorInfo.setInvalidType(true);
1477ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        else {
1478ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1479cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          ParseDeclaratorInternal(DeclaratorInfo,
1480cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                                  &Parser::ParseDirectNewDeclarator);
1481ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        }
14824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl        ParenTypeId = false;
14834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      }
14844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
14854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  } else {
1486cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // A new-type-id is a simplified type-id, where essentially the
1487cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // direct-declarator is replaced by a direct-new-declarator.
1488cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseCXXTypeSpecifierSeq(DS))
1489cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      DeclaratorInfo.setInvalidType(true);
1490ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    else {
1491ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1492cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      ParseDeclaratorInternal(DeclaratorInfo,
1493cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                              &Parser::ParseDirectNewDeclarator);
1494ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    }
14954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ParenTypeId = false;
14964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
1497eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (DeclaratorInfo.isInvalidType()) {
1498cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
149920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
1500cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
15014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1502a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector ConstructorArgs(Actions);
15034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation ConstructorLParen, ConstructorRParen;
15044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
15054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
15064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorLParen = ConsumeParen();
15074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (Tok.isNot(tok::r_paren)) {
15084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      CommaLocsTy CommaLocs;
1509cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1510cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
151120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl        return ExprError();
1512cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      }
15134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
15144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
1515cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ConstructorRParen.isInvalid()) {
1516cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
151720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1518cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
15194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
15204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1521f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1522f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(PlacementArgs), PlacementRParen,
1523f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             ParenTypeId, DeclaratorInfo, ConstructorLParen,
1524f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(ConstructorArgs), ConstructorRParen);
15254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
15264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
15274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
15284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal.
15294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
15304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        direct-new-declarator:
15314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '[' expression ']'
15324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   direct-new-declarator '[' constant-expression ']'
15334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
153459232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) {
15354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Parse the array dimensions.
15364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool first = true;
15374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  while (Tok.is(tok::l_square)) {
15384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LLoc = ConsumeBracket();
15392f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl    OwningExprResult Size(first ? ParseExpression()
15402f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl                                : ParseConstantExpression());
15410e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Size.isInvalid()) {
15424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Recover
15434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      SkipUntil(tok::r_square);
15444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
15454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
15464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    first = false;
15474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1548ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
15494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
15507e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            Size.release(), LLoc, RLoc),
1551ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl                  RLoc);
15524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1553ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (RLoc.isInvalid())
15544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
15554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
15564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
15574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
15584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
15594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator.
15604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
15614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
15624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
15634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
15644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
15654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
15664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
15674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1568cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redlbool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
156959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner                                         Declarator &D) {
15704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // The '(' was already consumed.
15714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (isTypeIdInParens()) {
1572cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseSpecifierQualifierList(D.getMutableDeclSpec());
1573ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetSourceRange(D.getDeclSpec().getSourceRange());
1574cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseDeclarator(D);
1575eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    return D.isInvalidType();
15764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
15774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
15784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // It's not a type, it has to be an expression list.
15794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Discard the comma locations - ActOnCXXNew has enough parameters.
15804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  CommaLocsTy CommaLocs;
15814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return ParseExpressionList(PlacementArgs, CommaLocs);
15824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
15834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
15844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
15854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new.
15864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
158759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional
158859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
158959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location.  Otherwise, "Start" is the location of the
159059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token.
159159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner///
15924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        delete-expression:
15934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' cast-expression
15944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' '[' ']' cast-expression
159559232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::OwningExprResult
159659232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
159759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
159859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken(); // Consume 'delete'
15994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
16004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Array delete?
16014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ArrayDelete = false;
16024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_square)) {
16034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ArrayDelete = true;
16044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LHS = ConsumeBracket();
16054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
16064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (RHS.isInvalid())
160720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
16084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
16094c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
16102f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Operand(ParseCastExpression(false));
16110e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Operand.isInvalid())
161220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return move(Operand);
16134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1614f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
16154c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
161664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
16171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
161864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch(kind) {
161964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  default: assert(false && "Not a known unary type trait.");
162064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign;
162164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_copy:        return UTT_HasNothrowCopy;
162264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
162364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign;
162464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_copy:        return UTT_HasTrivialCopy;
162564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
162664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor;
162764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor;
162864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_abstract:             return UTT_IsAbstract;
162964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_class:                return UTT_IsClass;
163064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_empty:                return UTT_IsEmpty;
163164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_enum:                 return UTT_IsEnum;
163264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_pod:                  return UTT_IsPOD;
163364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_polymorphic:          return UTT_IsPolymorphic;
163464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_union:                return UTT_IsUnion;
1635ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl  case tok::kw___is_literal:              return UTT_IsLiteral;
163664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
163764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
163864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
163964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
164064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits
164164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates.
164264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
164364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///       primary-expression:
164464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU]             unary-type-trait '(' type-id ')'
164564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
16461eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParser::OwningExprResult Parser::ParseUnaryTypeTrait() {
164764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
164864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation Loc = ConsumeToken();
164964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
165064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation LParen = Tok.getLocation();
165164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
165264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return ExprError();
165364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
165464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
165564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // there will be cryptic errors about mismatched parentheses and missing
165664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // specifiers.
1657809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
165864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
165964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
166064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
1661809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
1662809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
1663809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
1664809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
166564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
1666f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1667f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1668f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1669f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens.
1670f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::OwningExprResult
1671f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1672f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         TypeTy *&CastTy,
1673f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation LParenLoc,
1674f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation &RParenLoc) {
1675f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(getLang().CPlusPlus && "Should only be called for C++!");
1676f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1677f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(isTypeIdInParens() && "Not a type-id!");
1678f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1679f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  OwningExprResult Result(Actions, true);
1680f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  CastTy = 0;
1681f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1682f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // We need to disambiguate a very ugly part of the C++ syntax:
1683f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1684f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())x;  - type-id
1685f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())*x; - type-id
1686f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())/x; - expression
1687f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T());   - expression
1688f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1689f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The bad news is that we cannot use the specialized tentative parser, since
1690f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // it can only verify that the thing inside the parens can be parsed as
1691f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // type-id, it is not useful for determining the context past the parens.
1692f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
1693f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The good news is that the parser can disambiguate this part without
1694a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis  // making any unnecessary Action calls.
1695f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  //
1696f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // It uses a scheme similar to parsing inline methods. The parenthesized
1697f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // tokens are cached, the context that follows is determined (possibly by
1698f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parsing a cast-expression), and then we re-introduce the cached tokens
1699f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // into the token stream and parse them appropriately.
1700f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
17011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParenParseOption ParseAs;
1702f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  CachedTokens Toks;
1703f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1704f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Store the tokens of the parentheses. We will parse them after we determine
1705f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // the context that follows them.
1706f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1707f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We didn't find the ')' we expected.
1708f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
1709f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
1710f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
1711f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1712f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::l_brace)) {
1713f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = CompoundLiteral;
1714f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  } else {
1715f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    bool NotCastExpr;
1716b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1717b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1718b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      NotCastExpr = true;
1719b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    } else {
1720b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // Try parsing the cast-expression that may follow.
1721b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // If it is not a cast-expression, NotCastExpr will be true and no token
1722b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // will be consumed.
1723b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      Result = ParseCastExpression(false/*isUnaryExpression*/,
1724b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman                                   false/*isAddressofOperand*/,
17252ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                                   NotCastExpr, false);
1726b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    }
1727f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1728f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // If we parsed a cast-expression, it's really a type-id, otherwise it's
1729f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // an expression.
1730f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
1731f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
1732f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
17331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The current token should go after the cached tokens.
1734f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Toks.push_back(Tok);
1735f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Re-enter the stored parenthesized tokens into the token stream, so we may
1736f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parse them now.
1737f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  PP.EnterTokenStream(Toks.data(), Toks.size(),
1738f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1739f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Drop the current token and bring the first cached one. It's the same token
1740f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // as when we entered this function.
1741f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  ConsumeAnyToken();
1742f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1743f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (ParseAs >= CompoundLiteral) {
1744f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    TypeResult Ty = ParseTypeName();
1745f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1746f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Match the ')'.
1747f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Tok.is(tok::r_paren))
1748f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      RParenLoc = ConsumeParen();
1749f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    else
1750f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      MatchRHSPunctuation(tok::r_paren, LParenLoc);
1751f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1752f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (ParseAs == CompoundLiteral) {
1753f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      ExprType = CompoundLiteral;
1754f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1755f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    }
17561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1757f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1758f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    assert(ParseAs == CastExpr);
1759f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1760f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Ty.isInvalid())
1761f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ExprError();
1762f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1763f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    CastTy = Ty.get();
1764f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
1765f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Result is what ParseCastExpression returned earlier.
1766f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    if (!Result.isInvalid())
17671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
17682ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                                     move(Result));
1769f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return move(Result);
1770f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
17711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1772f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Not a compound literal, and not followed by a cast-expression.
1773f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  assert(ParseAs == SimpleExpr);
1774f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1775f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  ExprType = SimpleExpr;
1776f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Result = ParseExpression();
1777f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (!Result.isInvalid() && Tok.is(tok::r_paren))
1778f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1779f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1780f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // Match the ')'.
1781f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Result.isInvalid()) {
1782f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    SkipUntil(tok::r_paren);
1783f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
1784f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
17851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1786f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::r_paren))
1787f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    RParenLoc = ConsumeParen();
1788f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  else
1789f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
1790f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
1791f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  return move(Result);
1792f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis}
1793