ParseExprCXX.cpp revision 25a767651d14db87aa03dd5fe3e011d877dd4100
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements the Expression parsing implementation for C++.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h"
16bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor#include "RAIIObjectsForParser.h"
1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
1819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h"
193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h"
203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic int SelectDigraphErrorMessage(tok::TokenKind Kind) {
24ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  switch (Kind) {
25ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_template:         return 0;
26ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_const_cast:       return 1;
27ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_dynamic_cast:     return 2;
28ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_reinterpret_cast: return 3;
29ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_static_cast:      return 4;
30ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    default:
31ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      assert(0 && "Unknown type for digraph error message.");
32ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      return -1;
33ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  }
34ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
35ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
36ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith// Are the two tokens adjacent in the same source file?
37ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
38ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceManager &SM = PP.getSourceManager();
39ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
40ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceLocation FirstEnd = FirstLoc.getFileLocWithOffset(First.getLength());
41ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  return FirstEnd == SM.getSpellingLoc(Second.getLocation());
42ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
43ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
44ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith// Suggest fixit for "<::" after a cast.
45ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
46ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                       Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
47ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Pull '<:' and ':' off token stream.
48ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (!AtDigraph)
49ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    PP.Lex(DigraphToken);
50ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  PP.Lex(ColonToken);
51ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
52ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceRange Range;
53ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Range.setBegin(DigraphToken.getLocation());
54ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Range.setEnd(ColonToken.getLocation());
55ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
56ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      << SelectDigraphErrorMessage(Kind)
57ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      << FixItHint::CreateReplacement(Range, "< ::");
58ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
59ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Update token information to reflect their change in token type.
60ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setKind(tok::coloncolon);
61ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setLocation(ColonToken.getLocation().getFileLocWithOffset(-1));
62ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setLength(2);
63ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  DigraphToken.setKind(tok::less);
64ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  DigraphToken.setLength(1);
65ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
66ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Push new tokens back to token stream.
67ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  PP.EnterToken(ColonToken);
68ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (!AtDigraph)
69ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    PP.EnterToken(DigraphToken);
70ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
71ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present.
732dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
742dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or
762dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream.
77eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
78eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'[opt] nested-name-specifier
79eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'
80eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
81eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       nested-name-specifier:
82eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         type-name '::'
83eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         namespace-name '::'
84eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         nested-name-specifier identifier '::'
852dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///         nested-name-specifier 'template'[opt] simple-template-id '::'
862dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
872dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed
892dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty)
902dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following
922dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the
932dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed.
94eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
952dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of
962dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it.
972dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
98d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param MayBePseudoDestructor When non-NULL, points to a flag that
99d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// indicates whether this nested-name-specifier may be part of a
100d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// pseudo-destructor name. In this case, the flag will be set false
101d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we don't actually end up parsing a destructor name. Moreorover,
102d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we do end up determining that we are parsing a destructor name,
103d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// the last component of the nested-name-specifier is not parsed as
104d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// part of the scope specifier.
105d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
106b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor/// member access expression, e.g., the \p T:: in \p p->T::m.
107b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor///
1089ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// \returns true if there was an error parsing a scope specifier
109495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
110b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType ObjectType,
111b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            bool EnteringContext,
1124147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                            bool *MayBePseudoDestructor,
1134147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                            bool IsTypename) {
1144bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  assert(getLang().CPlusPlus &&
1157452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Call sites of this function should be guarded by checking for C++");
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
117eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::annot_cxxscope)) {
118c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
119c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 Tok.getAnnotationRange(),
120c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 SS);
121eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    ConsumeToken();
1229ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
123eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
124e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner
12539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  bool HasScopeSpecifier = false;
12639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
1275b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner  if (Tok.is(tok::coloncolon)) {
1285b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    // ::new and ::delete aren't nested-name-specifiers.
1295b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    tok::TokenKind NextKind = NextToken().getKind();
1305b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
1315b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner      return false;
1321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13355a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    // '::' - Global scope qualifier.
1342e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
1352e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
1362e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
13739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    HasScopeSpecifier = true;
138eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
139eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
140d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  bool CheckForDestructor = false;
141d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (MayBePseudoDestructor && *MayBePseudoDestructor) {
142d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CheckForDestructor = true;
143d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = false;
144d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
145d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
14639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  while (true) {
1472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (HasScopeSpecifier) {
1482dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p5:
1492dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the qualified-id has the form
1503b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
1512dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //       ::class-name-or-namespace-name::...
1523b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
1532dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the class-name-or-namespace-name is looked up in global scope as a
1542dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name.
1552dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
1562dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // To implement this, we clear out the object type as soon as we've
1572dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // seen a leading '::' or part of a nested-name-specifier.
158b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      ObjectType = ParsedType();
15981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
16081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      if (Tok.is(tok::code_completion)) {
16181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // Code completion for a nested-name-specifier, where the code
16281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // code completion token follows the '::'.
16323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
164b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        SourceLocation ccLoc = ConsumeCodeCompletionToken();
165b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // Include code completion token into the range of the scope otherwise
166b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // when we try to annotate the scope tokens the dangling code completion
167b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // token will cause assertion in
168b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // Preprocessor::AnnotatePreviousCachedTokens.
169b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        SS.setEndLoc(ccLoc);
17081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      }
1712dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
1721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier:
17477cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    //   nested-name-specifier 'template'[opt] simple-template-id '::'
17577cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner
17677cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // Parse the optional 'template' keyword, then make sure we have
17777cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // 'identifier <' after it.
17877cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    if (Tok.is(tok::kw_template)) {
1792dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // If we don't have a scope specifier or an object type, this isn't a
180eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // nested-name-specifier, since they aren't allowed to start with
181eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // 'template'.
1822dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      if (!HasScopeSpecifier && !ObjectType)
183eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
184eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman
1857bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TentativeParsingAction TPA(*this);
18677cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      SourceLocation TemplateKWLoc = ConsumeToken();
187ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
188ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      UnqualifiedId TemplateName;
189ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::identifier)) {
190ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
1917bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
192ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
193ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else if (Tok.is(tok::kw_operator)) {
194ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
1957bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor                                       TemplateName)) {
1967bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
197ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
1987bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        }
199ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
200e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt        if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
201e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt            TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
202ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          Diag(TemplateName.getSourceRange().getBegin(),
203ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor               diag::err_id_after_template_in_nested_name_spec)
204ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor            << TemplateName.getSourceRange();
2057bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
206ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
207ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        }
208ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
2097bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
21077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        break;
21177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      }
2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2137bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // If the next token is not '<', we have a qualified-id that refers
2147bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // to a template name, such as T::template apply, but is not a
2157bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // template-id.
2167bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      if (Tok.isNot(tok::less)) {
2177bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
2187bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        break;
2197bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      }
2207bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor
2217bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // Commit to parsing the template-id.
2227bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TPA.Commit();
223d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      TemplateTy Template;
22423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
225d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                TemplateKWLoc,
226d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                    SS,
227d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                  TemplateName,
228d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                    ObjectType,
229d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                EnteringContext,
230d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                                    Template)) {
231059101f922de6eb765601459925f4c8914420b23Douglas Gregor        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
232d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                    TemplateKWLoc, false))
233d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          return true;
234d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      } else
2359ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
2361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23777cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      continue;
23877cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    }
2391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
2411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We have
24239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
24339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //   simple-template-id '::'
24439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
24539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // So we need to check whether the simple-template-id is of the
246c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // right kind (it should name a type or be dependent), and then
247c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // convert it into a type within the nested-name-specifier.
24825a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
249d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
250d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
2519ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
252d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
253d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
2546cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      // Consume the template-id token.
2556cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      ConsumeToken();
2566cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
2576cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
2586cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      SourceLocation CCLoc = ConsumeToken();
2591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2606cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      if (!HasScopeSpecifier)
2616cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        HasScopeSpecifier = true;
2626cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
2636cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      ASTTemplateArgsPtr TemplateArgsPtr(Actions,
2646cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                         TemplateId->getTemplateArgs(),
2656cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                         TemplateId->NumArgs);
2666cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
2676cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
2686cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              /*FIXME:*/SourceLocation(),
2696cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              SS,
2706cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->Template,
2716cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->TemplateNameLoc,
2726cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->LAngleLoc,
2736cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateArgsPtr,
2746cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->RAngleLoc,
2756cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              CCLoc,
2766cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              EnteringContext)) {
2776cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        SourceLocation StartLoc
2786cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor          = SS.getBeginLoc().isValid()? SS.getBeginLoc()
2796cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                      : TemplateId->TemplateNameLoc;
2806cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        SS.SetInvalid(SourceRange(StartLoc, CCLoc));
28167b9e831943300ce54e564e601971828ce4def15Chris Lattner      }
282eccce7e246a17e12a2afd6eabb9ac7c8d582db4eArgyrios Kyrtzidis
2836cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      continue;
28439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
28539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
2865c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2875c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // The rest of the nested-name-specifier possibilities start with
2885c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // tok::identifier.
2895c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Tok.isNot(tok::identifier))
2905c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      break;
2915c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2925c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    IdentifierInfo &II = *Tok.getIdentifierInfo();
2935c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
2945c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
2955c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '::'
2965c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   namespace-name '::'
2975c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   nested-name-specifier identifier '::'
2985c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    Token Next = NextToken();
29946646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
30046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // If we get foo:bar, this is almost certainly a typo for foo::bar.  Recover
30146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // and emit a fixit hint for it.
302b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor    if (Next.is(tok::colon) && !ColonIsSacred) {
3032e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
3042e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Tok.getLocation(),
3052e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Next.getLocation(), ObjectType,
306b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            EnteringContext) &&
307b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // If the token after the colon isn't an identifier, it's still an
308b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // error, but they probably meant something else strange so don't
309b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // recover like this.
310b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          PP.LookAhead(1).is(tok::identifier)) {
311b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
312849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateReplacement(Next.getLocation(), "::");
313b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor
314b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        // Recover as if the user wrote '::'.
315b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Next.setKind(tok::coloncolon);
316b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor      }
31746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    }
31846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
3195c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::coloncolon)) {
32077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
32123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
32277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                                II, ObjectType)) {
323d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
3249ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
325d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
326d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
3275c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // We have an identifier followed by a '::'. Lookup this name
3285c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // as the name in a nested-name-specifier.
3295c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation IdLoc = ConsumeToken();
33046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner      assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
33146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner             "NextToken() not working properly!");
3325c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation CCLoc = ConsumeToken();
3331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3342e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      HasScopeSpecifier = true;
3352e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
3362e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                              ObjectType, EnteringContext, SS))
3372e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        SS.SetInvalid(SourceRange(IdLoc, CCLoc));
3382e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
3395c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      continue;
3405c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
342ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    // Check for '<::' which should be '< ::' instead of '[:' when following
343ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    // a template name.
344ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    if (Next.is(tok::l_square) && Next.getLength() == 2) {
345ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      Token SecondToken = GetLookAheadToken(2);
346ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      if (SecondToken.is(tok::colon) &&
347ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith          AreTokensAdjacent(PP, Next, SecondToken)) {
348ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith        TemplateTy Template;
349ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith        UnqualifiedId TemplateName;
350ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith        TemplateName.setIdentifier(&II, Tok.getLocation());
351ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith        bool MemberOfUnknownSpecialization;
352ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith        if (Actions.isTemplateName(getCurScope(), SS,
353ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                                   /*hasTemplateKeyword=*/false,
354ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                                   TemplateName,
355ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                                   ObjectType,
356ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                                   EnteringContext,
357ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                                   Template,
358ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                                   MemberOfUnknownSpecialization)) {
359ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith          FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
360ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                     /*AtDigraph*/false);
361ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith        }
362ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      }
363ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    }
364ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
3655c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
3665c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '<'
3675c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::less)) {
3685c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      TemplateTy Template;
369014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
370014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(&II, Tok.getLocation());
3711fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
37223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
3737c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                              /*hasTemplateKeyword=*/false,
374014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                                        TemplateName,
3752dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                                        ObjectType,
376495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        EnteringContext,
3771fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                                        Template,
3781fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                              MemberOfUnknownSpecialization)) {
3795c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // We have found a template name, so annotate this this token
3805c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // with a template-id annotation. We do not permit the
3815c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // template-id to be translated into a type annotation,
3825c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // because some clients (e.g., the parsing of class template
3835c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // specializations) still want to see the original template-id
3845c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // token.
385ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
386059101f922de6eb765601459925f4c8914420b23Douglas Gregor        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
387ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                    SourceLocation(), false))
3889ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
3895c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
390d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      }
391d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
392d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
3934147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet          (IsTypename || IsTemplateArgumentList(1))) {
394d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // We have something like t::getAs<T>, where getAs is a
395d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // member of an unknown specialization. However, this will only
396d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
397d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
3984147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        unsigned DiagID = diag::err_missing_dependent_template_keyword;
3994147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        if (getLang().Microsoft)
400cf320c6388c90f1938c264e87d77a0e43946e2c3Francois Pichet          DiagID = diag::warn_missing_dependent_template_keyword;
4014147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet
4024147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        Diag(Tok.getLocation(), DiagID)
403d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << II.getName()
404d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
405d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
406d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TemplateNameKind TNK
40723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor              = Actions.ActOnDependentTemplateName(getCurScope(),
408d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   Tok.getLocation(), SS,
409d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   TemplateName, ObjectType,
410d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   EnteringContext, Template)) {
411d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          // Consume the identifier.
412d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          ConsumeToken();
413059101f922de6eb765601459925f4c8914420b23Douglas Gregor          if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
414d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                      SourceLocation(), false))
415d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor            return true;
416d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        }
417d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        else
418d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          return true;
419d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor
420d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        continue;
4215c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
4225c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
4235c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
42439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // We don't have any tokens that form the beginning of a
42539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier, so we're done.
42639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    break;
42739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
429d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Even if we didn't see any pieces of a nested-name-specifier, we
430d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // still check whether there is a tilde in this position, which
431d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // indicates a potential pseudo-destructor.
432d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (CheckForDestructor && Tok.is(tok::tilde))
433d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = true;
434d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
4359ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
436eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
437eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
438eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression.
439eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
440eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       id-expression:
441eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         unqualified-id
442eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         qualified-id
443eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
444eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
445eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
446eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' identifier
447eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' operator-function-id
448edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor///         '::' template-id
449eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
450eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not
451eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect:
452eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
453eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' conversion-function-id
454eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' '~' class-name
455eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
456eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics:
457eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
458eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {};
459eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {}
460eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() {
461eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: A :: ~ C(); // Some Sema error about using destructor with a
462eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///                  // namespace.
463eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: ~ C(); // Some Parser error like 'unexpected ~'.
464eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// }
465eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
466eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like:
467eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
468eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
469eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
470eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' unqualified-id
471eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
472eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the
473eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope.
474eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
475ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a
476ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts,
477ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may
478ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear.
479ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl///
48060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
481eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  // qualified-id:
482eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
483eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::' unqualified-id
484eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //
485eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
486b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
48702a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
48802a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  UnqualifiedId Name;
48902a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  if (ParseUnqualifiedId(SS,
49002a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*EnteringContext=*/false,
49102a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*AllowDestructorName=*/false,
49202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         /*AllowConstructorName=*/false,
493b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                         /*ObjectType=*/ ParsedType(),
49402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         Name))
49502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor    return ExprError();
496b681b61fea36618778b8030360e90e3f4641233bJohn McCall
497b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // This is only the direct operand of an & operator if it is not
498b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // followed by a postfix-expression suffix.
4999c72c6088d591ace8503b842d39448c2040f3033John McCall  if (isAddressOfOperand && isPostfixExpressionSuffixStart())
5009c72c6088d591ace8503b842d39448c2040f3033John McCall    isAddressOfOperand = false;
50102a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
50223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
50302a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                                   isAddressOfOperand);
50402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor
505eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
506eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
5075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type.
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       postfix-expression: [C++ 5.2p1]
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'dynamic_cast' '<' type-name '>' '(' expression ')'
5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'static_cast' '<' type-name '>' '(' expression ')'
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'const_cast' '<' type-name '>' '(' expression ')'
5155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
51660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXCasts() {
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
5185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CastName = 0;     // For error messages
5195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Kind) {
5215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown C++ cast!"); abort();
5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_const_cast:       CastName = "const_cast";       break;
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_static_cast:      CastName = "static_cast";      break;
5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation OpLoc = ConsumeToken();
5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LAngleBracketLoc = Tok.getLocation();
5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
531ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Check for "<::" which is parsed as "[:".  If found, fix token stream,
532ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // diagnose error, suggest fix, and recover parsing.
533ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Token Next = NextToken();
534ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
535ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      AreTokensAdjacent(PP, Tok, Next))
536ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
537ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
53920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
541809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult CastTy = ParseTypeName();
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation RAngleBracketLoc = Tok.getLocation();
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5441ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
54520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
5485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
54921e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
55021e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    return ExprError();
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
55260d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = ParseExpression();
5531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55421e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  // Match the ')'.
55527591ff4fc64600fd67c5d81899e3efe5ef41a80Douglas Gregor  RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
557809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (!Result.isInvalid() && !CastTy.isInvalid())
55849badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
559f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                       LAngleBracketLoc, CastTy.get(),
560809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                       RAngleBracketLoc,
5619ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                       LParenLoc, Result.take(), RParenLoc);
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
56320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
566c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression.
567c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
568c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///       postfix-expression: [C++ 5.2p1]
569c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' expression ')'
570c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' type-id ')'
571c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
57260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXTypeid() {
573c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
574c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
575c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation OpLoc = ConsumeToken();
576c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation LParenLoc = Tok.getLocation();
577c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation RParenLoc;
578c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
579c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  // typeid expressions are always parenthesized.
580c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
581c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      "typeid"))
58220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
583c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
58460d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result;
585c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
586c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (isTypeIdInParens()) {
587809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult Ty = ParseTypeName();
588c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
589c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
5904eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
591c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
5924eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    if (Ty.isInvalid() || RParenLoc.isInvalid())
59320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
594c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
595c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
596b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                    Ty.get().getAsOpaquePtr(), RParenLoc);
597c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  } else {
598e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // C++0x [expr.typeid]p3:
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   When typeid is applied to an expression other than an lvalue of a
6001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   polymorphic class type [...] The expression is an unevaluated
601e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //   operand (Clause 5).
602e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Note that we can't tell whether the expression is an lvalue of a
604e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // polymorphic class type until after we've parsed the expression, so
605ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // we the expression is potentially potentially evaluated.
606ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(Actions,
607f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                       Sema::PotentiallyPotentiallyEvaluated);
608c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = ParseExpression();
609c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
610c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
6110e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Result.isInvalid())
612c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      SkipUntil(tok::r_paren);
613c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    else {
6144eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor      RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
6154eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor      if (RParenLoc.isInvalid())
6164eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor        return ExprError();
617fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor
618fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor      // If we are a foo<int> that identifies a single function, resolve it now...
619fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor      Expr* e = Result.get();
620fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor      if (e->getType() == Actions.Context.OverloadTy) {
621fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor        ExprResult er =
622fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor              Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
623fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor        if (er.isUsable())
624fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor          Result = er.release();
625fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor      }
626c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
627effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl                                      Result.release(), RParenLoc);
628c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    }
629c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  }
630c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
63120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
632c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
633c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
63401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
63501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
63601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' expression ')'
63701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' type-id ')'
63801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
63901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetExprResult Parser::ParseCXXUuidof() {
64001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
64101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
64201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  SourceLocation OpLoc = ConsumeToken();
64301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  SourceLocation LParenLoc = Tok.getLocation();
64401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  SourceLocation RParenLoc;
64501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
64601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  // __uuidof expressions are always parenthesized.
64701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
64801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      "__uuidof"))
64901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    return ExprError();
65001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
65101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  ExprResult Result;
65201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
65301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  if (isTypeIdInParens()) {
65401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    TypeResult Ty = ParseTypeName();
65501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
65601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
65701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
65801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
65901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Ty.isInvalid())
66001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      return ExprError();
66101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
66201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
66301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                    Ty.get().getAsOpaquePtr(), RParenLoc);
66401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  } else {
66501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
66601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    Result = ParseExpression();
66701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
66801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
66901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Result.isInvalid())
67001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      SkipUntil(tok::r_paren);
67101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    else {
67201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
67301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
67401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
67501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                      Result.release(), RParenLoc);
67601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    }
67701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  }
67801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
67901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  return move(Result);
68001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet}
68101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
682d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \brief Parse a C++ pseudo-destructor expression after the base,
683d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// . or -> operator, and nested-name-specifier have already been
684d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// parsed.
685d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
686d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       postfix-expression: [C++ 5.2]
687d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression . pseudo-destructor-name
688d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression -> pseudo-destructor-name
689d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
690d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       pseudo-destructor-name:
691d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] type-name :: ~type-name
692d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier template simple-template-id ::
693d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///                 ~type-name
694d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] ~type-name
695d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
69660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
697d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
698d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 tok::TokenKind OpKind,
699d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 CXXScopeSpec &SS,
700b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                 ParsedType ObjectType) {
701d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // We're parsing either a pseudo-destructor-name or a dependent
702d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // member access that has the same form as a
703d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // pseudo-destructor-name. We parse both in the same way and let
704d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // the action model sort them out.
705d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  //
706d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Note that the ::[opt] nested-name-specifier[opt] has already
707d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been parsed, and if there was a simple-template-id, it has
708d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been coalesced into a template-id annotation token.
709d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId FirstTypeName;
710d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation CCLoc;
711d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::identifier)) {
712d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
713d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
714d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
715d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
716d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else if (Tok.is(tok::annot_template_id)) {
717d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setTemplateId(
718d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                              (TemplateIdAnnotation *)Tok.getAnnotationValue());
719d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
720d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
721d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
722d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else {
723d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(0, SourceLocation());
724d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
725d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
726d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the tilde.
727d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
728d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation TildeLoc = ConsumeToken();
729d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (!Tok.is(tok::identifier)) {
730d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    Diag(Tok, diag::err_destructor_tilde_identifier);
731d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
732d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
733d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
734d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the second type.
735d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId SecondTypeName;
736d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  IdentifierInfo *Name = Tok.getIdentifierInfo();
737d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation NameLoc = ConsumeToken();
738d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SecondTypeName.setIdentifier(Name, NameLoc);
739d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
740d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // If there is a '<', the second type name is a template-id. Parse
741d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // it as such.
742d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::less) &&
743d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
7440278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                   SecondTypeName, /*AssumeTemplateName=*/true,
7450278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                   /*TemplateKWLoc*/SourceLocation()))
746d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
747d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
7489ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
7499ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                           OpLoc, OpKind,
750d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           SS, FirstTypeName, CCLoc,
751d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           TildeLoc, SecondTypeName,
752d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           Tok.is(tok::l_paren));
753d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor}
754d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       boolean-literal: [C++ 2.13.5]
7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'true'
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'false'
76060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXBoolLiteral() {
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
762f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
76450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
76550dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression.
76650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///
76750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///       throw-expression: [C++ 15]
76850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///         'throw' assignment-expression[opt]
76960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseThrowExpression() {
77050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  assert(Tok.is(tok::kw_throw) && "Not throw!");
77150dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
77220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl
7732a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // If the current token isn't the start of an assignment-expression,
7742a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // then the expression is not present.  This handles things like:
7752a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  //   "C ? throw : (void)42", which is crazy but legal.
7762a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
7772a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::semi:
7782a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_paren:
7792a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_square:
7802a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_brace:
7812a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::colon:
7822a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::comma:
7839ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return Actions.ActOnCXXThrow(ThrowLoc, 0);
78450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
7852a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  default:
78660d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Expr(ParseAssignmentExpression());
78720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    if (Expr.isInvalid()) return move(Expr);
7889ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
7892a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  }
79050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
7914cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
7924cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer.
7934cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
7944cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
7954cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which
7964cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called.
79760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXThis() {
7984cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  assert(Tok.is(tok::kw_this) && "Not 'this'!");
7994cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  SourceLocation ThisLoc = ConsumeToken();
800f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXThis(ThisLoc);
8014cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
802987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
803987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
804987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)")
805987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)")
806987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()").
807dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// See [C++ 5.2.3].
808987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
809987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       postfix-expression: [C++ 5.2p1]
810dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl///         simple-type-specifier '(' expression-list[opt] ')'
811dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] simple-type-specifier braced-init-list
812dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl///         typename-specifier '(' expression-list[opt] ')'
813dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] typename-specifier braced-init-list
814987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
81560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
81620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
817987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
818b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
819987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
820dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  assert((Tok.is(tok::l_paren) ||
821dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl          (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
822dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl         && "Expected '(' or '{'!");
823bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor
824dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  if (Tok.is(tok::l_brace)) {
825987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
826dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // FIXME: Convert to a proper type construct expression.
827dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    return ParseBraceInitializer();
828987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
829dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  } else {
830dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
831dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
832dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    SourceLocation LParenLoc = ConsumeParen();
833dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
834dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    ExprVector Exprs(Actions);
835dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    CommaLocsTy CommaLocs;
836dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
837dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (Tok.isNot(tok::r_paren)) {
838dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      if (ParseExpressionList(Exprs, CommaLocs)) {
839dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl        SkipUntil(tok::r_paren);
840dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl        return ExprError();
841dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      }
842987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    }
843987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
844dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // Match the ')'.
845dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
846987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
847dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // TypeRep could be null, if it references an invalid typedef.
848dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (!TypeRep)
849dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      return ExprError();
850ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl
851dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
852dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl           "Unexpected number of commas!");
853dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
854dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl                                             RParenLoc);
855dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  }
856987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
857987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
85899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// ParseCXXCondition - if/switch/while condition expression.
85971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
86071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///       condition:
86171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         expression
86271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         type-specifier-seq declarator '=' assignment-expression
86371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
86471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///             '=' assignment-expression
86571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
86699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param ExprResult if the condition was parsed as an expression, the
86799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed expression.
86899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
86999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param DeclResult if the condition was parsed as a declaration, the
87099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed declaration.
87199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
872586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param Loc The location of the start of the statement that requires this
873586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// condition, e.g., the "for" in a for loop.
874586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
875586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param ConvertToBoolean Whether the condition expression should be
876586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// converted to a boolean value.
877586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
87899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \returns true if there was a parsing, false otherwise.
87960d7b3a319d84d688752be3870615ac0f111fb16John McCallbool Parser::ParseCXXCondition(ExprResult &ExprOut,
88060d7b3a319d84d688752be3870615ac0f111fb16John McCall                               Decl *&DeclOut,
881586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               SourceLocation Loc,
882586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               bool ConvertToBoolean) {
88301dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  if (Tok.is(tok::code_completion)) {
884f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
885dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
88601dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  }
88701dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor
88899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!isCXXConditionDeclaration()) {
889586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // Parse the expression.
89060d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprOut = ParseExpression(); // expression
89160d7b3a319d84d688752be3870615ac0f111fb16John McCall    DeclOut = 0;
89260d7b3a319d84d688752be3870615ac0f111fb16John McCall    if (ExprOut.isInvalid())
893586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return true;
894586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
895586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // If required, convert to a boolean value.
896586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    if (ConvertToBoolean)
89760d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprOut
89860d7b3a319d84d688752be3870615ac0f111fb16John McCall        = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
89960d7b3a319d84d688752be3870615ac0f111fb16John McCall    return ExprOut.isInvalid();
90099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
90171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
90271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // type-specifier-seq
9030b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
90471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
90571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
90671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // declarator
90771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
90871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
90971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
91071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // simple-asm-expr[opt]
91171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.is(tok::kw_asm)) {
912ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
91360d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult AsmLabel(ParseSimpleAsm(&Loc));
9140e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (AsmLabel.isInvalid()) {
91571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis      SkipUntil(tok::semi);
91699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return true;
91771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    }
918effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    DeclaratorInfo.setAsmLabel(AsmLabel.release());
919ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.SetRangeEnd(Loc);
92071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  }
92171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
92271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // If attributes are present, parse them.
9237f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseGNUAttributes(DeclaratorInfo);
92471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
92599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  // Type-check the declaration itself.
92660d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
9277f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                        DeclaratorInfo);
92860d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclOut = Dcl.get();
92960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprOut = ExprError();
930a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
93171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // '=' assignment-expression
932a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis  if (isTokenEqualOrMistypedEqualEqual(
933a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis                               diag::err_invalid_equalequal_after_declarator)) {
934dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeToken();
93560d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult AssignExpr(ParseAssignmentExpression());
93699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (!AssignExpr.isInvalid())
93734b41d939a1328f484511c6002ba2456db879a29Richard Smith      Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
93834b41d939a1328f484511c6002ba2456db879a29Richard Smith                                   DS.getTypeSpecType() == DeclSpec::TST_auto);
93999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  } else {
94099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // FIXME: C++0x allows a braced-init-list
94199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    Diag(Tok, diag::err_expected_equal_after_declarator);
94299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
94399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
944586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // FIXME: Build a reference to this declaration? Convert it to bool?
945586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // (This is currently handled by Sema).
946483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
947483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Actions.FinalizeDeclaration(DeclOut);
948586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
94999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return false;
95071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis}
95171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
9526aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brief Determine whether the current token starts a C++
9536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// simple-type-specifier.
9546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::isCXXSimpleTypeSpecifier() const {
9556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  switch (Tok.getKind()) {
9566aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::annot_typename:
9576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_short:
9586aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_long:
959338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet  case tok::kw___int64:
9606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_signed:
9616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_unsigned:
9626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_void:
9636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char:
9646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_int:
9656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_float:
9666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_double:
9676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_wchar_t:
9686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char16_t:
9696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char32_t:
9706aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_bool:
971d9d75e57dfa22366c0379c92beac1db82db34e9aDouglas Gregor  case tok::kw_decltype:
9726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_typeof:
973db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt  case tok::kw___underlying_type:
9746aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
9756aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
9766aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  default:
9776aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    break;
9786aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
9796aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
9806aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
9816aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
9826aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
983987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
984987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of
985987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier.
986987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
987987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       simple-type-specifier:
988eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier[opt] type-name
989987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
990987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         char
991987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         wchar_t
992987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         bool
993987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         short
994987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         int
995987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         long
996987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         signed
997987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         unsigned
998987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         float
999987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         double
1000987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         void
1001987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU]   typeof-specifier
1002987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto               [TODO]
1003987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1004987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       type-name:
1005987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         class-name
1006987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         enum-name
1007987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typedef-name
1008987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1009987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1010987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  DS.SetRangeStart(Tok.getLocation());
1011987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  const char *PrevSpec;
1012fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
1013987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation Loc = Tok.getLocation();
10141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1015987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  switch (Tok.getKind()) {
101655a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::identifier:   // foo::bar
101755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::coloncolon:   // ::foo::bar
101855a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    assert(0 && "Annotation token should already be formed!");
10191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
1020987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    assert(0 && "Not a simple-type-specifier token!");
1021987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    abort();
102255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner
1023987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // type-name
1024b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  case tok::annot_typename: {
10256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (getTypeAnnotation(Tok))
10266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
10276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                         getTypeAnnotation(Tok));
10286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    else
10296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecError();
10309bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
10319bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
10329bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    ConsumeToken();
10339bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
10349bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
10359bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
10369bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C interface.  If we don't have Objective-C or a '<', this is
10379bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // just a normal reference to a typedef name.
10389bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    if (Tok.is(tok::less) && getLang().ObjC1)
10399bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor      ParseObjCProtocolQualifiers(DS);
10409bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
10419bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.Finish(Diags, PP);
10429bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    return;
1043987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
10441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1045987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // builtin types
1046987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_short:
1047fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
1048987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1049987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_long:
1050fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
1051338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet    break;
1052338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet  case tok::kw___int64:
1053338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet    DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1054987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1055987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_signed:
1056fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
1057987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1058987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_unsigned:
1059fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
1060987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1061987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_void:
1062fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
1063987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1064987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_char:
1065fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
1066987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1067987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_int:
1068fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
1069987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1070987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_float:
1071fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
1072987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1073987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_double:
1074fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
1075987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1076987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_wchar_t:
1077fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
1078987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1079f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char16_t:
1080fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
1081f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1082f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char32_t:
1083fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
1084f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1085987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_bool:
1086fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
1087987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
10881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10896aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: C++0x decltype support.
1090987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // GNU typeof support.
1091987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_typeof:
1092987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    ParseTypeofSpecifier(DS);
10939b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor    DS.Finish(Diags, PP);
1094987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    return;
1095987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
1096b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  if (Tok.is(tok::annot_typename))
1097eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1098eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  else
1099eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getLocation());
1100987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  ConsumeToken();
11019b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor  DS.Finish(Diags, PP);
1102987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
11031cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
11042f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
11052f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers,
11062f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished
11072f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are
11082f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and
11092f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false
11102f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise.
11112f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
11122f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///   type-specifier-seq: [C++ 8.1]
11132f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///     type-specifier type-specifier-seq[opt]
11142f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
11152f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
11162f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  DS.SetRangeStart(Tok.getLocation());
11172f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  const char *PrevSpec = 0;
1118fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
1119fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  bool isInvalid = 0;
11202f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
11212f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // Parse one or more of the type specifiers.
1122d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1123d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl      ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
11249fa8e569407e02148888136609431a3fe083096dNick Lewycky    Diag(Tok, diag::err_expected_type);
11252f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor    return true;
11262f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  }
11271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1128d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1129d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl         ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1130d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  {}
11312f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
1132396a9f235e160093b5f803f7a6a18fad7b68bdbeDouglas Gregor  DS.Finish(Diags, PP);
11332f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  return false;
11342f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
11352f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
11363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of
11373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form.
11383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
11393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or
11403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
11413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will
11423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to
11433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller.
11443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
11453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if
11463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id.
11473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
11483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual
11493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name.
11503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
11513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or
11523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor.
11533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
11543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the
11553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
11563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
115746df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
115846df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
115946df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
11603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id
11613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully,
11623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id.
11633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1164d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param AssumeTemplateId When true, this routine will assume that the name
1165d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// refers to a template without performing name lookup to verify.
1166d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
11673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise.
11683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
11693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          IdentifierInfo *Name,
11703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          SourceLocation NameLoc,
11713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          bool EnteringContext,
1172b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          ParsedType ObjectType,
1173d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                          UnqualifiedId &Id,
11740278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          bool AssumeTemplateId,
11750278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          SourceLocation TemplateKWLoc) {
11760278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  assert((AssumeTemplateId || Tok.is(tok::less)) &&
11770278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor         "Expected '<' to finish parsing a template-id");
11783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
11793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateTy Template;
11803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateNameKind TNK = TNK_Non_template;
11813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (Id.getKind()) {
11823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
1183014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
1184e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
1185d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    if (AssumeTemplateId) {
118623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
1187d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Id, ObjectType, EnteringContext,
1188d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Template);
1189d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
1190d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        return true;
11911fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    } else {
11921fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
11937c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS,
11947c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateKWLoc.isValid(), Id,
11957c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   ObjectType, EnteringContext, Template,
11961fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
11971fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor
11981fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
11991fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          ObjectType && IsTemplateArgumentList()) {
12001fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // We have something like t->getAs<T>(), where getAs is a
12011fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // member of an unknown specialization. However, this will only
12021fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
12031fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
12041fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        std::string Name;
12051fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        if (Id.getKind() == UnqualifiedId::IK_Identifier)
12061fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = Id.Identifier->getName();
12071fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        else {
12081fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = "operator ";
12091fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
12101fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
12111fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          else
12121fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += Id.Identifier->getName();
12131fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        }
12141fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
12151fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << Name
12161fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << FixItHint::CreateInsertion(Id.StartLocation, "template ");
121723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
1218d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                 SS, Id, ObjectType,
1219d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                 EnteringContext, Template);
1220d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TNK == TNK_Non_template)
12211fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          return true;
12221fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      }
12231fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    }
12243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
12253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1226014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_ConstructorName: {
1227014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
12281fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1229014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
12307c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara    TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
12317c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                 TemplateName, ObjectType,
12321fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 EnteringContext, Template,
12331fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 MemberOfUnknownSpecialization);
12343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1235014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
12363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1237014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_DestructorName: {
1238014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
12391fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1240014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
12412d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (ObjectType) {
124223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
1243d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               TemplateName, ObjectType,
1244d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               EnteringContext, Template);
1245d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
12462d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
12472d1c21414199a7452f122598189363a3922605b1Douglas Gregor    } else {
12487c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
12497c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateName, ObjectType,
12501fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   EnteringContext, Template,
12511fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
12522d1c21414199a7452f122598189363a3922605b1Douglas Gregor
1253b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
1254124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        Diag(NameLoc, diag::err_destructor_template_id)
1255124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          << Name << SS.getRange();
12562d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
12572d1c21414199a7452f122598189363a3922605b1Douglas Gregor      }
12582d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
12593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1260014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
12613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  default:
12633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
12643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
12653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (TNK == TNK_Non_template)
12673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
12683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Parse the enclosed template argument list.
12703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
12713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgList TemplateArgs;
12720278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (Tok.is(tok::less) &&
12730278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
1274059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                       SS, true, LAngleLoc,
12753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgs,
12763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       RAngleLoc))
12773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
12783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
1280e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1281e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
12823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Form a parsed representation of the template-id to be stored in the
12833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // UnqualifiedId.
12843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateIdAnnotation *TemplateId
12853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      = TemplateIdAnnotation::Allocate(TemplateArgs.size());
12863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
12873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
12883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->Name = Id.Identifier;
1289014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = OO_None;
12903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
12913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
1292014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Name = 0;
1293014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = Id.OperatorFunctionId.Operator;
1294014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
12953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
12963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1297059101f922de6eb765601459925f4c8914420b23Douglas Gregor    TemplateId->SS = SS;
12982b5289b6fd7e3d9899868410a498c081c9595662John McCall    TemplateId->Template = Template;
12993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Kind = TNK;
13003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->LAngleLoc = LAngleLoc;
13013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->RAngleLoc = RAngleLoc;
1302314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
13033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
1304314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor         Arg != ArgEnd; ++Arg)
13053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Args[Arg] = TemplateArgs[Arg];
13063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setTemplateId(TemplateId);
13083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
13093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
13103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Bundle the template arguments together.
13123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
13133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                     TemplateArgs.size());
13143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Constructor and destructor names.
1316f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Type
1317059101f922de6eb765601459925f4c8914420b23Douglas Gregor    = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
13183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  LAngleLoc, TemplateArgsPtr,
13193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                  RAngleLoc);
13203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Type.isInvalid())
13213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
13223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
13243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
13253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  else
13263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
13273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
13283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return false;
13293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
13303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1331ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part
1332ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id.
13333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1334ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or
1335ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way.
13363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1337ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
13383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       operator-function-id: [C++ 13.5]
13393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         'operator' operator
13403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1341ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       operator: one of
13423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            new   delete  new[]   delete[]
13433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            +     -    *  /    %  ^    &   |   ~
13443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            !     =    <  >    += -=   *=  /=  %=
13453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
13463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
13473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ()    []
13483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
13493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-function-id: [C++ 12.3.2]
13503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator conversion-type-id
13513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
13523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-type-id:
13533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         type-specifier-seq conversion-declarator[opt]
13543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
13553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-declarator:
13563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ptr-operator conversion-declarator[opt]
13573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode
13583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
13593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
13603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
13613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
13623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the
13633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
13643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1365ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
1366ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed.
1367ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1368ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
1369ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1370ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise.
1371ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1372b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType ObjectType,
1373ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        UnqualifiedId &Result) {
1374ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1375ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1376ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Consume the 'operator' keyword.
1377ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation KeywordLoc = ConsumeToken();
1378ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1379ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Determine what kind of operator name we have.
1380ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  unsigned SymbolIdx = 0;
1381ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation SymbolLocations[3];
1382ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  OverloadedOperatorKind Op = OO_None;
1383ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  switch (Tok.getKind()) {
1384ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_new:
1385ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_delete: {
1386ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      bool isNew = Tok.getKind() == tok::kw_new;
1387ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the 'new' or 'delete'.
1388ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();
1389ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::l_square)) {
1390ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the '['.
1391ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SourceLocation LBracketLoc = ConsumeBracket();
1392ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the ']'.
1393ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1394ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                         LBracketLoc);
1395ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (RBracketLoc.isInvalid())
1396ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          return true;
1397ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1398ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SymbolLocations[SymbolIdx++] = LBracketLoc;
1399ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        SymbolLocations[SymbolIdx++] = RBracketLoc;
1400ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_Array_New : OO_Array_Delete;
1401ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
1402ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_New : OO_Delete;
1403ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
1404ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1405ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1406ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1407ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1408ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::Token:                                                     \
1409ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
1410ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_##Name;                                                    \
1411ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1412ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1413ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def"
1414ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1415ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_paren: {
1416ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the '('.
1417ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation LParenLoc = ConsumeParen();
1418ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the ')'.
1419ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1420ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                     LParenLoc);
1421ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (RParenLoc.isInvalid())
1422ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1423ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1424ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = LParenLoc;
1425ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = RParenLoc;
1426ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Call;
1427ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1428ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1429ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1430ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_square: {
1431ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the '['.
1432ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation LBracketLoc = ConsumeBracket();
1433ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the ']'.
1434ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1435ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                                       LBracketLoc);
1436ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (RBracketLoc.isInvalid())
1437ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1438ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1439ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = LBracketLoc;
1440ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = RBracketLoc;
1441ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Subscript;
1442ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1443ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1444ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1445ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::code_completion: {
1446ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Code completion for the operator name.
144723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOperatorName(getCurScope());
1448ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1449ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the operator token.
1450dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
1451ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1452ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Don't try to parse any further.
1453ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return true;
1454ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1455ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1456ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    default:
1457ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1458ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
1459ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1460ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Op != OO_None) {
1461ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // We have parsed an operator-function-id.
1462ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1463ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return false;
1464ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
14650486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
14660486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  // Parse a literal-operator-id.
14670486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //
14680486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //   literal-operator-id: [C++0x 13.5.8]
14690486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //     operator "" identifier
14700486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
14710486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
14720486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    if (Tok.getLength() != 2)
14730486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
14740486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    ConsumeStringToken();
14750486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
14760486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    if (Tok.isNot(tok::identifier)) {
14770486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_expected_ident);
14780486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      return true;
14790486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    }
14800486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
14810486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    IdentifierInfo *II = Tok.getIdentifierInfo();
14820486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
14833e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    return false;
14840486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  }
1485ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1486ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse a conversion-function-id.
1487ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1488ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-function-id: [C++ 12.3.2]
1489ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     operator conversion-type-id
1490ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1491ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-type-id:
1492ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     type-specifier-seq conversion-declarator[opt]
1493ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1494ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-declarator:
1495ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     ptr-operator conversion-declarator[opt]
1496ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1497ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the type-specifier-seq.
14980b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
1499f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor  if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
1500ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1501ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1502ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the conversion-declarator, which is merely a sequence of
1503ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // ptr-operators.
1504ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Declarator D(DS, Declarator::TypeNameContext);
1505ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1506ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1507ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Finish up the type.
1508f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
1509ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Ty.isInvalid())
1510ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1511ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1512ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Note that this is a conversion-function-id.
1513ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1514ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                 D.getSourceRange().getEnd());
1515ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return false;
1516ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
1517ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1518ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1519ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity.
1520ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1521ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
1522ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       unqualified-id: [C++ expr.prim.general]
1523ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         identifier
1524ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         operator-function-id
1525ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         conversion-function-id
1526ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO]
1527ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         ~ class-name
1528ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         template-id
1529ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1530ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode
1531ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1532ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
1533ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1534ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1535ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the
1536ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier.
1537ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
15383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name.
15393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
15403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name.
15413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
154246df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
154346df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
154446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
15453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
15463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
15473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise.
15483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
15493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowDestructorName,
15503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowConstructorName,
1551b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                ParsedType ObjectType,
15523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                UnqualifiedId &Result) {
15530278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
15540278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // Handle 'A::template B'. This is for template-ids which have not
15550278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // already been annotated by ParseOptionalCXXScopeSpecifier().
15560278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  bool TemplateSpecified = false;
15570278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  SourceLocation TemplateKWLoc;
15580278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
15590278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      (ObjectType || SS.isSet())) {
15600278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateSpecified = true;
15610278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateKWLoc = ConsumeToken();
15620278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  }
15630278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
15643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
15653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   identifier
15663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (when it hasn't already been annotated)
15673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::identifier)) {
15683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the identifier.
15693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *Id = Tok.getIdentifierInfo();
15703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation IdLoc = ConsumeToken();
15713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1572b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    if (!getLang().CPlusPlus) {
1573b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // If we're not in C++, only identifiers matter. Record the
1574b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // identifier and return.
1575b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      Result.setIdentifier(Id, IdLoc);
1576b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      return false;
1577b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    }
1578b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor
15793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (AllowConstructorName &&
158023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
15813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed a constructor name.
158223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
15839e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                                    &SS, false, false,
15849e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                                    ParsedType(),
15859e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            /*NonTrivialTypeSourceInfo=*/true),
15863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                IdLoc, IdLoc);
15873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
15883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an identifier.
15893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setIdentifier(Id, IdLoc);
15903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
15913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // If the next token is a '<', we may have a template.
15930278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less))
15943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
15950278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          ObjectType, Result,
15960278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          TemplateSpecified, TemplateKWLoc);
15973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
15993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
16003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
16023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (already parsed and annotated)
16033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::annot_template_id)) {
160425a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
16050efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
16060efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    // If the template-name names the current class, then this is a constructor
16070efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    if (AllowConstructorName && TemplateId->Name &&
160823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
16090efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      if (SS.isSet()) {
16100efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // C++ [class.qual]p2 specifies that a qualified template-name
16110efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // is taken as the constructor name where a constructor can be
16120efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // declared. Thus, the template arguments are extraneous, so
16130efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // complain about them and remove them entirely.
16140efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Diag(TemplateId->TemplateNameLoc,
16150efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor             diag::err_out_of_line_constructor_template_id)
16160efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor          << TemplateId->Name
1617849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateRemoval(
16180efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                    SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
16190efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
16200efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                                  TemplateId->TemplateNameLoc,
162123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor                                                      getCurScope(),
16229e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                                      &SS, false, false,
16239e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                                      ParsedType(),
16249e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            /*NontrivialTypeSourceInfo=*/true),
16250efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->TemplateNameLoc,
16260efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->RAngleLoc);
16270efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        ConsumeToken();
16280efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        return false;
16290efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      }
16300efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
16310efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      Result.setConstructorTemplateId(TemplateId);
16320efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      ConsumeToken();
16330efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      return false;
16340efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    }
16350efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
16363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // We have already parsed a template-id; consume the annotation token as
16373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // our unqualified-id.
16380efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    Result.setTemplateId(TemplateId);
16393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ConsumeToken();
16403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
16413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
16423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
16443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   operator-function-id
16453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   conversion-function-id
16463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::kw_operator)) {
1647ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
16483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
16493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1650e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // If we have an operator-function-id or a literal-operator-id and the next
1651e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // token is a '<', we may have a
1652ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //
1653ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //   template-id:
1654ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //     operator-function-id < template-argument-list[opt] >
1655e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1656e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt         Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
16570278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor        (TemplateSpecified || Tok.is(tok::less)))
1658ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1659ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                          EnteringContext, ObjectType,
16600278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          Result,
16610278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          TemplateSpecified, TemplateKWLoc);
16623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
16643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
16653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1666b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor  if (getLang().CPlusPlus &&
1667b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
16683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // C++ [expr.unary.op]p10:
16693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   There is an ambiguity in the unary-expression ~X(), where X is a
16703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   class-name. The ambiguity is resolved in favor of treating ~ as a
16713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //    unary complement rather than treating ~X as referring to a destructor.
16723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the '~'.
16743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation TildeLoc = ConsumeToken();
16753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name.
16773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.isNot(tok::identifier)) {
1678124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      Diag(Tok, diag::err_destructor_tilde_identifier);
16793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
16803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
16813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name (or template-name in a simple-template-id).
16833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
16843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation ClassNameLoc = ConsumeToken();
16853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16860278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less)) {
1687b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
16882d1c21414199a7452f122598189363a3922605b1Douglas Gregor      return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
16890278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          EnteringContext, ObjectType, Result,
16900278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor                                          TemplateSpecified, TemplateKWLoc);
16912d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
16922d1c21414199a7452f122598189363a3922605b1Douglas Gregor
16933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a destructor name.
1694b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1695b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              ClassNameLoc, getCurScope(),
1696b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              SS, ObjectType,
1697b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              EnteringContext);
1698124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (!Ty)
16993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
1700124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
17013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
17023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
17033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
17043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17052d1c21414199a7452f122598189363a3922605b1Douglas Gregor  Diag(Tok, diag::err_expected_unqualified_id)
17062d1c21414199a7452f122598189363a3922605b1Douglas Gregor    << getLang().CPlusPlus;
17073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return true;
17083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
17093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
17114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors.
17121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
171359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has
171459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
171559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location.  Otherwise, "Start" is the location of the 'new' token.
17164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
17174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
17184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] new-type-id
17194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
17204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
17214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
17224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
17234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
17244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
17254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1726cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-type-id:
1727cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   type-specifier-seq new-declarator[opt]
1728893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor/// [GNU]             attributes type-specifier-seq new-declarator[opt]
1729cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
1730cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-declarator:
1731cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   ptr-operator new-declarator[opt]
1732cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   direct-new-declarator
1733cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
17344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-initializer:
17354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list[opt] ')'
1736dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x]           braced-init-list
17374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
173860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
173959232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
174059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_new) && "expected 'new' token");
174159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken();   // Consume 'new'
17424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
17444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // second form of new-expression. It can't be a new-type-id.
17454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1746a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector PlacementArgs(Actions);
17474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation PlacementLParen, PlacementRParen;
17484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17494bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor  SourceRange TypeIdParens;
17500b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
1751cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
17524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
17534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // If it turns out to be a placement, we change the type location.
17544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementLParen = ConsumeParen();
1755cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1756cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
175720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1758cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
17594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
1761cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementRParen.isInvalid()) {
1762cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
176320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1764cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
17654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1766cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementArgs.empty()) {
17674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Reset the placement locations. There was no placement.
17684bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor      TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
17694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      PlacementLParen = PlacementRParen = SourceLocation();
17704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    } else {
17714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // We still need the type.
17724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      if (Tok.is(tok::l_paren)) {
17734bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor        TypeIdParens.setBegin(ConsumeParen());
1774893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor        MaybeParseGNUAttributes(DeclaratorInfo);
1775cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseSpecifierQualifierList(DS);
1776ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1777cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseDeclarator(DeclaratorInfo);
17784bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor        TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
17794bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor                                                TypeIdParens.getBegin()));
17804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      } else {
1781893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor        MaybeParseGNUAttributes(DeclaratorInfo);
1782cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        if (ParseCXXTypeSpecifierSeq(DS))
1783cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          DeclaratorInfo.setInvalidType(true);
1784ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        else {
1785ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1786cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          ParseDeclaratorInternal(DeclaratorInfo,
1787cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                                  &Parser::ParseDirectNewDeclarator);
1788ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        }
17894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      }
17904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
17914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  } else {
1792cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // A new-type-id is a simplified type-id, where essentially the
1793cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // direct-declarator is replaced by a direct-new-declarator.
1794893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor    MaybeParseGNUAttributes(DeclaratorInfo);
1795cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseCXXTypeSpecifierSeq(DS))
1796cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      DeclaratorInfo.setInvalidType(true);
1797ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    else {
1798ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
1799cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      ParseDeclaratorInternal(DeclaratorInfo,
1800cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                              &Parser::ParseDirectNewDeclarator);
1801ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    }
18024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
1803eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (DeclaratorInfo.isInvalidType()) {
1804cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
180520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
1806cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
18074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1808a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector ConstructorArgs(Actions);
18094c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation ConstructorLParen, ConstructorRParen;
18104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
18114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
18124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorLParen = ConsumeParen();
18134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (Tok.isNot(tok::r_paren)) {
18144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      CommaLocsTy CommaLocs;
1815cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1816cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
181720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl        return ExprError();
1818cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      }
18194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
18204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
1821cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ConstructorRParen.isInvalid()) {
1822cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
182320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1824cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
1825dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  } else if (Tok.is(tok::l_brace)) {
1826dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // FIXME: Have to communicate the init-list to ActOnCXXNew.
1827dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    ParseBraceInitializer();
18284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
18294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1830f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1831f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(PlacementArgs), PlacementRParen,
18324bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor                             TypeIdParens, DeclaratorInfo, ConstructorLParen,
1833f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(ConstructorArgs), ConstructorRParen);
18344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
18354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
18364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
18374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal.
18384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
18394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        direct-new-declarator:
18404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '[' expression ']'
18414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   direct-new-declarator '[' constant-expression ']'
18424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
184359232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) {
18444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Parse the array dimensions.
18454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool first = true;
18464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  while (Tok.is(tok::l_square)) {
18474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LLoc = ConsumeBracket();
184860d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Size(first ? ParseExpression()
18492f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl                                : ParseConstantExpression());
18500e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Size.isInvalid()) {
18514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Recover
18524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      SkipUntil(tok::r_square);
18534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
18544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
18554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    first = false;
18564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1857ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
18580b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
18590b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes attrs(AttrFactory);
18600b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    D.AddTypeInfo(DeclaratorChunk::getArray(0,
18617f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                            /*static=*/false, /*star=*/false,
18627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            Size.release(), LLoc, RLoc),
18630b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                  attrs, RLoc);
18644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1865ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (RLoc.isInvalid())
18664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
18674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
18684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
18694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
18704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
18714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator.
18724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
18734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
18744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
18754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
18764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
18774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
18784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
18794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
1880ca0408fb49c1370430672acf2d770b7151cf71deJohn McCallbool Parser::ParseExpressionListOrTypeId(
1881ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall                                   llvm::SmallVectorImpl<Expr*> &PlacementArgs,
188259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner                                         Declarator &D) {
18834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // The '(' was already consumed.
18844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (isTypeIdInParens()) {
1885cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseSpecifierQualifierList(D.getMutableDeclSpec());
1886ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetSourceRange(D.getDeclSpec().getSourceRange());
1887cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseDeclarator(D);
1888eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    return D.isInvalidType();
18894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
18904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
18914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // It's not a type, it has to be an expression list.
18924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Discard the comma locations - ActOnCXXNew has enough parameters.
18934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  CommaLocsTy CommaLocs;
18944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return ParseExpressionList(PlacementArgs, CommaLocs);
18954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
18964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
18974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
18984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new.
18994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
190059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional
190159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
190259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location.  Otherwise, "Start" is the location of the
190359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token.
190459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner///
19054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        delete-expression:
19064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' cast-expression
19074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' '[' ']' cast-expression
190860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
190959232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
191059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
191159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken(); // Consume 'delete'
19124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
19134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Array delete?
19144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ArrayDelete = false;
19154c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_square)) {
19164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ArrayDelete = true;
19174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation LHS = ConsumeBracket();
19184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
19194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (RHS.isInvalid())
192020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
19214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
19224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
192360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Operand(ParseCastExpression(false));
19240e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Operand.isInvalid())
192520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return move(Operand);
19264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
19279ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
19284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
192964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
19301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
193164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch(kind) {
193220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  default: assert(false && "Not a known unary type trait.");
193364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign;
193464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
193520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___has_nothrow_copy:           return UTT_HasNothrowCopy;
193664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign;
1937023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  case tok::kw___has_trivial_constructor:
1938023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                    return UTT_HasTrivialDefaultConstructor;
193920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___has_trivial_copy:           return UTT_HasTrivialCopy;
194064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor;
194164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor;
194264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_abstract:             return UTT_IsAbstract;
194320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_arithmetic:              return UTT_IsArithmetic;
194420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_array:                   return UTT_IsArray;
194564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_class:                return UTT_IsClass;
194620c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_complete_type:           return UTT_IsCompleteType;
194720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_compound:                return UTT_IsCompound;
194820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_const:                   return UTT_IsConst;
194964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_empty:                return UTT_IsEmpty;
195064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_enum:                 return UTT_IsEnum;
195120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_floating_point:          return UTT_IsFloatingPoint;
195220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_function:                return UTT_IsFunction;
195320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_fundamental:             return UTT_IsFundamental;
195420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_integral:                return UTT_IsIntegral;
195520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_lvalue_reference:        return UTT_IsLvalueReference;
195620c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
195720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_object_pointer:   return UTT_IsMemberObjectPointer;
195820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_pointer:          return UTT_IsMemberPointer;
195920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_object:                  return UTT_IsObject;
19604e61ddd644e9c6293697a966d98d7c1905cf63a8Chandler Carruth  case tok::kw___is_literal:              return UTT_IsLiteral;
19613840281126e7d10552c55f6fd8b1ec9483898906Chandler Carruth  case tok::kw___is_literal_type:         return UTT_IsLiteral;
196264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_pod:                  return UTT_IsPOD;
196320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_pointer:                 return UTT_IsPointer;
196464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_polymorphic:          return UTT_IsPolymorphic;
196520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_reference:               return UTT_IsReference;
196620c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_rvalue_reference:        return UTT_IsRvalueReference;
196720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_scalar:                  return UTT_IsScalar;
196820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_signed:                  return UTT_IsSigned;
196920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_standard_layout:         return UTT_IsStandardLayout;
197020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_trivial:                 return UTT_IsTrivial;
1971feb375d31b7e9108b04a9f55b721d5e0c793a558Sean Hunt  case tok::kw___is_trivially_copyable:      return UTT_IsTriviallyCopyable;
197264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_union:                return UTT_IsUnion;
197320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_unsigned:                return UTT_IsUnsigned;
197420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_void:                    return UTT_IsVoid;
197520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_volatile:                return UTT_IsVolatile;
197664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
19776ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
19786ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
19796ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichetstatic BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
19806ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  switch(kind) {
198138c2b730a8553fa1cf369d0c5567f8b5d0a3dda8Francois Pichet  default: llvm_unreachable("Not a known binary type trait");
1982f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case tok::kw___is_base_of:                 return BTT_IsBaseOf;
198320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_convertible:             return BTT_IsConvertible;
198420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_same:                    return BTT_IsSame;
1985f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
19869f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor  case tok::kw___is_convertible_to:          return BTT_IsConvertibleTo;
19876ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
198864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
198964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
199021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegleystatic ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
199121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch(kind) {
199221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  default: llvm_unreachable("Not a known binary type trait");
199321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case tok::kw___array_rank:                 return ATT_ArrayRank;
199421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case tok::kw___array_extent:               return ATT_ArrayExtent;
199521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
199621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
199721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
1998552622067dc45013d240f73952fece703f5e63bdJohn Wiegleystatic ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
1999552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  switch(kind) {
2000552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  default: assert(false && "Not a known unary expression trait.");
2001552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case tok::kw___is_lvalue_expr:             return ET_IsLValueExpr;
2002552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case tok::kw___is_rvalue_expr:             return ET_IsRValueExpr;
2003552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  }
2004552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
2005552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
200664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
200764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits
200864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates.
200964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
201064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///       primary-expression:
201164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU]             unary-type-trait '(' type-id ')'
201264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
201360d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseUnaryTypeTrait() {
201464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
201564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation Loc = ConsumeToken();
201664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
201764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation LParen = Tok.getLocation();
201864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
201964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return ExprError();
202064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
202164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
202264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // there will be cryptic errors about mismatched parentheses and missing
202364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // specifiers.
2024809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
202564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
202664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
202764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
2028809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2029809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2030809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
20313d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor  return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
203264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
2033f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
20346ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
20356ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// pseudo-functions that allow implementation of the TR1/C++0x type traits
20366ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// templates.
20376ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///
20386ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///       primary-expression:
20396ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// [GNU]             binary-type-trait '(' type-id ',' type-id ')'
20406ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///
20416ad6f2848d7652ab2991286eb48be440d3493b28Francois PichetExprResult Parser::ParseBinaryTypeTrait() {
20426ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
20436ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  SourceLocation Loc = ConsumeToken();
20446ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
20456ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  SourceLocation LParen = Tok.getLocation();
20466ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
20476ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
20486ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
20496ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeResult LhsTy = ParseTypeName();
20506ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (LhsTy.isInvalid()) {
20516ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
20526ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
20536ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
20546ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
20556ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
20566ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
20576ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
20586ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
20596ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
20606ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeResult RhsTy = ParseTypeName();
20616ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (RhsTy.isInvalid()) {
20626ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
20636ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
20646ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
20656ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
20666ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
20676ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
20686ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
20696ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
20706ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
207121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// ParseArrayTypeTrait - Parse the built-in array type-trait
207221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// pseudo-functions.
207321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///
207421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///       primary-expression:
207521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// [Embarcadero]     '__array_rank' '(' type-id ')'
207621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// [Embarcadero]     '__array_extent' '(' type-id ',' expression ')'
207721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///
207821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John WiegleyExprResult Parser::ParseArrayTypeTrait() {
207921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
208021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  SourceLocation Loc = ConsumeToken();
208121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
208221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  SourceLocation LParen = Tok.getLocation();
208321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
208421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return ExprError();
208521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
208621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  TypeResult Ty = ParseTypeName();
208721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  if (Ty.isInvalid()) {
208821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    SkipUntil(tok::comma);
208921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    SkipUntil(tok::r_paren);
209021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return ExprError();
209121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
209221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
209321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch (ATT) {
209421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayRank: {
209521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
209621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, RParen);
209721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
209821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayExtent: {
209921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
210021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      SkipUntil(tok::r_paren);
210121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      return ExprError();
210221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    }
210321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
210421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    ExprResult DimExpr = ParseExpression();
210521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
210621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
210721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), RParen);
210821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
210921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  default:
211021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    break;
211121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
211221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  return ExprError();
211321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
211421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
2115552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// ParseExpressionTrait - Parse built-in expression-trait
2116552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// pseudo-functions like __is_lvalue_expr( xxx ).
2117552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///
2118552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///       primary-expression:
2119552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// [Embarcadero]     expression-trait '(' expression ')'
2120552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///
2121552622067dc45013d240f73952fece703f5e63bdJohn WiegleyExprResult Parser::ParseExpressionTrait() {
2122552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2123552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  SourceLocation Loc = ConsumeToken();
2124552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2125552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  SourceLocation LParen = Tok.getLocation();
2126552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2127552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    return ExprError();
2128552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2129552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  ExprResult Expr = ParseExpression();
2130552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2131552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2132552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2133552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), RParen);
2134552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
2135552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2136552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2137f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2138f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2139f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens.
214060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2141f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
2142b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                         ParsedType &CastTy,
2143f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation LParenLoc,
2144f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis                                         SourceLocation &RParenLoc) {
2145f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(getLang().CPlusPlus && "Should only be called for C++!");
2146f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2147f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(isTypeIdInParens() && "Not a type-id!");
2148f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
214960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(true);
2150b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  CastTy = ParsedType();
2151f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2152f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // We need to disambiguate a very ugly part of the C++ syntax:
2153f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2154f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())x;  - type-id
2155f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())*x; - type-id
2156f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())/x; - expression
2157f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T());   - expression
2158f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2159f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The bad news is that we cannot use the specialized tentative parser, since
2160f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // it can only verify that the thing inside the parens can be parsed as
2161f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // type-id, it is not useful for determining the context past the parens.
2162f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2163f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The good news is that the parser can disambiguate this part without
2164a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis  // making any unnecessary Action calls.
2165f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  //
2166f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // It uses a scheme similar to parsing inline methods. The parenthesized
2167f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // tokens are cached, the context that follows is determined (possibly by
2168f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parsing a cast-expression), and then we re-introduce the cached tokens
2169f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // into the token stream and parse them appropriately.
2170f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
21711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParenParseOption ParseAs;
2172f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  CachedTokens Toks;
2173f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2174f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Store the tokens of the parentheses. We will parse them after we determine
2175f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // the context that follows them.
217614b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis  if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
2177f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We didn't find the ')' we expected.
2178f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
2179f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
2180f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
2181f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2182f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::l_brace)) {
2183f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = CompoundLiteral;
2184f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  } else {
2185f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    bool NotCastExpr;
2186b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2187b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2188b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      NotCastExpr = true;
2189b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    } else {
2190b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // Try parsing the cast-expression that may follow.
2191b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // If it is not a cast-expression, NotCastExpr will be true and no token
2192b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // will be consumed.
2193b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      Result = ParseCastExpression(false/*isUnaryExpression*/,
2194b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman                                   false/*isAddressofOperand*/,
2195b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   NotCastExpr,
2196b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   ParsedType()/*TypeOfCast*/);
2197b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    }
2198f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2199f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // If we parsed a cast-expression, it's really a type-id, otherwise it's
2200f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // an expression.
2201f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
2202f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
2203f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
22041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The current token should go after the cached tokens.
2205f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Toks.push_back(Tok);
2206f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Re-enter the stored parenthesized tokens into the token stream, so we may
2207f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parse them now.
2208f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  PP.EnterTokenStream(Toks.data(), Toks.size(),
2209f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2210f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Drop the current token and bring the first cached one. It's the same token
2211f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // as when we entered this function.
2212f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  ConsumeAnyToken();
2213f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2214f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (ParseAs >= CompoundLiteral) {
2215f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    TypeResult Ty = ParseTypeName();
2216f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2217f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Match the ')'.
2218f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Tok.is(tok::r_paren))
2219f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      RParenLoc = ConsumeParen();
2220f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    else
2221f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      MatchRHSPunctuation(tok::r_paren, LParenLoc);
2222f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2223f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (ParseAs == CompoundLiteral) {
2224f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      ExprType = CompoundLiteral;
2225f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2226f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    }
22271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2228f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2229f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    assert(ParseAs == CastExpr);
2230f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2231f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (Ty.isInvalid())
2232f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ExprError();
2233f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2234f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    CastTy = Ty.get();
2235f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2236f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Result is what ParseCastExpression returned earlier.
2237f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    if (!Result.isInvalid())
223823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
22399ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     Result.take());
2240f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return move(Result);
2241f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
22421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2243f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Not a compound literal, and not followed by a cast-expression.
2244f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  assert(ParseAs == SimpleExpr);
2245f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2246f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  ExprType = SimpleExpr;
2247f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Result = ParseExpression();
2248f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (!Result.isInvalid() && Tok.is(tok::r_paren))
22499ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
2250f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2251f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // Match the ')'.
2252f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Result.isInvalid()) {
2253f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    SkipUntil(tok::r_paren);
2254f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
2255f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
22561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2257f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::r_paren))
2258f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    RParenLoc = ConsumeParen();
2259f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  else
2260f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    MatchRHSPunctuation(tok::r_paren, LParenLoc);
2261f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2262f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  return move(Result);
2263f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis}
2264