ParseExprCXX.cpp revision 9ef9875bbe19dc9f73c6c95b803d9a4945168690
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
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h"
15bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor#include "RAIIObjectsForParser.h"
16dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman#include "clang/Basic/PrettyStackTrace.h"
1733762775706e81c17ca774102ceda36049ecc593Richard Smith#include "clang/Lex/LiteralSupport.h"
1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Parse/ParseDiagnostic.h"
1919510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
2019510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h"
2155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Scope.h"
223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h"
233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic int SelectDigraphErrorMessage(tok::TokenKind Kind) {
27ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  switch (Kind) {
28ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_template:         return 0;
29ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_const_cast:       return 1;
30ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_dynamic_cast:     return 2;
31ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_reinterpret_cast: return 3;
32ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_static_cast:      return 4;
33ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    default:
34b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unknown type for digraph error message.");
35ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  }
36ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
37ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
38ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith// Are the two tokens adjacent in the same source file?
3919a2702042b7e3ee838cca458b35f607111a3897Richard Smithbool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
40ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceManager &SM = PP.getSourceManager();
41ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
42a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
43ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  return FirstEnd == SM.getSpellingLoc(Second.getLocation());
44ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
45ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
46ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith// Suggest fixit for "<::" after a cast.
47ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
48ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                       Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
49ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Pull '<:' and ':' off token stream.
50ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (!AtDigraph)
51ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    PP.Lex(DigraphToken);
52ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  PP.Lex(ColonToken);
53ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
54ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceRange Range;
55ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Range.setBegin(DigraphToken.getLocation());
56ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Range.setEnd(ColonToken.getLocation());
57ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
58ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      << SelectDigraphErrorMessage(Kind)
59ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      << FixItHint::CreateReplacement(Range, "< ::");
60ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
61ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Update token information to reflect their change in token type.
62ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setKind(tok::coloncolon);
63a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
64ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setLength(2);
65ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  DigraphToken.setKind(tok::less);
66ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  DigraphToken.setLength(1);
67ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
68ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Push new tokens back to token stream.
69ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  PP.EnterToken(ColonToken);
70ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (!AtDigraph)
71ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    PP.EnterToken(DigraphToken);
72ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
73ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
74950be71c745409e373ae8a834490f9026c8ac222Richard Trieu// Check for '<::' which should be '< ::' instead of '[:' when following
75950be71c745409e373ae8a834490f9026c8ac222Richard Trieu// a template name.
76950be71c745409e373ae8a834490f9026c8ac222Richard Trieuvoid Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
77950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                                        bool EnteringContext,
78950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                                        IdentifierInfo &II, CXXScopeSpec &SS) {
79c11030ea936f6952deb5a1423ce1648173cd417eRichard Trieu  if (!Next.is(tok::l_square) || Next.getLength() != 2)
80950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
81950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
82950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  Token SecondToken = GetLookAheadToken(2);
8319a2702042b7e3ee838cca458b35f607111a3897Richard Smith  if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
84950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
85950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
86950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  TemplateTy Template;
87950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  UnqualifiedId TemplateName;
88950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  TemplateName.setIdentifier(&II, Tok.getLocation());
89950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  bool MemberOfUnknownSpecialization;
90950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
91950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                              TemplateName, ObjectType, EnteringContext,
92950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                              Template, MemberOfUnknownSpecialization))
93950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
94950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
95950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
96950be71c745409e373ae8a834490f9026c8ac222Richard Trieu             /*AtDigraph*/false);
97950be71c745409e373ae8a834490f9026c8ac222Richard Trieu}
98950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
99919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu/// \brief Emits an error for a left parentheses after a double colon.
100919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu///
101919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu/// When a '(' is found after a '::', emit an error.  Attempt to fix the token
102bba91b881c946cbcd200c87dc0a83553506e2458Nico Weber/// stream by removing the '(', and the matching ')' if found.
103919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieuvoid Parser::CheckForLParenAfterColonColon() {
104919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  if (!Tok.is(tok::l_paren))
105919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    return;
106919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
107919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
108919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  Token Tok1 = getCurToken();
109919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
110919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    return;
111919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
112919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  if (Tok1.is(tok::identifier)) {
113919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    Token Tok2 = GetLookAheadToken(1);
114919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    if (Tok2.is(tok::r_paren)) {
115919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      ConsumeToken();
116919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      PP.EnterToken(Tok1);
117919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      r_parenLoc = ConsumeParen();
118919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    }
119919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  } else if (Tok1.is(tok::star)) {
120919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    Token Tok2 = GetLookAheadToken(1);
121919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    if (Tok2.is(tok::identifier)) {
122919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      Token Tok3 = GetLookAheadToken(2);
123919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      if (Tok3.is(tok::r_paren)) {
124919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        ConsumeToken();
125919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        ConsumeToken();
126919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        PP.EnterToken(Tok2);
127919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        PP.EnterToken(Tok1);
128919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        r_parenLoc = ConsumeParen();
129919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      }
130919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    }
131919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  }
132919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
133919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  Diag(l_parenLoc, diag::err_paren_after_colon_colon)
134919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      << FixItHint::CreateRemoval(l_parenLoc)
135919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      << FixItHint::CreateRemoval(r_parenLoc);
136919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu}
137919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
1381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present.
1392dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1402dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
1411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or
1422dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream.
143eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
144eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'[opt] nested-name-specifier
145eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'
146eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
147eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       nested-name-specifier:
148eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         type-name '::'
149eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         namespace-name '::'
150eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         nested-name-specifier identifier '::'
1512dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///         nested-name-specifier 'template'[opt] simple-template-id '::'
1522dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1532dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed
1552dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty)
1562dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following
1582dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the
1592dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed.
160eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
1612dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of
1622dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it.
1632dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
164d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param MayBePseudoDestructor When non-NULL, points to a flag that
165d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// indicates whether this nested-name-specifier may be part of a
166d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// pseudo-destructor name. In this case, the flag will be set false
167d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we don't actually end up parsing a destructor name. Moreorover,
168d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we do end up determining that we are parsing a destructor name,
169d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// the last component of the nested-name-specifier is not parsed as
170d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// part of the scope specifier.
1712db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith///
1722db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// \param IsTypename If \c true, this nested-name-specifier is known to be
1732db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// part of a type name. This is used to improve error recovery.
1742db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith///
1752db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
1762db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// filled in with the leading identifier in the last component of the
1772db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// nested-name-specifier, if any.
178b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor///
1799ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// \returns true if there was an error parsing a scope specifier
180495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
181b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType ObjectType,
182b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            bool EnteringContext,
1834147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                            bool *MayBePseudoDestructor,
1842db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith                                            bool IsTypename,
1852db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith                                            IdentifierInfo **LastII) {
1864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus &&
1877452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Call sites of this function should be guarded by checking for C++");
1881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
189eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::annot_cxxscope)) {
1902db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith    assert(!LastII && "want last identifier but have already annotated scope");
191c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
192c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 Tok.getAnnotationRange(),
193c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 SS);
194eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    ConsumeToken();
1959ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
196eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
197e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner
1982db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  if (LastII)
1992db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith    *LastII = 0;
2002db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith
20139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  bool HasScopeSpecifier = false;
20239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
2035b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner  if (Tok.is(tok::coloncolon)) {
2045b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    // ::new and ::delete aren't nested-name-specifiers.
2055b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    tok::TokenKind NextKind = NextToken().getKind();
2065b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
2075b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner      return false;
2081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20955a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    // '::' - Global scope qualifier.
2102e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
2112e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
212919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
213919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    CheckForLParenAfterColonColon();
214919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
21539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    HasScopeSpecifier = true;
216eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
217eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
218d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  bool CheckForDestructor = false;
219d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (MayBePseudoDestructor && *MayBePseudoDestructor) {
220d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CheckForDestructor = true;
221d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = false;
222d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
223d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
22442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
22542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    DeclSpec DS(AttrFactory);
22642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation DeclLoc = Tok.getLocation();
22742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation EndLoc  = ParseDecltypeSpecifier(DS);
22842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    if (Tok.isNot(tok::coloncolon)) {
22942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
23042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      return false;
23142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    }
23242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
23342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation CCLoc = ConsumeToken();
23442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
23542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
23642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
23742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    HasScopeSpecifier = true;
23842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  }
23942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
24039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  while (true) {
2412dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (HasScopeSpecifier) {
2422dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p5:
2432dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the qualified-id has the form
2443b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
2452dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //       ::class-name-or-namespace-name::...
2463b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
2472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the class-name-or-namespace-name is looked up in global scope as a
2482dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name.
2492dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
2502dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // To implement this, we clear out the object type as soon as we've
2512dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // seen a leading '::' or part of a nested-name-specifier.
252b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      ObjectType = ParsedType();
25381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
25481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      if (Tok.is(tok::code_completion)) {
25581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // Code completion for a nested-name-specifier, where the code
25681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // code completion token follows the '::'.
25723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
258b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // Include code completion token into the range of the scope otherwise
259b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // when we try to annotate the scope tokens the dangling code completion
260b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // token will cause assertion in
261b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // Preprocessor::AnnotatePreviousCachedTokens.
2627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        SS.setEndLoc(Tok.getLocation());
2637d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
2647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return true;
26581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      }
2662dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
2671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier:
26977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    //   nested-name-specifier 'template'[opt] simple-template-id '::'
27077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner
27177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // Parse the optional 'template' keyword, then make sure we have
27277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // 'identifier <' after it.
27377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    if (Tok.is(tok::kw_template)) {
2742dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // If we don't have a scope specifier or an object type, this isn't a
275eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // nested-name-specifier, since they aren't allowed to start with
276eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // 'template'.
2772dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      if (!HasScopeSpecifier && !ObjectType)
278eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
279eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman
2807bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TentativeParsingAction TPA(*this);
28177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      SourceLocation TemplateKWLoc = ConsumeToken();
282ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
283ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      UnqualifiedId TemplateName;
284ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::identifier)) {
285ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
2867bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
287ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
288ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else if (Tok.is(tok::kw_operator)) {
289ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
2907bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor                                       TemplateName)) {
2917bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
292ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
2937bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        }
294ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
295e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt        if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
296e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt            TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
297ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          Diag(TemplateName.getSourceRange().getBegin(),
298ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor               diag::err_id_after_template_in_nested_name_spec)
299ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor            << TemplateName.getSourceRange();
3007bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
301ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
302ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        }
303ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
3047bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
30577cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        break;
30677cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      }
3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3087bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // If the next token is not '<', we have a qualified-id that refers
3097bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // to a template name, such as T::template apply, but is not a
3107bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // template-id.
3117bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      if (Tok.isNot(tok::less)) {
3127bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
3137bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        break;
3147bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      }
3157bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor
3167bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // Commit to parsing the template-id.
3177bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TPA.Commit();
318d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      TemplateTy Template;
319e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      if (TemplateNameKind TNK
320e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara          = Actions.ActOnDependentTemplateName(getCurScope(),
321e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               SS, TemplateKWLoc, TemplateName,
322e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               ObjectType, EnteringContext,
323e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               Template)) {
324e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
325e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName, false))
326d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          return true;
327d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      } else
3289ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
3291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      continue;
33177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    }
3321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We have
33539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
33639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //   simple-template-id '::'
33739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
33839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // So we need to check whether the simple-template-id is of the
339c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // right kind (it should name a type or be dependent), and then
340c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // convert it into a type within the nested-name-specifier.
34125a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
342d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
343d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
3449ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
345d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
346d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
3472db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith      if (LastII)
3482db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith        *LastII = TemplateId->Name;
3492db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith
3506cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      // Consume the template-id token.
3516cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      ConsumeToken();
3526cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
3536cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
3546cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      SourceLocation CCLoc = ConsumeToken();
3551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3566796fc1adcaf57c38d072a238b016b2834afbe0dDavid Blaikie      HasScopeSpecifier = true;
3576cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
3585354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer      ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
3596cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                         TemplateId->NumArgs);
3606cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
3616cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
362e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                              SS,
363e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                              TemplateId->TemplateKWLoc,
3646cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->Template,
3656cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->TemplateNameLoc,
3666cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->LAngleLoc,
3676cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateArgsPtr,
3686cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->RAngleLoc,
3696cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              CCLoc,
3706cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              EnteringContext)) {
3716cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        SourceLocation StartLoc
3726cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor          = SS.getBeginLoc().isValid()? SS.getBeginLoc()
3736cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                      : TemplateId->TemplateNameLoc;
3746cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        SS.SetInvalid(SourceRange(StartLoc, CCLoc));
37567b9e831943300ce54e564e601971828ce4def15Chris Lattner      }
376eccce7e246a17e12a2afd6eabb9ac7c8d582db4eArgyrios Kyrtzidis
3776cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      continue;
37839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
37939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
3805c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
3815c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // The rest of the nested-name-specifier possibilities start with
3825c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // tok::identifier.
3835c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Tok.isNot(tok::identifier))
3845c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      break;
3855c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
3865c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    IdentifierInfo &II = *Tok.getIdentifierInfo();
3875c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
3885c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
3895c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '::'
3905c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   namespace-name '::'
3915c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   nested-name-specifier identifier '::'
3925c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    Token Next = NextToken();
39346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
39446646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // If we get foo:bar, this is almost certainly a typo for foo::bar.  Recover
39546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // and emit a fixit hint for it.
396b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor    if (Next.is(tok::colon) && !ColonIsSacred) {
3972e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
3982e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Tok.getLocation(),
3992e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Next.getLocation(), ObjectType,
400b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            EnteringContext) &&
401b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // If the token after the colon isn't an identifier, it's still an
402b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // error, but they probably meant something else strange so don't
403b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // recover like this.
404b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          PP.LookAhead(1).is(tok::identifier)) {
405b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
406849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateReplacement(Next.getLocation(), "::");
407b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor
408b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        // Recover as if the user wrote '::'.
409b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Next.setKind(tok::coloncolon);
410b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor      }
41146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    }
41246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
4135c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::coloncolon)) {
41477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
41523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
41677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                                II, ObjectType)) {
417d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
4189ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
419d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
420d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
4212db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith      if (LastII)
4222db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith        *LastII = &II;
4232db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith
4245c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // We have an identifier followed by a '::'. Lookup this name
4255c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // as the name in a nested-name-specifier.
4265c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation IdLoc = ConsumeToken();
42746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner      assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
42846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner             "NextToken() not working properly!");
4295c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation CCLoc = ConsumeToken();
4301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
431919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      CheckForLParenAfterColonColon();
432919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
4332e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      HasScopeSpecifier = true;
4342e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
4352e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                              ObjectType, EnteringContext, SS))
4362e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        SS.SetInvalid(SourceRange(IdLoc, CCLoc));
4372e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
4385c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      continue;
4395c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
441950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
442ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
4435c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
4445c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '<'
4455c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::less)) {
4465c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      TemplateTy Template;
447014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
448014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(&II, Tok.getLocation());
4491fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
45023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
4517c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                              /*hasTemplateKeyword=*/false,
452014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                                        TemplateName,
4532dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                                        ObjectType,
454495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        EnteringContext,
4551fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                                        Template,
4561fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                              MemberOfUnknownSpecialization)) {
4576796fc1adcaf57c38d072a238b016b2834afbe0dDavid Blaikie        // We have found a template name, so annotate this token
4585c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // with a template-id annotation. We do not permit the
4595c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // template-id to be translated into a type annotation,
4605c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // because some clients (e.g., the parsing of class template
4615c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // specializations) still want to see the original template-id
4625c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // token.
463ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
464e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
465e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName, false))
4669ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
4675c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
468d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      }
469d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
470d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
4714147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet          (IsTypename || IsTemplateArgumentList(1))) {
472d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // We have something like t::getAs<T>, where getAs is a
473d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // member of an unknown specialization. However, this will only
474d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
475d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
4764147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        unsigned DiagID = diag::err_missing_dependent_template_keyword;
4774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (getLangOpts().MicrosoftExt)
478cf320c6388c90f1938c264e87d77a0e43946e2c3Francois Pichet          DiagID = diag::warn_missing_dependent_template_keyword;
4794147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet
4804147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        Diag(Tok.getLocation(), DiagID)
481d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << II.getName()
482d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
483d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
484d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TemplateNameKind TNK
48523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor              = Actions.ActOnDependentTemplateName(getCurScope(),
486e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                   SS, SourceLocation(),
487d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   TemplateName, ObjectType,
488d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   EnteringContext, Template)) {
489d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          // Consume the identifier.
490d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          ConsumeToken();
491e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara          if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
492e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      TemplateName, false))
493e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara            return true;
494d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        }
495d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        else
496d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          return true;
497d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor
498d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        continue;
4995c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
5005c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
5015c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
50239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // We don't have any tokens that form the beginning of a
50339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier, so we're done.
50439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    break;
50539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
507d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Even if we didn't see any pieces of a nested-name-specifier, we
508d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // still check whether there is a tilde in this position, which
509d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // indicates a potential pseudo-destructor.
510d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (CheckForDestructor && Tok.is(tok::tilde))
511d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = true;
512d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
5139ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
514eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
515eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
516eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression.
517eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
518eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       id-expression:
519eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         unqualified-id
520eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         qualified-id
521eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
522eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
523eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
524eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' identifier
525eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' operator-function-id
526edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor///         '::' template-id
527eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
528eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not
529eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect:
530eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
531eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' conversion-function-id
532eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' '~' class-name
533eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
534eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics:
535eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
536eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {};
537eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {}
538eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() {
539eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: A :: ~ C(); // Some Sema error about using destructor with a
540eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///                  // namespace.
541eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: ~ C(); // Some Parser error like 'unexpected ~'.
542eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// }
543eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
544eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like:
545eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
546eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
547eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
548eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' unqualified-id
549eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
550eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the
551eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope.
552eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
553ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a
554ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts,
555ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may
556ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear.
557ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl///
55860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
559eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  // qualified-id:
560eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
561eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::' unqualified-id
562eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //
563eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
564efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
565e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
566e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SourceLocation TemplateKWLoc;
56702a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  UnqualifiedId Name;
568e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  if (ParseUnqualifiedId(SS,
569e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*EnteringContext=*/false,
570e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*AllowDestructorName=*/false,
571e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*AllowConstructorName=*/false,
572b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                         /*ObjectType=*/ ParsedType(),
573e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         TemplateKWLoc,
57402a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         Name))
57502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor    return ExprError();
576b681b61fea36618778b8030360e90e3f4641233bJohn McCall
577b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // This is only the direct operand of an & operator if it is not
578b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // followed by a postfix-expression suffix.
5799c72c6088d591ace8503b842d39448c2040f3033John McCall  if (isAddressOfOperand && isPostfixExpressionSuffixStart())
5809c72c6088d591ace8503b842d39448c2040f3033John McCall    isAddressOfOperand = false;
581e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
582e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
583e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   Tok.is(tok::l_paren), isAddressOfOperand);
584eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
585eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
586ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// ParseLambdaExpression - Parse a C++0x lambda expression.
587ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
588ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-expression:
589ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         lambda-introducer lambda-declarator[opt] compound-statement
590ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
591ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-introducer:
592ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '[' lambda-capture[opt] ']'
593ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
594ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-capture:
595ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-default
596ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-list
597ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-default ',' capture-list
598ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
599ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture-default:
600ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '&'
601ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '='
602ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
603ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture-list:
604ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture
605ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-list ',' capture
606ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
607ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture:
608ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         identifier
609ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '&' identifier
610ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         'this'
611ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
612ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-declarator:
613ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '(' parameter-declaration-clause ')' attribute-specifier[opt]
614ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///           'mutable'[opt] exception-specification[opt]
615ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///           trailing-return-type[opt]
616ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
617ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::ParseLambdaExpression() {
618ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse lambda-introducer.
619ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  LambdaIntroducer Intro;
620ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
621dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie  Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
622ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (DiagID) {
623ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Diag(Tok, DiagID.getValue());
624ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SkipUntil(tok::r_square);
625dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    SkipUntil(tok::l_brace);
626dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    SkipUntil(tok::r_brace);
627dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprError();
628ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
629ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
630ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return ParseLambdaExpressionAfterIntroducer(Intro);
631ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
632ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
633ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// TryParseLambdaExpression - Use lookahead and potentially tentative
634ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// parsing to determine if we are looking at a C++0x lambda expression, and parse
635ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// it if we are.
636ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
637ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// If we are not looking at a lambda expression, returns ExprError().
638ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::TryParseLambdaExpression() {
63980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  assert(getLangOpts().CPlusPlus11
640ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor         && Tok.is(tok::l_square)
641ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor         && "Not at the start of a possible lambda expression.");
642ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
643ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  const Token Next = NextToken(), After = GetLookAheadToken(2);
644ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
645ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // If lookahead indicates this is a lambda...
646ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Next.is(tok::r_square) ||     // []
647ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Next.is(tok::equal) ||        // [=
648ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (Next.is(tok::amp) &&         // [&] or [&,
649ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor       (After.is(tok::r_square) ||
650ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        After.is(tok::comma))) ||
651ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (Next.is(tok::identifier) &&  // [identifier]
652ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor       After.is(tok::r_square))) {
653ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    return ParseLambdaExpression();
654ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
655ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
656dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // If lookahead indicates an ObjC message send...
657dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // [identifier identifier
658ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Next.is(tok::identifier) && After.is(tok::identifier)) {
659dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprEmpty();
660ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
661ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
662dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // Here, we're stuck: lambda introducers and Objective-C message sends are
663dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // unambiguous, but it requires arbitrary lookhead.  [a,b,c,d,e,f,g] is a
664dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send.  Instead of
665dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // writing two routines to parse a lambda introducer, just try to parse
666dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // a lambda introducer first, and fall back if that fails.
667dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // (TryParseLambdaIntroducer never produces any diagnostic output.)
668ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  LambdaIntroducer Intro;
669ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (TryParseLambdaIntroducer(Intro))
670dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprEmpty();
671ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return ParseLambdaExpressionAfterIntroducer(Intro);
672ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
673ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
674ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// ParseLambdaExpression - Parse a lambda introducer.
675ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
676ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// Returns a DiagnosticID if it hit something unexpected.
677dc84cd5efdd3430efb22546b4ac656aa0540b210David BlaikieOptional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
678dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie  typedef Optional<unsigned> DiagResult;
679ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
680ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
6814a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_square);
6824a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
6834a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
6844a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Intro.Range.setBegin(T.getOpenLocation());
685ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
686ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  bool first = true;
687ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
688ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse capture-default.
689ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Tok.is(tok::amp) &&
690ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
691ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Intro.Default = LCD_ByRef;
6923ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor    Intro.DefaultLoc = ConsumeToken();
693ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    first = false;
694ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  } else if (Tok.is(tok::equal)) {
695ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Intro.Default = LCD_ByCopy;
6963ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor    Intro.DefaultLoc = ConsumeToken();
697ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    first = false;
698ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
699ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
700ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  while (Tok.isNot(tok::r_square)) {
701ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (!first) {
70281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      if (Tok.isNot(tok::comma)) {
703437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // Provide a completion for a lambda introducer here. Except
704437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // in Objective-C, where this is Almost Surely meant to be a message
705437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // send. In that case, fail here and let the ObjC message
706437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // expression parser perform the completion.
707d48ab06b178e400ac31ef4fe649e9c33d2caf651Douglas Gregor        if (Tok.is(tok::code_completion) &&
708d48ab06b178e400ac31ef4fe649e9c33d2caf651Douglas Gregor            !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
709d48ab06b178e400ac31ef4fe649e9c33d2caf651Douglas Gregor              !Intro.Captures.empty())) {
71081f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
71181f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                               /*AfterAmpersand=*/false);
71281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          ConsumeCodeCompletionToken();
71381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          break;
71481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        }
71581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
716ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_expected_comma_or_rsquare);
71781f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      }
718ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      ConsumeToken();
719ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
720ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
72181f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    if (Tok.is(tok::code_completion)) {
72281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      // If we're in Objective-C++ and we have a bare '[', then this is more
72381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      // likely to be a message receiver.
7244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (getLangOpts().ObjC1 && first)
72581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        Actions.CodeCompleteObjCMessageReceiver(getCurScope());
72681f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      else
72781f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
72881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                             /*AfterAmpersand=*/false);
72981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      ConsumeCodeCompletionToken();
73081f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      break;
73181f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    }
732ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
73381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    first = false;
73481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
735ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse capture.
736ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    LambdaCaptureKind Kind = LCK_ByCopy;
737ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation Loc;
738ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    IdentifierInfo* Id = 0;
739a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor    SourceLocation EllipsisLoc;
740a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor
741ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::kw_this)) {
742ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Kind = LCK_This;
743ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Loc = ConsumeToken();
744ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    } else {
745ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Tok.is(tok::amp)) {
746ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Kind = LCK_ByRef;
747ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        ConsumeToken();
74881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
74981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        if (Tok.is(tok::code_completion)) {
75081f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
75181f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                               /*AfterAmpersand=*/true);
75281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          ConsumeCodeCompletionToken();
75381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          break;
75481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        }
755ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      }
756ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
757ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Tok.is(tok::identifier)) {
758ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Id = Tok.getIdentifierInfo();
759ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Loc = ConsumeToken();
760a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor
761a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor        if (Tok.is(tok::ellipsis))
762a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor          EllipsisLoc = ConsumeToken();
763ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      } else if (Tok.is(tok::kw_this)) {
764ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // FIXME: If we want to suggest a fixit here, will need to return more
765ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
766ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // Clear()ed to prevent emission in case of tentative parsing?
767ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_this_captured_by_reference);
768ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      } else {
769ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_expected_capture);
770ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      }
771ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
772ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
773a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor    Intro.addCapture(Kind, Loc, Id, EllipsisLoc);
774ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
775ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
7764a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
7774a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Intro.Range.setEnd(T.getCloseLocation());
778ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
779ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return DiagResult();
780ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
781ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
78281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
783ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
784ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// Returns true if it hit something unexpected.
785ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregorbool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
786ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  TentativeParsingAction PA(*this);
787ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
788dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie  Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
789ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
790ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (DiagID) {
791ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    PA.Revert();
792ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    return true;
793ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
794ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
795ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  PA.Commit();
796ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return false;
797ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
798ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
799ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
800ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// expression.
801ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::ParseLambdaExpressionAfterIntroducer(
802ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                     LambdaIntroducer &Intro) {
803dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
804dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
805dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
806dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
807dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman                                "lambda expression parsing");
808dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
809ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse lambda-declarator[opt].
810ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  DeclSpec DS(AttrFactory);
811f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman  Declarator D(DS, Declarator::LambdaExprContext);
812ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
813ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Tok.is(tok::l_paren)) {
814ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ParseScope PrototypeScope(this,
815ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                              Scope::FunctionPrototypeScope |
8163a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith                              Scope::FunctionDeclarationScope |
817ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                              Scope::DeclScope);
818ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
81959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation DeclEndLoc;
8204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
8214a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
82259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation LParenLoc = T.getOpenLocation();
823ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
824ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse parameter-declaration-clause.
825ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ParsedAttributes Attr(AttrFactory);
826cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
827ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation EllipsisLoc;
828ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
829ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.isNot(tok::r_paren))
830ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
831ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
8324a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
83359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation RParenLoc = T.getCloseLocation();
83459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    DeclEndLoc = RParenLoc;
835ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
836ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse 'mutable'[opt].
837ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation MutableLoc;
838ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::kw_mutable)) {
839ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      MutableLoc = ConsumeToken();
840ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      DeclEndLoc = MutableLoc;
841ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
842ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
843ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse exception-specification[opt].
844ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ExceptionSpecificationType ESpecType = EST_None;
845ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceRange ESpecRange;
846cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<ParsedType, 2> DynamicExceptions;
847cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<SourceRange, 2> DynamicExceptionRanges;
848ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ExprResult NoexceptExpr;
849a058fd4f0a944174295f77169b438510dad389f8Richard Smith    ESpecType = tryParseExceptionSpecification(ESpecRange,
85074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                               DynamicExceptions,
85174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                               DynamicExceptionRanges,
852a058fd4f0a944174295f77169b438510dad389f8Richard Smith                                               NoexceptExpr);
853ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
854ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (ESpecType != EST_None)
855ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      DeclEndLoc = ESpecRange.getEnd();
856ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
857ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse attribute-specifier[opt].
8584e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
859ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
86059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation FunLocalRangeEnd = DeclEndLoc;
86159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara
862ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse trailing-return-type[opt].
86354655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith    TypeResult TrailingReturnType;
864ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::arrow)) {
86559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      FunLocalRangeEnd = Tok.getLocation();
866ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      SourceRange Range;
86754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith      TrailingReturnType = ParseTrailingReturnType(Range);
868ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Range.getEnd().isValid())
869ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        DeclEndLoc = Range.getEnd();
870ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
871ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
872ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    PrototypeScope.Exit();
873ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
87459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation NoLoc;
875ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
87659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*isAmbiguous=*/false,
87759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           LParenLoc,
878ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           ParamInfo.data(), ParamInfo.size(),
87959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           EllipsisLoc, RParenLoc,
880ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DS.getTypeQualifiers(),
881ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           /*RefQualifierIsLValueRef=*/true,
88259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*RefQualifierLoc=*/NoLoc,
88359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*ConstQualifierLoc=*/NoLoc,
88459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*VolatileQualifierLoc=*/NoLoc,
885ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           MutableLoc,
886ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           ESpecType, ESpecRange.getBegin(),
887ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptions.data(),
888ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptionRanges.data(),
889ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptions.size(),
890ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           NoexceptExpr.isUsable() ?
891ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                             NoexceptExpr.get() : 0,
89259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           LParenLoc, FunLocalRangeEnd, D,
893ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           TrailingReturnType),
894ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                  Attr, DeclEndLoc);
895c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor  } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
896c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    // It's common to forget that one needs '()' before 'mutable' or the
897c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    // result type. Deal with this.
898c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    Diag(Tok, diag::err_lambda_missing_parens)
899c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      << Tok.is(tok::arrow)
900c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
901c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    SourceLocation DeclLoc = Tok.getLocation();
902c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    SourceLocation DeclEndLoc = DeclLoc;
903c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor
904c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    // Parse 'mutable', if it's there.
905c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    SourceLocation MutableLoc;
906c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    if (Tok.is(tok::kw_mutable)) {
907c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      MutableLoc = ConsumeToken();
908c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      DeclEndLoc = MutableLoc;
909c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    }
910c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor
911c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    // Parse the return type, if there is one.
91254655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith    TypeResult TrailingReturnType;
913c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    if (Tok.is(tok::arrow)) {
914c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      SourceRange Range;
91554655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith      TrailingReturnType = ParseTrailingReturnType(Range);
916c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      if (Range.getEnd().isValid())
917c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor        DeclEndLoc = Range.getEnd();
918c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    }
919c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor
920c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    ParsedAttributes Attr(AttrFactory);
92159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation NoLoc;
922c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
92359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*isAmbiguous=*/false,
92459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*LParenLoc=*/NoLoc,
92559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*Params=*/0,
92659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*NumParams=*/0,
92759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*EllipsisLoc=*/NoLoc,
92859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*RParenLoc=*/NoLoc,
92959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*TypeQuals=*/0,
93059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*RefQualifierIsLValueRef=*/true,
93159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*RefQualifierLoc=*/NoLoc,
93259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*ConstQualifierLoc=*/NoLoc,
93359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*VolatileQualifierLoc=*/NoLoc,
93459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               MutableLoc,
93559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               EST_None,
93659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*ESpecLoc=*/NoLoc,
93759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*Exceptions=*/0,
93859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*ExceptionRanges=*/0,
93959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*NumExceptions=*/0,
94059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*NoexceptExpr=*/0,
94159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               DeclLoc, DeclEndLoc, D,
94259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               TrailingReturnType),
943c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor                  Attr, DeclEndLoc);
944ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
945c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor
946ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
947906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
948906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  // it.
949fccfb625e3090e77da9b6a79edcab159c7006685Douglas Gregor  unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
950fccfb625e3090e77da9b6a79edcab159c7006685Douglas Gregor  ParseScope BodyScope(this, ScopeFlags);
951906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman
952ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman  Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
953ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman
954ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse compound-statement.
955dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  if (!Tok.is(tok::l_brace)) {
956ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Diag(Tok, diag::err_expected_lambda_body);
957dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
958dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprError();
959ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
960ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
961dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  StmtResult Stmt(ParseCompoundStatementBody());
962dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  BodyScope.Exit();
963dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
964deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  if (!Stmt.isInvalid())
9659e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor    return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
966dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
967deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
968deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  return ExprError();
969ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
970ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type.
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       postfix-expression: [C++ 5.2p1]
9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'dynamic_cast' '<' type-name '>' '(' expression ')'
9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'static_cast' '<' type-name '>' '(' expression ')'
9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'const_cast' '<' type-name '>' '(' expression ')'
9795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
98060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXCasts() {
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CastName = 0;     // For error messages
9835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Kind) {
985eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie  default: llvm_unreachable("Unknown C++ cast!");
9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_const_cast:       CastName = "const_cast";       break;
9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
9885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_static_cast:      CastName = "static_cast";      break;
9905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation OpLoc = ConsumeToken();
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LAngleBracketLoc = Tok.getLocation();
9945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
995ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Check for "<::" which is parsed as "[:".  If found, fix token stream,
996ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // diagnose error, suggest fix, and recover parsing.
99778fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith  if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
99878fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith    Token Next = NextToken();
99978fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith    if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
100078fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith      FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
100178fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith  }
1002ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
10035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
100420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
100631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Parse the common declaration-specifiers piece.
100731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclSpec DS(AttrFactory);
100831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
100931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
101031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Parse the abstract-declarator, if present.
101131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
101231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
101331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
10145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation RAngleBracketLoc = Tok.getLocation();
10155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10161ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
101720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
10185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  SourceLocation LParenLoc, RParenLoc;
10204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
102321e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    return ExprError();
10245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
102560d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = ParseExpression();
10261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
102721e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  // Match the ')'.
10284a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
10295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
103031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
103149badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
103231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                       LAngleBracketLoc, DeclaratorInfo,
1033809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                       RAngleBracketLoc,
10344a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getOpenLocation(), Result.take(),
10354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
10365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10373fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1040c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression.
1041c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
1042c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///       postfix-expression: [C++ 5.2p1]
1043c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' expression ')'
1044c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' type-id ')'
1045c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
104660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXTypeid() {
1047c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1048c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1049c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation OpLoc = ConsumeToken();
10504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  SourceLocation LParenLoc, RParenLoc;
10514a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
1052c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1053c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  // typeid expressions are always parenthesized.
10544a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
105520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
10564a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  LParenLoc = T.getOpenLocation();
1057c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
105860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result;
1059c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
10600576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // C++0x [expr.typeid]p3:
10610576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //   When typeid is applied to an expression other than an lvalue of a
10620576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //   polymorphic class type [...] The expression is an unevaluated
10630576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //   operand (Clause 5).
10640576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //
10650576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Note that we can't tell whether the expression is an lvalue of a
10660576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // polymorphic class type until after we've parsed the expression; we
10670576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // speculatively assume the subexpression is unevaluated, and fix it up
10680576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // later.
10690576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //
10700576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // We enter the unevaluated context before trying to determine whether we
10710576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // have a type-id, because the tentative parse logic will try to resolve
10720576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // names, and must treat them as unevaluated.
107380bfa3d125fa0b9c636977ea37b4a55b2c9b1037Eli Friedman  EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
107480bfa3d125fa0b9c636977ea37b4a55b2c9b1037Eli Friedman                                               Sema::ReuseLambdaContextDecl);
10750576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
1076c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (isTypeIdInParens()) {
1077809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult Ty = ParseTypeName();
1078c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1079c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
10804a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
10814a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    RParenLoc = T.getCloseLocation();
10824eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    if (Ty.isInvalid() || RParenLoc.isInvalid())
108320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1084c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1085c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
1086b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                    Ty.get().getAsOpaquePtr(), RParenLoc);
1087c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  } else {
1088c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = ParseExpression();
1089c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1090c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
10910e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Result.isInvalid())
1092c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      SkipUntil(tok::r_paren);
1093c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    else {
10944a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
10954a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      RParenLoc = T.getCloseLocation();
10964eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor      if (RParenLoc.isInvalid())
10974eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor        return ExprError();
1098fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor
1099c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
1100effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl                                      Result.release(), RParenLoc);
1101c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    }
1102c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  }
1103c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
11043fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
1105c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
1106c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
110701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
110801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
110901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' expression ')'
111001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' type-id ')'
111101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
111201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetExprResult Parser::ParseCXXUuidof() {
111301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
111401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
111501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  SourceLocation OpLoc = ConsumeToken();
11164a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
111701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
111801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  // __uuidof expressions are always parenthesized.
11194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
112001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    return ExprError();
112101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
112201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  ExprResult Result;
112301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
112401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  if (isTypeIdInParens()) {
112501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    TypeResult Ty = ParseTypeName();
112601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
112701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
11284a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
112901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
113001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Ty.isInvalid())
113101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      return ExprError();
113201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
11334a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
11344a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    Ty.get().getAsOpaquePtr(),
11354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    T.getCloseLocation());
113601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  } else {
113701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
113801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    Result = ParseExpression();
113901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
114001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
114101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Result.isInvalid())
114201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      SkipUntil(tok::r_paren);
114301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    else {
11444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
114501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
11464a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
11474a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      /*isType=*/false,
11484a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      Result.release(), T.getCloseLocation());
114901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    }
115001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  }
115101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
11523fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
115301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet}
115401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
1155d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \brief Parse a C++ pseudo-destructor expression after the base,
1156d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// . or -> operator, and nested-name-specifier have already been
1157d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// parsed.
1158d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
1159d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       postfix-expression: [C++ 5.2]
1160d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression . pseudo-destructor-name
1161d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression -> pseudo-destructor-name
1162d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
1163d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       pseudo-destructor-name:
1164d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1165d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier template simple-template-id ::
1166d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///                 ~type-name
1167d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] ~type-name
1168d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
116960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
1170d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1171d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 tok::TokenKind OpKind,
1172d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 CXXScopeSpec &SS,
1173b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                 ParsedType ObjectType) {
1174d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // We're parsing either a pseudo-destructor-name or a dependent
1175d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // member access that has the same form as a
1176d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // pseudo-destructor-name. We parse both in the same way and let
1177d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // the action model sort them out.
1178d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  //
1179d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Note that the ::[opt] nested-name-specifier[opt] has already
1180d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been parsed, and if there was a simple-template-id, it has
1181d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been coalesced into a template-id annotation token.
1182d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId FirstTypeName;
1183d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation CCLoc;
1184d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::identifier)) {
1185d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1186d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
1187d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1188d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
1189d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else if (Tok.is(tok::annot_template_id)) {
1190e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    // FIXME: retrieve TemplateKWLoc from template-id annotation and
1191e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    // store it in the pseudo-dtor node (to be used when instantiating it).
1192d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setTemplateId(
1193d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                              (TemplateIdAnnotation *)Tok.getAnnotationValue());
1194d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
1195d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1196d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
1197d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else {
1198d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(0, SourceLocation());
1199d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
1200d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1201d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the tilde.
1202d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1203d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation TildeLoc = ConsumeToken();
120491ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
120591ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
120691ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    DeclSpec DS(AttrFactory);
120785c60db2131c6d210d4777c3d50bdaf0e69bb8bfBenjamin Kramer    ParseDecltypeSpecifier(DS);
120891ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    if (DS.getTypeSpecType() == TST_error)
120991ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie      return ExprError();
121091ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
121191ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                             OpKind, TildeLoc, DS,
121291ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                             Tok.is(tok::l_paren));
121391ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  }
121491ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
1215d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (!Tok.is(tok::identifier)) {
1216d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    Diag(Tok, diag::err_destructor_tilde_identifier);
1217d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
1218d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
1219d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1220d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the second type.
1221d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId SecondTypeName;
1222d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  IdentifierInfo *Name = Tok.getIdentifierInfo();
1223d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation NameLoc = ConsumeToken();
1224d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SecondTypeName.setIdentifier(Name, NameLoc);
1225d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1226d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // If there is a '<', the second type name is a template-id. Parse
1227d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // it as such.
1228d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::less) &&
1229e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1230e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   Name, NameLoc,
1231e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   false, ObjectType, SecondTypeName,
1232e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   /*AssumeTemplateName=*/true))
1233d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
1234d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
12359ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
12369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                           OpLoc, OpKind,
1237d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           SS, FirstTypeName, CCLoc,
1238d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           TildeLoc, SecondTypeName,
1239d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           Tok.is(tok::l_paren));
1240d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor}
1241d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       boolean-literal: [C++ 2.13.5]
12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'true'
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'false'
124760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXBoolLiteral() {
12485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
1249f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
12505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
125150dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
125250dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression.
125350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///
125450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///       throw-expression: [C++ 15]
125550dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///         'throw' assignment-expression[opt]
125660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseThrowExpression() {
125750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  assert(Tok.is(tok::kw_throw) && "Not throw!");
125850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
125920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl
12602a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // If the current token isn't the start of an assignment-expression,
12612a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // then the expression is not present.  This handles things like:
12622a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  //   "C ? throw : (void)42", which is crazy but legal.
12632a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
12642a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::semi:
12652a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_paren:
12662a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_square:
12672a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_brace:
12682a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::colon:
12692a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::comma:
1270bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
127150dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
12722a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  default:
127360d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Expr(ParseAssignmentExpression());
12743fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    if (Expr.isInvalid()) return Expr;
1275bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
12762a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  }
127750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
12784cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
12794cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer.
12804cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
12814cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
12824cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which
12834cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called.
128460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXThis() {
12854cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  assert(Tok.is(tok::kw_this) && "Not 'this'!");
12864cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  SourceLocation ThisLoc = ConsumeToken();
1287f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXThis(ThisLoc);
12884cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
1289987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1290987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1291987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)")
1292987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)")
1293987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()").
1294dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// See [C++ 5.2.3].
1295987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1296987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       postfix-expression: [C++ 5.2p1]
1297dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl///         simple-type-specifier '(' expression-list[opt] ')'
1298dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] simple-type-specifier braced-init-list
1299dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl///         typename-specifier '(' expression-list[opt] ')'
1300dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] typename-specifier braced-init-list
1301987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
130260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
130320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
1304987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1305b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
1306987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1307dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  assert((Tok.is(tok::l_paren) ||
130880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
1309dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl         && "Expected '(' or '{'!");
1310bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor
1311dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  if (Tok.is(tok::l_brace)) {
13126dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    ExprResult Init = ParseBraceInitializer();
13136dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    if (Init.isInvalid())
13146dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl      return Init;
13156dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    Expr *InitList = Init.take();
13166dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
13176dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl                                             MultiExprArg(&InitList, 1),
13186dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl                                             SourceLocation());
1319dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  } else {
13204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
13214a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
1322dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
13234e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer    ExprVector Exprs;
1324dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    CommaLocsTy CommaLocs;
1325dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
1326dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (Tok.isNot(tok::r_paren)) {
1327dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      if (ParseExpressionList(Exprs, CommaLocs)) {
1328dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl        SkipUntil(tok::r_paren);
1329dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl        return ExprError();
1330dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      }
1331987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    }
1332987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1333dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // Match the ')'.
13344a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
1335987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1336dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // TypeRep could be null, if it references an invalid typedef.
1337dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (!TypeRep)
1338dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      return ExprError();
1339ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl
1340dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1341dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl           "Unexpected number of commas!");
13424a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
13433fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                             Exprs,
13444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                             T.getCloseLocation());
1345dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  }
1346987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
1347987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
134899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// ParseCXXCondition - if/switch/while condition expression.
134971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
135071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///       condition:
135171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         expression
135271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         type-specifier-seq declarator '=' assignment-expression
13530635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith/// [C++11] type-specifier-seq declarator '=' initializer-clause
13540635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith/// [C++11] type-specifier-seq declarator braced-init-list
135571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
135671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///             '=' assignment-expression
135771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
13581ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param ExprOut if the condition was parsed as an expression, the parsed
13591ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// expression.
136099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
13611ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param DeclOut if the condition was parsed as a declaration, the parsed
13621ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// declaration.
136399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
1364586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param Loc The location of the start of the statement that requires this
1365586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// condition, e.g., the "for" in a for loop.
1366586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
1367586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param ConvertToBoolean Whether the condition expression should be
1368586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// converted to a boolean value.
1369586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
137099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \returns true if there was a parsing, false otherwise.
137160d7b3a319d84d688752be3870615ac0f111fb16John McCallbool Parser::ParseCXXCondition(ExprResult &ExprOut,
137260d7b3a319d84d688752be3870615ac0f111fb16John McCall                               Decl *&DeclOut,
1373586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               SourceLocation Loc,
1374586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               bool ConvertToBoolean) {
137501dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  if (Tok.is(tok::code_completion)) {
1376f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
13777d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
13787d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return true;
137901dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  }
138001dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor
13812edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  ParsedAttributesWithRange attrs(AttrFactory);
13824e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  MaybeParseCXX11Attributes(attrs);
13832edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
138499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!isCXXConditionDeclaration()) {
13852edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ProhibitAttributes(attrs);
13862edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
1387586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // Parse the expression.
138860d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprOut = ParseExpression(); // expression
138960d7b3a319d84d688752be3870615ac0f111fb16John McCall    DeclOut = 0;
139060d7b3a319d84d688752be3870615ac0f111fb16John McCall    if (ExprOut.isInvalid())
1391586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return true;
1392586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
1393586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // If required, convert to a boolean value.
1394586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    if (ConvertToBoolean)
139560d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprOut
139660d7b3a319d84d688752be3870615ac0f111fb16John McCall        = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
139760d7b3a319d84d688752be3870615ac0f111fb16John McCall    return ExprOut.isInvalid();
139899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
139971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
140071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // type-specifier-seq
14010b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
14026b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  DS.takeAttributesFrom(attrs);
140371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
140471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
140571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // declarator
140671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
140771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
140871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
140971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // simple-asm-expr[opt]
141071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.is(tok::kw_asm)) {
1411ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
141260d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult AsmLabel(ParseSimpleAsm(&Loc));
14130e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (AsmLabel.isInvalid()) {
141471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis      SkipUntil(tok::semi);
141599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return true;
141671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    }
1417effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    DeclaratorInfo.setAsmLabel(AsmLabel.release());
1418ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.SetRangeEnd(Loc);
141971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  }
142071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
142171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // If attributes are present, parse them.
14227f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseGNUAttributes(DeclaratorInfo);
142371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
142499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  // Type-check the declaration itself.
142560d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
14267f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                        DeclaratorInfo);
142760d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclOut = Dcl.get();
142860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprOut = ExprError();
1429a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
143071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // '=' assignment-expression
1431d6c7c67313634b317a0d63c32be0511a121bb33dRichard Trieu  // If a '==' or '+=' is found, suggest a fixit to '='.
14320635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  bool CopyInitialization = isTokenEqualOrEqualTypo();
14330635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  if (CopyInitialization)
1434dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeToken();
14350635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith
14360635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  ExprResult InitExpr = ExprError();
143780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
14380635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Diag(Tok.getLocation(),
14390635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith         diag::warn_cxx98_compat_generalized_initializer_lists);
14400635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    InitExpr = ParseBraceInitializer();
14410635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  } else if (CopyInitialization) {
14420635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    InitExpr = ParseAssignmentExpression();
14430635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  } else if (Tok.is(tok::l_paren)) {
14440635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    // This was probably an attempt to initialize the variable.
14450635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    SourceLocation LParen = ConsumeParen(), RParen = LParen;
14460635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
14470635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith      RParen = ConsumeParen();
14480635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Diag(DeclOut ? DeclOut->getLocation() : LParen,
14490635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith         diag::err_expected_init_in_condition_lparen)
14500635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith      << SourceRange(LParen, RParen);
145199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  } else {
14520635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
14530635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith         diag::err_expected_init_in_condition);
145499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
14550635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith
14560635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  if (!InitExpr.isInvalid())
14570635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
14580635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith                                 DS.getTypeSpecType() == DeclSpec::TST_auto);
14590635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith
1460586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // FIXME: Build a reference to this declaration? Convert it to bool?
1461586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // (This is currently handled by Sema).
1462483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
1463483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Actions.FinalizeDeclaration(DeclOut);
1464586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
146599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return false;
146671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis}
146771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
1468987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1469987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of
1470987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier.
1471987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1472987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       simple-type-specifier:
1473eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier[opt] type-name
1474987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1475987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         char
1476987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         wchar_t
1477987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         bool
1478987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         short
1479987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         int
1480987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         long
1481987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         signed
1482987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         unsigned
1483987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         float
1484987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         double
1485987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         void
1486987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU]   typeof-specifier
1487987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto               [TODO]
1488987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1489987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       type-name:
1490987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         class-name
1491987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         enum-name
1492987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typedef-name
1493987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1494987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1495987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  DS.SetRangeStart(Tok.getLocation());
1496987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  const char *PrevSpec;
1497fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
1498987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation Loc = Tok.getLocation();
14991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1500987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  switch (Tok.getKind()) {
150155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::identifier:   // foo::bar
150255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::coloncolon:   // ::foo::bar
1503b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Annotation token should already be formed!");
15041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
1505b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Not a simple-type-specifier token!");
150655a7cefc846765ac7d142a63f773747a20518d71Chris Lattner
1507987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // type-name
1508b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  case tok::annot_typename: {
15096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (getTypeAnnotation(Tok))
15106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
15116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                         getTypeAnnotation(Tok));
15126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    else
15136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecError();
15149bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
15159bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
15169bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    ConsumeToken();
15179bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
15189bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
15199bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
15209bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C interface.  If we don't have Objective-C or a '<', this is
15219bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // just a normal reference to a typedef name.
15224e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Tok.is(tok::less) && getLangOpts().ObjC1)
15239bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor      ParseObjCProtocolQualifiers(DS);
15249bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
15259bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.Finish(Diags, PP);
15269bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    return;
1527987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
15281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1529987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // builtin types
1530987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_short:
1531fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
1532987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1533987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_long:
1534fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
1535338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet    break;
1536338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet  case tok::kw___int64:
1537338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet    DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1538987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1539987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_signed:
1540fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
1541987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1542987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_unsigned:
1543fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
1544987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1545987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_void:
1546fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
1547987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1548987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_char:
1549fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
1550987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1551987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_int:
1552fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
1553987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
15545a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case tok::kw___int128:
15555a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
15565a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
1557aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case tok::kw_half:
1558aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1559aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    break;
1560987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_float:
1561fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
1562987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1563987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_double:
1564fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
1565987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1566987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_wchar_t:
1567fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
1568987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1569f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char16_t:
1570fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
1571f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1572f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char32_t:
1573fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
1574f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1575987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_bool:
1576fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
1577987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
15785e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie  case tok::annot_decltype:
15795e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie  case tok::kw_decltype:
15805e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie    DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
15815e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie    return DS.Finish(Diags, PP);
15821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1583987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // GNU typeof support.
1584987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_typeof:
1585987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    ParseTypeofSpecifier(DS);
15869b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor    DS.Finish(Diags, PP);
1587987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    return;
1588987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
1589b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  if (Tok.is(tok::annot_typename))
1590eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1591eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  else
1592eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getLocation());
1593987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  ConsumeToken();
15949b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor  DS.Finish(Diags, PP);
1595987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
15961cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
15972f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
15982f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers,
15992f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished
16002f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are
16012f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and
16022f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false
16032f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise.
16042f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
16052f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///   type-specifier-seq: [C++ 8.1]
16062f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///     type-specifier type-specifier-seq[opt]
16072f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
16082f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
160969730c115c2d0fec2f20609d905d920a5a41b29bRichard Smith  ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
1610396a9f235e160093b5f803f7a6a18fad7b68bdbeDouglas Gregor  DS.Finish(Diags, PP);
16112f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  return false;
16122f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
16132f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
16143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of
16153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form.
16163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
16173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or
16183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
16193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will
16203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to
16213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller.
16223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
16233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if
16243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id.
16253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
16263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual
16273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name.
16283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
16293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or
16303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor.
16313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
16323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the
16333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
16343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
163546df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
163646df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
163746df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
16383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id
16393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully,
16403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id.
16413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1642d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param AssumeTemplateId When true, this routine will assume that the name
1643d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// refers to a template without performing name lookup to verify.
1644d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
16453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise.
16463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1647e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          SourceLocation TemplateKWLoc,
16483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          IdentifierInfo *Name,
16493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          SourceLocation NameLoc,
16503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          bool EnteringContext,
1651b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          ParsedType ObjectType,
1652d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                          UnqualifiedId &Id,
1653e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          bool AssumeTemplateId) {
16540278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  assert((AssumeTemplateId || Tok.is(tok::less)) &&
16550278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor         "Expected '<' to finish parsing a template-id");
16563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateTy Template;
16583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateNameKind TNK = TNK_Non_template;
16593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (Id.getKind()) {
16603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
1661014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
1662e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
1663d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    if (AssumeTemplateId) {
1664e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
1665d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Id, ObjectType, EnteringContext,
1666d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Template);
1667d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
1668d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        return true;
16691fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    } else {
16701fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
16717c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS,
16727c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateKWLoc.isValid(), Id,
16737c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   ObjectType, EnteringContext, Template,
16741fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
16751fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor
16761fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
16771fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          ObjectType && IsTemplateArgumentList()) {
16781fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // We have something like t->getAs<T>(), where getAs is a
16791fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // member of an unknown specialization. However, this will only
16801fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
16811fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
16821fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        std::string Name;
16831fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        if (Id.getKind() == UnqualifiedId::IK_Identifier)
16841fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = Id.Identifier->getName();
16851fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        else {
16861fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = "operator ";
16871fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
16881fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
16891fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          else
16901fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += Id.Identifier->getName();
16911fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        }
16921fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
16931fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << Name
16941fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << FixItHint::CreateInsertion(Id.StartLocation, "template ");
1695e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1696e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 SS, TemplateKWLoc, Id,
1697e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 ObjectType, EnteringContext,
1698e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 Template);
1699d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TNK == TNK_Non_template)
17001fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          return true;
17011fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      }
17021fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    }
17033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
17043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1705014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_ConstructorName: {
1706014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
17071fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1708014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
17097c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara    TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
17107c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                 TemplateName, ObjectType,
17111fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 EnteringContext, Template,
17121fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 MemberOfUnknownSpecialization);
17133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1714014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
17153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1716014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_DestructorName: {
1717014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
17181fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1719014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
17202d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (ObjectType) {
1721e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1722e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               SS, TemplateKWLoc, TemplateName,
1723e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               ObjectType, EnteringContext,
1724e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               Template);
1725d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
17262d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
17272d1c21414199a7452f122598189363a3922605b1Douglas Gregor    } else {
17287c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
17297c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateName, ObjectType,
17301fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   EnteringContext, Template,
17311fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
17322d1c21414199a7452f122598189363a3922605b1Douglas Gregor
1733b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
1734124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        Diag(NameLoc, diag::err_destructor_template_id)
1735124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          << Name << SS.getRange();
17362d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
17372d1c21414199a7452f122598189363a3922605b1Douglas Gregor      }
17382d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
17393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1740014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
17413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  default:
17433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
17443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
17453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (TNK == TNK_Non_template)
17473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
17483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Parse the enclosed template argument list.
17503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
17513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgList TemplateArgs;
17520278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (Tok.is(tok::less) &&
17530278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
1754059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                       SS, true, LAngleLoc,
17553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgs,
17563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       RAngleLoc))
17573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
17583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
1760e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1761e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
17623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Form a parsed representation of the template-id to be stored in the
17633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // UnqualifiedId.
17643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateIdAnnotation *TemplateId
176513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
17663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
17683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->Name = Id.Identifier;
1769014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = OO_None;
17703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
17713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
1772014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Name = 0;
1773014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = Id.OperatorFunctionId.Operator;
1774014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
17753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
17763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1777059101f922de6eb765601459925f4c8914420b23Douglas Gregor    TemplateId->SS = SS;
17782b28bf1a8fa6e1c598805374f29e4fbf45e751feBenjamin Kramer    TemplateId->TemplateKWLoc = TemplateKWLoc;
17792b5289b6fd7e3d9899868410a498c081c9595662John McCall    TemplateId->Template = Template;
17803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Kind = TNK;
17813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->LAngleLoc = LAngleLoc;
17823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->RAngleLoc = RAngleLoc;
1783314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
17843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
1785314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor         Arg != ArgEnd; ++Arg)
17863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Args[Arg] = TemplateArgs[Arg];
17873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setTemplateId(TemplateId);
17893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
17903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
17913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Bundle the template arguments together.
17935354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
1794fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara
17953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Constructor and destructor names.
1796f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Type
179755d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
179855d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara                                  Template, NameLoc,
1799fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                  LAngleLoc, TemplateArgsPtr, RAngleLoc,
1800fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                  /*IsCtorOrDtorName=*/true);
18013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Type.isInvalid())
18023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
18033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
18043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
18053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
18063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  else
18073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
18083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
18093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return false;
18103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
18113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1812ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part
1813ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id.
18143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1815ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or
1816ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way.
18173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1818ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
18193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       operator-function-id: [C++ 13.5]
18203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         'operator' operator
18213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1822ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       operator: one of
18233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            new   delete  new[]   delete[]
18243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            +     -    *  /    %  ^    &   |   ~
18253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            !     =    <  >    += -=   *=  /=  %=
18263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
18273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
18283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ()    []
18293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
18303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-function-id: [C++ 12.3.2]
18313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator conversion-type-id
18323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
18333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-type-id:
18343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         type-specifier-seq conversion-declarator[opt]
18353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
18363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-declarator:
18373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ptr-operator conversion-declarator[opt]
18383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode
18393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
18401ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param SS The nested-name-specifier that preceded this unqualified-id. If
18413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
18423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
18433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the
18443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
18453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1846ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
1847ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed.
1848ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1849ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
1850ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1851ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise.
1852ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1853b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType ObjectType,
1854ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        UnqualifiedId &Result) {
1855ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1856ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1857ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Consume the 'operator' keyword.
1858ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation KeywordLoc = ConsumeToken();
1859ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1860ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Determine what kind of operator name we have.
1861ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  unsigned SymbolIdx = 0;
1862ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation SymbolLocations[3];
1863ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  OverloadedOperatorKind Op = OO_None;
1864ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  switch (Tok.getKind()) {
1865ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_new:
1866ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_delete: {
1867ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      bool isNew = Tok.getKind() == tok::kw_new;
1868ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the 'new' or 'delete'.
1869ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();
18706ee326af4e77e6f05973486097884d7431f2108dRichard Smith      // Check for array new/delete.
18716ee326af4e77e6f05973486097884d7431f2108dRichard Smith      if (Tok.is(tok::l_square) &&
187280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
18734a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        // Consume the '[' and ']'.
18744a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        BalancedDelimiterTracker T(*this, tok::l_square);
18754a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeOpen();
18764a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeClose();
18774a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        if (T.getCloseLocation().isInvalid())
1878ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          return true;
1879ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
18804a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        SymbolLocations[SymbolIdx++] = T.getOpenLocation();
18814a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        SymbolLocations[SymbolIdx++] = T.getCloseLocation();
1882ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_Array_New : OO_Array_Delete;
1883ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
1884ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_New : OO_Delete;
1885ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
1886ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1887ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1888ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1889ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1890ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::Token:                                                     \
1891ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
1892ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_##Name;                                                    \
1893ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1894ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1895ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def"
1896ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1897ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_paren: {
18984a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      // Consume the '(' and ')'.
18994a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      BalancedDelimiterTracker T(*this, tok::l_paren);
19004a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeOpen();
19014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
19024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      if (T.getCloseLocation().isInvalid())
1903ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1904ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
19054a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getOpenLocation();
19064a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getCloseLocation();
1907ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Call;
1908ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1909ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1910ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1911ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_square: {
19124a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      // Consume the '[' and ']'.
19134a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      BalancedDelimiterTracker T(*this, tok::l_square);
19144a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeOpen();
19154a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
19164a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      if (T.getCloseLocation().isInvalid())
1917ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1918ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
19194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getOpenLocation();
19204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getCloseLocation();
1921ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Subscript;
1922ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1923ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1924ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1925ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::code_completion: {
1926ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Code completion for the operator name.
192723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOperatorName(getCurScope());
19287d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
1929ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Don't try to parse any further.
1930ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return true;
1931ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1932ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1933ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    default:
1934ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1935ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
1936ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1937ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Op != OO_None) {
1938ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // We have parsed an operator-function-id.
1939ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1940ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return false;
1941ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
19420486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
19430486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  // Parse a literal-operator-id.
19440486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //
1945aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith  //   literal-operator-id: C++11 [over.literal]
1946aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith  //     operator string-literal identifier
1947aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith  //     operator user-defined-string-literal
19480486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
194980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
19507fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith    Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
195133762775706e81c17ca774102ceda36049ecc593Richard Smith
195233762775706e81c17ca774102ceda36049ecc593Richard Smith    SourceLocation DiagLoc;
195333762775706e81c17ca774102ceda36049ecc593Richard Smith    unsigned DiagId = 0;
195433762775706e81c17ca774102ceda36049ecc593Richard Smith
195533762775706e81c17ca774102ceda36049ecc593Richard Smith    // We're past translation phase 6, so perform string literal concatenation
195633762775706e81c17ca774102ceda36049ecc593Richard Smith    // before checking for "".
1957cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<Token, 4> Toks;
1958cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<SourceLocation, 4> TokLocs;
195933762775706e81c17ca774102ceda36049ecc593Richard Smith    while (isTokenStringLiteral()) {
196033762775706e81c17ca774102ceda36049ecc593Richard Smith      if (!Tok.is(tok::string_literal) && !DiagId) {
1961aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith        // C++11 [over.literal]p1:
1962aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith        //   The string-literal or user-defined-string-literal in a
1963aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith        //   literal-operator-id shall have no encoding-prefix [...].
196433762775706e81c17ca774102ceda36049ecc593Richard Smith        DiagLoc = Tok.getLocation();
196533762775706e81c17ca774102ceda36049ecc593Richard Smith        DiagId = diag::err_literal_operator_string_prefix;
196633762775706e81c17ca774102ceda36049ecc593Richard Smith      }
196733762775706e81c17ca774102ceda36049ecc593Richard Smith      Toks.push_back(Tok);
196833762775706e81c17ca774102ceda36049ecc593Richard Smith      TokLocs.push_back(ConsumeStringToken());
196999831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    }
19700486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
197133762775706e81c17ca774102ceda36049ecc593Richard Smith    StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
197233762775706e81c17ca774102ceda36049ecc593Richard Smith    if (Literal.hadError)
197333762775706e81c17ca774102ceda36049ecc593Richard Smith      return true;
197433762775706e81c17ca774102ceda36049ecc593Richard Smith
197533762775706e81c17ca774102ceda36049ecc593Richard Smith    // Grab the literal operator's suffix, which will be either the next token
197633762775706e81c17ca774102ceda36049ecc593Richard Smith    // or a ud-suffix from the string literal.
197733762775706e81c17ca774102ceda36049ecc593Richard Smith    IdentifierInfo *II = 0;
197833762775706e81c17ca774102ceda36049ecc593Richard Smith    SourceLocation SuffixLoc;
197933762775706e81c17ca774102ceda36049ecc593Richard Smith    if (!Literal.getUDSuffix().empty()) {
198033762775706e81c17ca774102ceda36049ecc593Richard Smith      II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
198133762775706e81c17ca774102ceda36049ecc593Richard Smith      SuffixLoc =
198233762775706e81c17ca774102ceda36049ecc593Richard Smith        Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
198333762775706e81c17ca774102ceda36049ecc593Richard Smith                                       Literal.getUDSuffixOffset(),
19844e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                       PP.getSourceManager(), getLangOpts());
198533762775706e81c17ca774102ceda36049ecc593Richard Smith    } else if (Tok.is(tok::identifier)) {
198633762775706e81c17ca774102ceda36049ecc593Richard Smith      II = Tok.getIdentifierInfo();
198733762775706e81c17ca774102ceda36049ecc593Richard Smith      SuffixLoc = ConsumeToken();
198833762775706e81c17ca774102ceda36049ecc593Richard Smith      TokLocs.push_back(SuffixLoc);
198933762775706e81c17ca774102ceda36049ecc593Richard Smith    } else {
19900486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_expected_ident);
19910486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      return true;
19920486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    }
19930486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
199433762775706e81c17ca774102ceda36049ecc593Richard Smith    // The string literal must be empty.
199533762775706e81c17ca774102ceda36049ecc593Richard Smith    if (!Literal.GetString().empty() || Literal.Pascal) {
1996aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      // C++11 [over.literal]p1:
1997aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      //   The string-literal or user-defined-string-literal in a
1998aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      //   literal-operator-id shall [...] contain no characters
1999aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      //   other than the implicit terminating '\0'.
200033762775706e81c17ca774102ceda36049ecc593Richard Smith      DiagLoc = TokLocs.front();
200133762775706e81c17ca774102ceda36049ecc593Richard Smith      DiagId = diag::err_literal_operator_string_not_empty;
200233762775706e81c17ca774102ceda36049ecc593Richard Smith    }
200333762775706e81c17ca774102ceda36049ecc593Richard Smith
200433762775706e81c17ca774102ceda36049ecc593Richard Smith    if (DiagId) {
200533762775706e81c17ca774102ceda36049ecc593Richard Smith      // This isn't a valid literal-operator-id, but we think we know
200633762775706e81c17ca774102ceda36049ecc593Richard Smith      // what the user meant. Tell them what they should have written.
2007cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko      SmallString<32> Str;
200833762775706e81c17ca774102ceda36049ecc593Richard Smith      Str += "\"\" ";
200933762775706e81c17ca774102ceda36049ecc593Richard Smith      Str += II->getName();
201033762775706e81c17ca774102ceda36049ecc593Richard Smith      Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
201133762775706e81c17ca774102ceda36049ecc593Richard Smith          SourceRange(TokLocs.front(), TokLocs.back()), Str);
201233762775706e81c17ca774102ceda36049ecc593Richard Smith    }
201333762775706e81c17ca774102ceda36049ecc593Richard Smith
201433762775706e81c17ca774102ceda36049ecc593Richard Smith    Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
20153e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    return false;
20160486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  }
2017ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2018ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse a conversion-function-id.
2019ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
2020ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-function-id: [C++ 12.3.2]
2021ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     operator conversion-type-id
2022ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
2023ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-type-id:
2024ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     type-specifier-seq conversion-declarator[opt]
2025ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
2026ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-declarator:
2027ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     ptr-operator conversion-declarator[opt]
2028ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2029ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the type-specifier-seq.
20300b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
2031f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor  if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
2032ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
2033ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2034ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the conversion-declarator, which is merely a sequence of
2035ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // ptr-operators.
2036ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Declarator D(DS, Declarator::TypeNameContext);
2037ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2038ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2039ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Finish up the type.
2040f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
2041ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Ty.isInvalid())
2042ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
2043ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2044ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Note that this is a conversion-function-id.
2045ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2046ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                 D.getSourceRange().getEnd());
2047ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return false;
2048ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
2049ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2050ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2051ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity.
2052ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2053ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
2054ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       unqualified-id: [C++ expr.prim.general]
2055ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         identifier
2056ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         operator-function-id
2057ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         conversion-function-id
2058ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO]
2059ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         ~ class-name
2060ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         template-id
2061ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2062ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode
2063ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
20641ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param SS The nested-name-specifier that preceded this unqualified-id. If
2065ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2066ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2067ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the
2068ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier.
2069ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
20703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name.
20713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
20723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name.
20733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
207446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
207546df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
207646df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
20773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
20783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
20793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise.
20803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
20813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowDestructorName,
20823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowConstructorName,
2083b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                ParsedType ObjectType,
2084e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                SourceLocation& TemplateKWLoc,
20853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                UnqualifiedId &Result) {
20860278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
20870278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // Handle 'A::template B'. This is for template-ids which have not
20880278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // already been annotated by ParseOptionalCXXScopeSpecifier().
20890278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  bool TemplateSpecified = false;
20904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
20910278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      (ObjectType || SS.isSet())) {
20920278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateSpecified = true;
20930278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateKWLoc = ConsumeToken();
20940278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  }
20950278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
20963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
20973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   identifier
20983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (when it hasn't already been annotated)
20993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::identifier)) {
21003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the identifier.
21013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *Id = Tok.getIdentifierInfo();
21023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation IdLoc = ConsumeToken();
21033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
21044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus) {
2105b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // If we're not in C++, only identifiers matter. Record the
2106b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // identifier and return.
2107b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      Result.setIdentifier(Id, IdLoc);
2108b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      return false;
2109b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    }
2110b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor
21113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (AllowConstructorName &&
211223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
21133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed a constructor name.
2114fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara      ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2115fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          &SS, false, false,
2116fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          ParsedType(),
2117fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          /*IsCtorOrDtorName=*/true,
2118fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          /*NonTrivialTypeSourceInfo=*/true);
2119fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara      Result.setConstructorName(Ty, IdLoc, IdLoc);
21203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
21213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an identifier.
21223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setIdentifier(Id, IdLoc);
21233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
21243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
21253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // If the next token is a '<', we may have a template.
21260278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less))
2127e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2128e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2129e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
21303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
21313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
21323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
21333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
21343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
21353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (already parsed and annotated)
21363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::annot_template_id)) {
213725a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
21380efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
21390efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    // If the template-name names the current class, then this is a constructor
21400efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    if (AllowConstructorName && TemplateId->Name &&
214123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
21420efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      if (SS.isSet()) {
21430efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // C++ [class.qual]p2 specifies that a qualified template-name
21440efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // is taken as the constructor name where a constructor can be
21450efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // declared. Thus, the template arguments are extraneous, so
21460efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // complain about them and remove them entirely.
21470efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Diag(TemplateId->TemplateNameLoc,
21480efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor             diag::err_out_of_line_constructor_template_id)
21490efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor          << TemplateId->Name
2150849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateRemoval(
21510efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                    SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
2152fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara        ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2153fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            TemplateId->TemplateNameLoc,
2154fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            getCurScope(),
2155fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            &SS, false, false,
2156fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            ParsedType(),
2157fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*IsCtorOrDtorName=*/true,
2158fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*NontrivialTypeSourceInfo=*/true);
2159fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara        Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
21600efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->RAngleLoc);
21610efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        ConsumeToken();
21620efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        return false;
21630efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      }
21640efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
21650efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      Result.setConstructorTemplateId(TemplateId);
21660efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      ConsumeToken();
21670efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      return false;
21680efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    }
21690efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
21703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // We have already parsed a template-id; consume the annotation token as
21713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // our unqualified-id.
21720efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    Result.setTemplateId(TemplateId);
2173e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    TemplateKWLoc = TemplateId->TemplateKWLoc;
21743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ConsumeToken();
21753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
21763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
21773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
21783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
21793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   operator-function-id
21803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   conversion-function-id
21813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::kw_operator)) {
2182ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
21833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
21843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
2185e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // If we have an operator-function-id or a literal-operator-id and the next
2186e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // token is a '<', we may have a
2187ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //
2188ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //   template-id:
2189ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //     operator-function-id < template-argument-list[opt] >
2190e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2191e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt         Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
21920278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor        (TemplateSpecified || Tok.is(tok::less)))
2193e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2194e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          0, SourceLocation(),
2195e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2196e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
21973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
21983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
21993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
22003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus &&
2202b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
22033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // C++ [expr.unary.op]p10:
22043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   There is an ambiguity in the unary-expression ~X(), where X is a
22053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   class-name. The ambiguity is resolved in favor of treating ~ as a
22063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //    unary complement rather than treating ~X as referring to a destructor.
22073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the '~'.
22093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation TildeLoc = ConsumeToken();
221053a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie
221153a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
221253a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      DeclSpec DS(AttrFactory);
221353a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
221453a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
221553a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie        Result.setDestructorName(TildeLoc, Type, EndLoc);
221653a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie        return false;
221753a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      }
221853a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      return true;
221953a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    }
22203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name.
22223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.isNot(tok::identifier)) {
2223124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      Diag(Tok, diag::err_destructor_tilde_identifier);
22243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
22253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
22263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name (or template-name in a simple-template-id).
22283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
22293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation ClassNameLoc = ConsumeToken();
22303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22310278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less)) {
2232b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
2233e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2234e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          ClassName, ClassNameLoc,
2235e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2236e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
22372d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
22382d1c21414199a7452f122598189363a3922605b1Douglas Gregor
22393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a destructor name.
2240b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2241b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              ClassNameLoc, getCurScope(),
2242b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              SS, ObjectType,
2243b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              EnteringContext);
2244124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (!Ty)
22453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
2246124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
22473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
22483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
22493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
22503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22512d1c21414199a7452f122598189363a3922605b1Douglas Gregor  Diag(Tok, diag::err_expected_unqualified_id)
22524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    << getLangOpts().CPlusPlus;
22533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return true;
22543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
22553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
22574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors.
22581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
225959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has
226059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
226159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location.  Otherwise, "Start" is the location of the 'new' token.
22624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
22634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
22644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] new-type-id
22654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
22664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
22674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
22684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
22694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
22704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
22714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
2272cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-type-id:
2273cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   type-specifier-seq new-declarator[opt]
2274893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor/// [GNU]             attributes type-specifier-seq new-declarator[opt]
2275cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
2276cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-declarator:
2277cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   ptr-operator new-declarator[opt]
2278cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   direct-new-declarator
2279cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
22804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-initializer:
22814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list[opt] ')'
2282dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x]           braced-init-list
22834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
228460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
228559232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
228659232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_new) && "expected 'new' token");
228759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken();   // Consume 'new'
22884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
22904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // second form of new-expression. It can't be a new-type-id.
22914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22924e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer  ExprVector PlacementArgs;
22934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation PlacementLParen, PlacementRParen;
22944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22954bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor  SourceRange TypeIdParens;
22960b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
22970b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
22984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
22994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // If it turns out to be a placement, we change the type location.
23004a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
23014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
23024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    PlacementLParen = T.getOpenLocation();
2303cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2304cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
230520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2306cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
23074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
23084a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
23094a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    PlacementRParen = T.getCloseLocation();
2310cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementRParen.isInvalid()) {
2311cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
231220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2313cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
23144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2315cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementArgs.empty()) {
23164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Reset the placement locations. There was no placement.
23174a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      TypeIdParens = T.getRange();
23184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      PlacementLParen = PlacementRParen = SourceLocation();
23194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    } else {
23204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // We still need the type.
23214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      if (Tok.is(tok::l_paren)) {
23224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        BalancedDelimiterTracker T(*this, tok::l_paren);
23234a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeOpen();
2324893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor        MaybeParseGNUAttributes(DeclaratorInfo);
2325cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseSpecifierQualifierList(DS);
2326ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2327cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseDeclarator(DeclaratorInfo);
23284a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeClose();
23294a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        TypeIdParens = T.getRange();
23304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      } else {
2331893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor        MaybeParseGNUAttributes(DeclaratorInfo);
2332cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        if (ParseCXXTypeSpecifierSeq(DS))
2333cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          DeclaratorInfo.setInvalidType(true);
2334ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        else {
2335ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2336cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          ParseDeclaratorInternal(DeclaratorInfo,
2337cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                                  &Parser::ParseDirectNewDeclarator);
2338ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        }
23394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      }
23404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
23414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  } else {
2342cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // A new-type-id is a simplified type-id, where essentially the
2343cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // direct-declarator is replaced by a direct-new-declarator.
2344893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor    MaybeParseGNUAttributes(DeclaratorInfo);
2345cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseCXXTypeSpecifierSeq(DS))
2346cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      DeclaratorInfo.setInvalidType(true);
2347ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    else {
2348ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2349cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      ParseDeclaratorInternal(DeclaratorInfo,
2350cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                              &Parser::ParseDirectNewDeclarator);
2351ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    }
23524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
2353eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (DeclaratorInfo.isInvalidType()) {
2354cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
235520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
2356cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
23574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
23582aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  ExprResult Initializer;
23594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
23604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
23612aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    SourceLocation ConstructorLParen, ConstructorRParen;
23624e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer    ExprVector ConstructorArgs;
23634a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
23644a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
23654a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    ConstructorLParen = T.getOpenLocation();
23664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (Tok.isNot(tok::r_paren)) {
23674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      CommaLocsTy CommaLocs;
2368cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2369cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
237020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl        return ExprError();
2371cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      }
23724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
23734a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
23744a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    ConstructorRParen = T.getCloseLocation();
2375cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ConstructorRParen.isInvalid()) {
2376cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
237720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2378cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
23792aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
23802aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                                             ConstructorRParen,
23813fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                             ConstructorArgs);
238280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
23837fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith    Diag(Tok.getLocation(),
23847fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith         diag::warn_cxx98_compat_generalized_initializer_lists);
23852aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    Initializer = ParseBraceInitializer();
23864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
23872aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  if (Initializer.isInvalid())
23882aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    return Initializer;
23894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2390f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
23913fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                             PlacementArgs, PlacementRParen,
23922aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                             TypeIdParens, DeclaratorInfo, Initializer.take());
23934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
23944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
23954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
23964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal.
23974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
23984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        direct-new-declarator:
23994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '[' expression ']'
24004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   direct-new-declarator '[' constant-expression ']'
24014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
240259232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) {
24034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Parse the array dimensions.
24044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool first = true;
24054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  while (Tok.is(tok::l_square)) {
24066ee326af4e77e6f05973486097884d7431f2108dRichard Smith    // An array-size expression can't start with a lambda.
24076ee326af4e77e6f05973486097884d7431f2108dRichard Smith    if (CheckProhibitedCXX11Attribute())
24086ee326af4e77e6f05973486097884d7431f2108dRichard Smith      continue;
24096ee326af4e77e6f05973486097884d7431f2108dRichard Smith
24104a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_square);
24114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
24124a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
241360d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Size(first ? ParseExpression()
24142f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl                                : ParseConstantExpression());
24150e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Size.isInvalid()) {
24164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Recover
24174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      SkipUntil(tok::r_square);
24184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
24194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
24204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    first = false;
24214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
24230b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
2424ad017fa7a4df7389d245d02a49b3c79ed70bedb9Bill Wendling    // Attributes here appertain to the array type. C++11 [expr.new]p5.
24256ee326af4e77e6f05973486097884d7431f2108dRichard Smith    ParsedAttributes Attrs(AttrFactory);
24264e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    MaybeParseCXX11Attributes(Attrs);
24276ee326af4e77e6f05973486097884d7431f2108dRichard Smith
24280b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    D.AddTypeInfo(DeclaratorChunk::getArray(0,
24297f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                            /*static=*/false, /*star=*/false,
24304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Size.release(),
24314a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            T.getOpenLocation(),
24324a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            T.getCloseLocation()),
24336ee326af4e77e6f05973486097884d7431f2108dRichard Smith                  Attrs, T.getCloseLocation());
24344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
24364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
24374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
24384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
24394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
24414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator.
24424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
24434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
24444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
24454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
24464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
24474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
24484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
24494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
2450ca0408fb49c1370430672acf2d770b7151cf71deJohn McCallbool Parser::ParseExpressionListOrTypeId(
24515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   SmallVectorImpl<Expr*> &PlacementArgs,
245259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner                                         Declarator &D) {
24534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // The '(' was already consumed.
24544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (isTypeIdInParens()) {
2455cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseSpecifierQualifierList(D.getMutableDeclSpec());
2456ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetSourceRange(D.getDeclSpec().getSourceRange());
2457cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseDeclarator(D);
2458eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    return D.isInvalidType();
24594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
24604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // It's not a type, it has to be an expression list.
24624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Discard the comma locations - ActOnCXXNew has enough parameters.
24634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  CommaLocsTy CommaLocs;
24644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return ParseExpressionList(PlacementArgs, CommaLocs);
24654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
24664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
24684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new.
24694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
247059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional
247159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
247259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location.  Otherwise, "Start" is the location of the
247359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token.
247459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner///
24754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        delete-expression:
24764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' cast-expression
24774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' '[' ']' cast-expression
247860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
247959232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
248059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
248159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken(); // Consume 'delete'
24824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Array delete?
24844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ArrayDelete = false;
24856ee326af4e77e6f05973486097884d7431f2108dRichard Smith  if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
2486950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    // C++11 [expr.delete]p1:
2487950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //   Whenever the delete keyword is followed by empty square brackets, it
2488950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //   shall be interpreted as [array delete].
2489950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //   [Footnote: A lambda expression with a lambda-introducer that consists
2490950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //              of empty square brackets can follow the delete keyword if
2491950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //              the lambda expression is enclosed in parentheses.]
2492950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2493950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //        lambda-introducer.
24944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ArrayDelete = true;
24954a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_square);
24964a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
24974a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
24984a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
24994a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
250020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
25014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
25024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
250360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Operand(ParseCastExpression(false));
25040e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Operand.isInvalid())
25053fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    return Operand;
25064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25079ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
25084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
250964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
25101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
251164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch(kind) {
2512b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a known unary type trait.");
251364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign;
25149ef9875bbe19dc9f73c6c95b803d9a4945168690Joao Matos  case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
251564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
251620c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___has_nothrow_copy:           return UTT_HasNothrowCopy;
251764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign;
25189ef9875bbe19dc9f73c6c95b803d9a4945168690Joao Matos  case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
2519023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  case tok::kw___has_trivial_constructor:
2520023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                    return UTT_HasTrivialDefaultConstructor;
25219ef9875bbe19dc9f73c6c95b803d9a4945168690Joao Matos  case tok::kw___has_trivial_move_constructor:
25229ef9875bbe19dc9f73c6c95b803d9a4945168690Joao Matos                                    return UTT_HasTrivialMoveConstructor;
252320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___has_trivial_copy:           return UTT_HasTrivialCopy;
252464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor;
252564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor;
252664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_abstract:             return UTT_IsAbstract;
252720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_arithmetic:              return UTT_IsArithmetic;
252820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_array:                   return UTT_IsArray;
252964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_class:                return UTT_IsClass;
253020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_complete_type:           return UTT_IsCompleteType;
253120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_compound:                return UTT_IsCompound;
253220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_const:                   return UTT_IsConst;
253364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_empty:                return UTT_IsEmpty;
253464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_enum:                 return UTT_IsEnum;
25355e9392ba18f5925e26cc5714d1412eda0d219826Douglas Gregor  case tok::kw___is_final:                 return UTT_IsFinal;
253620c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_floating_point:          return UTT_IsFloatingPoint;
253720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_function:                return UTT_IsFunction;
253820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_fundamental:             return UTT_IsFundamental;
253920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_integral:                return UTT_IsIntegral;
2540ea30e2f8667668173cf7433c3c80cf603bd922a4John McCall  case tok::kw___is_interface_class:         return UTT_IsInterfaceClass;
254120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_lvalue_reference:        return UTT_IsLvalueReference;
254220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
254320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_object_pointer:   return UTT_IsMemberObjectPointer;
254420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_pointer:          return UTT_IsMemberPointer;
254520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_object:                  return UTT_IsObject;
25464e61ddd644e9c6293697a966d98d7c1905cf63a8Chandler Carruth  case tok::kw___is_literal:              return UTT_IsLiteral;
25473840281126e7d10552c55f6fd8b1ec9483898906Chandler Carruth  case tok::kw___is_literal_type:         return UTT_IsLiteral;
254864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_pod:                  return UTT_IsPOD;
254920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_pointer:                 return UTT_IsPointer;
255064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_polymorphic:          return UTT_IsPolymorphic;
255120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_reference:               return UTT_IsReference;
255220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_rvalue_reference:        return UTT_IsRvalueReference;
255320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_scalar:                  return UTT_IsScalar;
255420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_signed:                  return UTT_IsSigned;
255520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_standard_layout:         return UTT_IsStandardLayout;
255620c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_trivial:                 return UTT_IsTrivial;
2557feb375d31b7e9108b04a9f55b721d5e0c793a558Sean Hunt  case tok::kw___is_trivially_copyable:      return UTT_IsTriviallyCopyable;
255864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_union:                return UTT_IsUnion;
255920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_unsigned:                return UTT_IsUnsigned;
256020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_void:                    return UTT_IsVoid;
256120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_volatile:                return UTT_IsVolatile;
256264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
25636ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
25646ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
25656ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichetstatic BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
25666ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  switch(kind) {
256738c2b730a8553fa1cf369d0c5567f8b5d0a3dda8Francois Pichet  default: llvm_unreachable("Not a known binary type trait");
2568f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case tok::kw___is_base_of:                 return BTT_IsBaseOf;
256920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_convertible:             return BTT_IsConvertible;
257020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_same:                    return BTT_IsSame;
2571f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
25729f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor  case tok::kw___is_convertible_to:          return BTT_IsConvertibleTo;
257325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  case tok::kw___is_trivially_assignable:    return BTT_IsTriviallyAssignable;
25746ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
257564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
257664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
25774ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregorstatic TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
25784ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  switch (kind) {
25794ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  default: llvm_unreachable("Not a known type trait");
25804ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  case tok::kw___is_trivially_constructible:
25814ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return TT_IsTriviallyConstructible;
25824ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  }
25834ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor}
25844ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
258521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegleystatic ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
258621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch(kind) {
258721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  default: llvm_unreachable("Not a known binary type trait");
258821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case tok::kw___array_rank:                 return ATT_ArrayRank;
258921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case tok::kw___array_extent:               return ATT_ArrayExtent;
259021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
259121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
259221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
2593552622067dc45013d240f73952fece703f5e63bdJohn Wiegleystatic ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2594552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  switch(kind) {
2595b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a known unary expression trait.");
2596552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case tok::kw___is_lvalue_expr:             return ET_IsLValueExpr;
2597552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case tok::kw___is_rvalue_expr:             return ET_IsRValueExpr;
2598552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  }
2599552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
2600552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
260164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
260264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits
260364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates.
260464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
260564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///       primary-expression:
260664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU]             unary-type-trait '(' type-id ')'
260764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
260860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseUnaryTypeTrait() {
260964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
261064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation Loc = ConsumeToken();
261164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
26124a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
26134a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
261464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return ExprError();
261564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
261664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
261764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // there will be cryptic errors about mismatched parentheses and missing
261864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // specifiers.
2619809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
262064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
26214a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
262264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
2623809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2624809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2625809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
26264a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
262764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
2628f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
26296ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
26306ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// pseudo-functions that allow implementation of the TR1/C++0x type traits
26316ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// templates.
26326ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///
26336ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///       primary-expression:
26346ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// [GNU]             binary-type-trait '(' type-id ',' type-id ')'
26356ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///
26366ad6f2848d7652ab2991286eb48be440d3493b28Francois PichetExprResult Parser::ParseBinaryTypeTrait() {
26376ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
26386ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  SourceLocation Loc = ConsumeToken();
26396ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
26404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
26414a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
26426ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
26436ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
26446ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeResult LhsTy = ParseTypeName();
26456ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (LhsTy.isInvalid()) {
26466ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
26476ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
26486ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
26496ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
26506ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
26516ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
26526ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
26536ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
26546ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
26556ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeResult RhsTy = ParseTypeName();
26566ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (RhsTy.isInvalid()) {
26576ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
26586ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
26596ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
26606ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
26614a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
26626ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
26634a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
26644a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      T.getCloseLocation());
26656ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
26666ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
26674ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor/// \brief Parse the built-in type-trait pseudo-functions that allow
26684ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor/// implementation of the TR1/C++11 type traits templates.
26694ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///
26704ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///       primary-expression:
26714ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///          type-trait '(' type-id-seq ')'
26724ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///
26734ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///       type-id-seq:
26744ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///          type-id ...[opt] type-id-seq[opt]
26754ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///
26764ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas GregorExprResult Parser::ParseTypeTrait() {
26774ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
26784ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  SourceLocation Loc = ConsumeToken();
26794ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
26804ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  BalancedDelimiterTracker Parens(*this, tok::l_paren);
26814ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  if (Parens.expectAndConsume(diag::err_expected_lparen))
26824ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return ExprError();
26834ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
2684cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<ParsedType, 2> Args;
26854ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  do {
26864ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // Parse the next type.
26874ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    TypeResult Ty = ParseTypeName();
26884ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Ty.isInvalid()) {
26894ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      Parens.skipToEnd();
26904ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      return ExprError();
26914ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
26924ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
26934ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // Parse the ellipsis, if present.
26944ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Tok.is(tok::ellipsis)) {
26954ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
26964ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      if (Ty.isInvalid()) {
26974ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        Parens.skipToEnd();
26984ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        return ExprError();
26994ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      }
27004ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
27014ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
27024ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // Add this type to the list of arguments.
27034ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    Args.push_back(Ty.get());
27044ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
27054ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Tok.is(tok::comma)) {
27064ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      ConsumeToken();
27074ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      continue;
27084ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
27094ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
27104ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    break;
27114ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  } while (true);
27124ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
27134ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  if (Parens.consumeClose())
27144ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return ExprError();
27154ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
27164ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
27174ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor}
27184ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
271921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// ParseArrayTypeTrait - Parse the built-in array type-trait
272021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// pseudo-functions.
272121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///
272221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///       primary-expression:
272321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// [Embarcadero]     '__array_rank' '(' type-id ')'
272421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// [Embarcadero]     '__array_extent' '(' type-id ',' expression ')'
272521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///
272621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John WiegleyExprResult Parser::ParseArrayTypeTrait() {
272721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
272821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  SourceLocation Loc = ConsumeToken();
272921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
27304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
27314a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
273221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return ExprError();
273321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
273421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  TypeResult Ty = ParseTypeName();
273521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  if (Ty.isInvalid()) {
273621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    SkipUntil(tok::comma);
273721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    SkipUntil(tok::r_paren);
273821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return ExprError();
273921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
274021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
274121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch (ATT) {
274221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayRank: {
27434a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
27444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
27454a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
274621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
274721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayExtent: {
274821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
274921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      SkipUntil(tok::r_paren);
275021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      return ExprError();
275121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    }
275221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
275321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    ExprResult DimExpr = ParseExpression();
27544a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
275521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
27564a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
27574a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
275821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
275921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
27603026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid ArrayTypeTrait!");
276121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
276221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
2763552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// ParseExpressionTrait - Parse built-in expression-trait
2764552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// pseudo-functions like __is_lvalue_expr( xxx ).
2765552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///
2766552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///       primary-expression:
2767552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// [Embarcadero]     expression-trait '(' expression ')'
2768552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///
2769552622067dc45013d240f73952fece703f5e63bdJohn WiegleyExprResult Parser::ParseExpressionTrait() {
2770552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2771552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  SourceLocation Loc = ConsumeToken();
2772552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
27734a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
27744a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
2775552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    return ExprError();
2776552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2777552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  ExprResult Expr = ParseExpression();
2778552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
27794a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
2780552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
27814a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
27824a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      T.getCloseLocation());
2783552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
2784552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2785552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2786f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2787f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2788f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens.
278960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2790f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
2791b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                         ParsedType &CastTy,
27924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                         BalancedDelimiterTracker &Tracker) {
27934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
2794f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2795f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(isTypeIdInParens() && "Not a type-id!");
2796f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
279760d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(true);
2798b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  CastTy = ParsedType();
2799f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2800f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // We need to disambiguate a very ugly part of the C++ syntax:
2801f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2802f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())x;  - type-id
2803f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())*x; - type-id
2804f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())/x; - expression
2805f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T());   - expression
2806f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2807f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The bad news is that we cannot use the specialized tentative parser, since
2808f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // it can only verify that the thing inside the parens can be parsed as
2809f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // type-id, it is not useful for determining the context past the parens.
2810f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2811f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The good news is that the parser can disambiguate this part without
2812a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis  // making any unnecessary Action calls.
2813f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  //
2814f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // It uses a scheme similar to parsing inline methods. The parenthesized
2815f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // tokens are cached, the context that follows is determined (possibly by
2816f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parsing a cast-expression), and then we re-introduce the cached tokens
2817f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // into the token stream and parse them appropriately.
2818f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
28191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParenParseOption ParseAs;
2820f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  CachedTokens Toks;
2821f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2822f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Store the tokens of the parentheses. We will parse them after we determine
2823f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // the context that follows them.
282414b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis  if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
2825f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We didn't find the ')' we expected.
28264a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Tracker.consumeClose();
2827f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
2828f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
2829f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2830f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::l_brace)) {
2831f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = CompoundLiteral;
2832f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  } else {
2833f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    bool NotCastExpr;
2834b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2835b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2836b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      NotCastExpr = true;
2837b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    } else {
2838b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // Try parsing the cast-expression that may follow.
2839b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // If it is not a cast-expression, NotCastExpr will be true and no token
2840b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // will be consumed.
2841b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      Result = ParseCastExpression(false/*isUnaryExpression*/,
2842b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman                                   false/*isAddressofOperand*/,
2843b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   NotCastExpr,
28440a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis                                   // type-id has priority.
2845cd78e612d6fa8e214e6a6bb0e739a0c3e419df91Kaelyn Uhrain                                   IsTypeCast);
2846b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    }
2847f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2848f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // If we parsed a cast-expression, it's really a type-id, otherwise it's
2849f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // an expression.
2850f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
2851f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
2852f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
28531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The current token should go after the cached tokens.
2854f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Toks.push_back(Tok);
2855f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Re-enter the stored parenthesized tokens into the token stream, so we may
2856f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parse them now.
2857f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  PP.EnterTokenStream(Toks.data(), Toks.size(),
2858f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2859f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Drop the current token and bring the first cached one. It's the same token
2860f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // as when we entered this function.
2861f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  ConsumeAnyToken();
2862f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2863f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (ParseAs >= CompoundLiteral) {
28640a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    // Parse the type declarator.
28650a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    DeclSpec DS(AttrFactory);
28660a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    ParseSpecifierQualifierList(DS);
28670a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
28680a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    ParseDeclarator(DeclaratorInfo);
2869f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2870f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Match the ')'.
28714a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Tracker.consumeClose();
2872f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2873f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (ParseAs == CompoundLiteral) {
2874f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      ExprType = CompoundLiteral;
28750a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis      TypeResult Ty = ParseTypeName();
28764a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor       return ParseCompoundLiteralExpression(Ty.get(),
28774a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Tracker.getOpenLocation(),
28784a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Tracker.getCloseLocation());
2879f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    }
28801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2881f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2882f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    assert(ParseAs == CastExpr);
2883f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
28840a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    if (DeclaratorInfo.isInvalidType())
2885f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ExprError();
2886f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2887f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Result is what ParseCastExpression returned earlier.
2888f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    if (!Result.isInvalid())
28894a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
28904a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    DeclaratorInfo, CastTy,
28914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    Tracker.getCloseLocation(), Result.take());
28923fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    return Result;
2893f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
28941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2895f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Not a compound literal, and not followed by a cast-expression.
2896f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  assert(ParseAs == SimpleExpr);
2897f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2898f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  ExprType = SimpleExpr;
2899f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Result = ParseExpression();
2900f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (!Result.isInvalid() && Tok.is(tok::r_paren))
29014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
29024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    Tok.getLocation(), Result.take());
2903f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2904f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // Match the ')'.
2905f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Result.isInvalid()) {
2906f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    SkipUntil(tok::r_paren);
2907f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
2908f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
29091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29104a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Tracker.consumeClose();
29113fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
2912f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis}
2913