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//===----------------------------------------------------------------------===//
13651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/AST/ASTContext.h"
14bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor#include "RAIIObjectsForParser.h"
15651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/AST/DeclTemplate.h"
16dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman#include "clang/Basic/PrettyStackTrace.h"
1733762775706e81c17ca774102ceda36049ecc593Richard Smith#include "clang/Lex/LiteralSupport.h"
1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Parse/ParseDiagnostic.h"
19651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/Parse/Parser.h"
2019510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
2119510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h"
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Scope.h"
233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h"
243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
25fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic int SelectDigraphErrorMessage(tok::TokenKind Kind) {
29ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  switch (Kind) {
30651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // template name
31651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::unknown:             return 0;
32651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // casts
33ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_const_cast:       return 1;
34ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_dynamic_cast:     return 2;
35ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_reinterpret_cast: return 3;
36ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_static_cast:      return 4;
37ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    default:
38b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unknown type for digraph error message.");
39ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  }
40ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
41ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
42ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith// Are the two tokens adjacent in the same source file?
4319a2702042b7e3ee838cca458b35f607111a3897Richard Smithbool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
44ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceManager &SM = PP.getSourceManager();
45ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
46a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
47ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  return FirstEnd == SM.getSpellingLoc(Second.getLocation());
48ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
49ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
50ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith// Suggest fixit for "<::" after a cast.
51ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
52ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                       Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
53ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Pull '<:' and ':' off token stream.
54ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (!AtDigraph)
55ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    PP.Lex(DigraphToken);
56ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  PP.Lex(ColonToken);
57ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
58ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceRange Range;
59ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Range.setBegin(DigraphToken.getLocation());
60ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Range.setEnd(ColonToken.getLocation());
61ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
62ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      << SelectDigraphErrorMessage(Kind)
63ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      << FixItHint::CreateReplacement(Range, "< ::");
64ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
65ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Update token information to reflect their change in token type.
66ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setKind(tok::coloncolon);
67a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
68ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setLength(2);
69ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  DigraphToken.setKind(tok::less);
70ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  DigraphToken.setLength(1);
71ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
72ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Push new tokens back to token stream.
73ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  PP.EnterToken(ColonToken);
74ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (!AtDigraph)
75ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    PP.EnterToken(DigraphToken);
76ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
77ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
78950be71c745409e373ae8a834490f9026c8ac222Richard Trieu// Check for '<::' which should be '< ::' instead of '[:' when following
79950be71c745409e373ae8a834490f9026c8ac222Richard Trieu// a template name.
80950be71c745409e373ae8a834490f9026c8ac222Richard Trieuvoid Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
81950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                                        bool EnteringContext,
82950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                                        IdentifierInfo &II, CXXScopeSpec &SS) {
83c11030ea936f6952deb5a1423ce1648173cd417eRichard Trieu  if (!Next.is(tok::l_square) || Next.getLength() != 2)
84950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
85950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
86950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  Token SecondToken = GetLookAheadToken(2);
8719a2702042b7e3ee838cca458b35f607111a3897Richard Smith  if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
88950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
89950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
90950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  TemplateTy Template;
91950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  UnqualifiedId TemplateName;
92950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  TemplateName.setIdentifier(&II, Tok.getLocation());
93950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  bool MemberOfUnknownSpecialization;
94950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
95950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                              TemplateName, ObjectType, EnteringContext,
96950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                              Template, MemberOfUnknownSpecialization))
97950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
98950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
99651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  FixDigraph(*this, PP, Next, SecondToken, tok::unknown,
100950be71c745409e373ae8a834490f9026c8ac222Richard Trieu             /*AtDigraph*/false);
101950be71c745409e373ae8a834490f9026c8ac222Richard Trieu}
102950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
103919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu/// \brief Emits an error for a left parentheses after a double colon.
104919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu///
105919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu/// When a '(' is found after a '::', emit an error.  Attempt to fix the token
106bba91b881c946cbcd200c87dc0a83553506e2458Nico Weber/// stream by removing the '(', and the matching ')' if found.
107919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieuvoid Parser::CheckForLParenAfterColonColon() {
108919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  if (!Tok.is(tok::l_paren))
109919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    return;
110919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
111919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
112919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  Token Tok1 = getCurToken();
113919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
114919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    return;
115919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
116919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  if (Tok1.is(tok::identifier)) {
117919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    Token Tok2 = GetLookAheadToken(1);
118919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    if (Tok2.is(tok::r_paren)) {
119919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      ConsumeToken();
120919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      PP.EnterToken(Tok1);
121919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      r_parenLoc = ConsumeParen();
122919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    }
123919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  } else if (Tok1.is(tok::star)) {
124919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    Token Tok2 = GetLookAheadToken(1);
125919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    if (Tok2.is(tok::identifier)) {
126919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      Token Tok3 = GetLookAheadToken(2);
127919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      if (Tok3.is(tok::r_paren)) {
128919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        ConsumeToken();
129919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        ConsumeToken();
130919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        PP.EnterToken(Tok2);
131919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        PP.EnterToken(Tok1);
132919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        r_parenLoc = ConsumeParen();
133919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      }
134919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    }
135919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  }
136919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
137919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  Diag(l_parenLoc, diag::err_paren_after_colon_colon)
138919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      << FixItHint::CreateRemoval(l_parenLoc)
139919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      << FixItHint::CreateRemoval(r_parenLoc);
140919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu}
141919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present.
1432dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1442dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or
1462dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream.
147eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
148eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'[opt] nested-name-specifier
149eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'
150eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
151eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       nested-name-specifier:
152eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         type-name '::'
153eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         namespace-name '::'
154eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         nested-name-specifier identifier '::'
1552dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///         nested-name-specifier 'template'[opt] simple-template-id '::'
1562dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1572dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed
1592dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty)
1602dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following
1622dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the
1632dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed.
164eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
1652dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of
1662dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it.
1672dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
168d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param MayBePseudoDestructor When non-NULL, points to a flag that
169d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// indicates whether this nested-name-specifier may be part of a
170d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// pseudo-destructor name. In this case, the flag will be set false
171d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we don't actually end up parsing a destructor name. Moreorover,
172d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we do end up determining that we are parsing a destructor name,
173d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// the last component of the nested-name-specifier is not parsed as
174d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// part of the scope specifier.
1752db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith///
1762db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// \param IsTypename If \c true, this nested-name-specifier is known to be
1772db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// part of a type name. This is used to improve error recovery.
1782db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith///
1792db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
1802db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// filled in with the leading identifier in the last component of the
1812db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// nested-name-specifier, if any.
182b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor///
1839ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// \returns true if there was an error parsing a scope specifier
184495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
185b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType ObjectType,
186b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            bool EnteringContext,
1874147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                            bool *MayBePseudoDestructor,
1882db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith                                            bool IsTypename,
1892db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith                                            IdentifierInfo **LastII) {
1904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus &&
1917452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Call sites of this function should be guarded by checking for C++");
1921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
193eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::annot_cxxscope)) {
1942db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith    assert(!LastII && "want last identifier but have already annotated scope");
195c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
196c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 Tok.getAnnotationRange(),
197c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 SS);
198eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    ConsumeToken();
1999ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
200eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
201e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner
2029c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo  if (Tok.is(tok::annot_template_id)) {
2039c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo    // If the current token is an annotated template id, it may already have
2049c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo    // a scope specifier. Restore it.
2059c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
2069c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo    SS = TemplateId->SS;
2079c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo  }
2089c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo
2092db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  if (LastII)
2106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    *LastII = nullptr;
2112db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith
21239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  bool HasScopeSpecifier = false;
21339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
2145b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner  if (Tok.is(tok::coloncolon)) {
2155b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    // ::new and ::delete aren't nested-name-specifiers.
2165b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    tok::TokenKind NextKind = NextToken().getKind();
2175b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
2185b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner      return false;
2191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22055a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    // '::' - Global scope qualifier.
2212e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
2222e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
223919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
224919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    CheckForLParenAfterColonColon();
225919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
22639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    HasScopeSpecifier = true;
227eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
228eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
229d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  bool CheckForDestructor = false;
230d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (MayBePseudoDestructor && *MayBePseudoDestructor) {
231d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CheckForDestructor = true;
232d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = false;
233d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
234d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
23542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
23642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    DeclSpec DS(AttrFactory);
23742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation DeclLoc = Tok.getLocation();
23842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation EndLoc  = ParseDecltypeSpecifier(DS);
239651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
240651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SourceLocation CCLoc;
241651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (!TryConsumeToken(tok::coloncolon, CCLoc)) {
24242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
24342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      return false;
24442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    }
245651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
24642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
24742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
24842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
24942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    HasScopeSpecifier = true;
25042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  }
25142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
25239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  while (true) {
2532dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (HasScopeSpecifier) {
2542dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p5:
2552dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the qualified-id has the form
2563b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
2572dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //       ::class-name-or-namespace-name::...
2583b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
2592dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the class-name-or-namespace-name is looked up in global scope as a
2602dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name.
2612dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
2622dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // To implement this, we clear out the object type as soon as we've
2632dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // seen a leading '::' or part of a nested-name-specifier.
264b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      ObjectType = ParsedType();
26581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
26681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      if (Tok.is(tok::code_completion)) {
26781b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // Code completion for a nested-name-specifier, where the code
26881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // code completion token follows the '::'.
26923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
270b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // Include code completion token into the range of the scope otherwise
271b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // when we try to annotate the scope tokens the dangling code completion
272b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // token will cause assertion in
273b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // Preprocessor::AnnotatePreviousCachedTokens.
2747d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        SS.setEndLoc(Tok.getLocation());
2757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
2767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return true;
27781b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      }
2782dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
2791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier:
28177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    //   nested-name-specifier 'template'[opt] simple-template-id '::'
28277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner
28377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // Parse the optional 'template' keyword, then make sure we have
28477cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // 'identifier <' after it.
28577cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    if (Tok.is(tok::kw_template)) {
2862dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // If we don't have a scope specifier or an object type, this isn't a
287eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // nested-name-specifier, since they aren't allowed to start with
288eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // 'template'.
2892dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      if (!HasScopeSpecifier && !ObjectType)
290eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
291eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman
2927bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TentativeParsingAction TPA(*this);
29377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      SourceLocation TemplateKWLoc = ConsumeToken();
294651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
295ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      UnqualifiedId TemplateName;
296ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::identifier)) {
297ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
2987bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
299ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
300ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else if (Tok.is(tok::kw_operator)) {
301651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // We don't need to actually parse the unqualified-id in this case,
302651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // because a simple-template-id cannot start with 'operator', but
303651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // go ahead and parse it anyway for consistency with the case where
304651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // we already annotated the template-id.
305651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
3067bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor                                       TemplateName)) {
3077bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
308ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
3097bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        }
310651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
311e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt        if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
312e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt            TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
313ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          Diag(TemplateName.getSourceRange().getBegin(),
314ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor               diag::err_id_after_template_in_nested_name_spec)
315ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor            << TemplateName.getSourceRange();
3167bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
317ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
318ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        }
319ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
3207bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
32177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        break;
32277cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      }
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3247bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // If the next token is not '<', we have a qualified-id that refers
3257bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // to a template name, such as T::template apply, but is not a
3267bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // template-id.
3277bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      if (Tok.isNot(tok::less)) {
3287bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
3297bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        break;
3307bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      }
3317bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor
3327bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // Commit to parsing the template-id.
3337bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TPA.Commit();
334d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      TemplateTy Template;
335e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      if (TemplateNameKind TNK
336e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara          = Actions.ActOnDependentTemplateName(getCurScope(),
337e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               SS, TemplateKWLoc, TemplateName,
338e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               ObjectType, EnteringContext,
339e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               Template)) {
340e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
341e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName, false))
342d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          return true;
343d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      } else
3449ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
3451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34677cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      continue;
34777cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    }
3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We have
35139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
352651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      //   template-id '::'
35339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
354651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // So we need to check whether the template-id is a simple-template-id of
355651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // the right kind (it should name a type or be dependent), and then
356c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // convert it into a type within the nested-name-specifier.
35725a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
358d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
359d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
3609ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
361d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
362d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
3632db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith      if (LastII)
3642db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith        *LastII = TemplateId->Name;
3652db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith
3666cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      // Consume the template-id token.
3676cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      ConsumeToken();
3686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
3696cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
3706cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      SourceLocation CCLoc = ConsumeToken();
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3726796fc1adcaf57c38d072a238b016b2834afbe0dDavid Blaikie      HasScopeSpecifier = true;
3736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
3745354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer      ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
3756cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                         TemplateId->NumArgs);
3766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
3776cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
378e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                              SS,
379e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                              TemplateId->TemplateKWLoc,
3806cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->Template,
3816cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->TemplateNameLoc,
3826cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->LAngleLoc,
3836cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateArgsPtr,
3846cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->RAngleLoc,
3856cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              CCLoc,
3866cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              EnteringContext)) {
3876cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        SourceLocation StartLoc
3886cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor          = SS.getBeginLoc().isValid()? SS.getBeginLoc()
3896cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                      : TemplateId->TemplateNameLoc;
3906cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        SS.SetInvalid(SourceRange(StartLoc, CCLoc));
39167b9e831943300ce54e564e601971828ce4def15Chris Lattner      }
392eccce7e246a17e12a2afd6eabb9ac7c8d582db4eArgyrios Kyrtzidis
3936cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      continue;
39439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
39539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
3965c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // The rest of the nested-name-specifier possibilities start with
3975c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // tok::identifier.
3985c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Tok.isNot(tok::identifier))
3995c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      break;
4005c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
4015c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    IdentifierInfo &II = *Tok.getIdentifierInfo();
4025c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
4035c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
4045c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '::'
4055c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   namespace-name '::'
4065c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   nested-name-specifier identifier '::'
4075c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    Token Next = NextToken();
40846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
40946646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // If we get foo:bar, this is almost certainly a typo for foo::bar.  Recover
41046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // and emit a fixit hint for it.
411b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor    if (Next.is(tok::colon) && !ColonIsSacred) {
4122e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
4132e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Tok.getLocation(),
4142e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Next.getLocation(), ObjectType,
415b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            EnteringContext) &&
416b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // If the token after the colon isn't an identifier, it's still an
417b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // error, but they probably meant something else strange so don't
418b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // recover like this.
419b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          PP.LookAhead(1).is(tok::identifier)) {
4206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        Diag(Next, diag::err_unexpected_colon_in_nested_name_spec)
421849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateReplacement(Next.getLocation(), "::");
422b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        // Recover as if the user wrote '::'.
423b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Next.setKind(tok::coloncolon);
424b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor      }
42546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    }
42646646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
4275c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::coloncolon)) {
42877549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
42923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
43077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                                II, ObjectType)) {
431d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
4329ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
433d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
434d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
4356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (ColonIsSacred) {
4366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        const Token &Next2 = GetLookAheadToken(2);
4376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (Next2.is(tok::kw_private) || Next2.is(tok::kw_protected) ||
4386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            Next2.is(tok::kw_public) || Next2.is(tok::kw_virtual)) {
4396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          Diag(Next2, diag::err_unexpected_token_in_nested_name_spec)
4406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              << Next2.getName()
4416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              << FixItHint::CreateReplacement(Next.getLocation(), ":");
4426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          Token ColonColon;
4436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          PP.Lex(ColonColon);
4446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          ColonColon.setKind(tok::colon);
4456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          PP.EnterToken(ColonColon);
4466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          break;
4476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        }
4486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      }
4496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
4502db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith      if (LastII)
4512db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith        *LastII = &II;
4522db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith
4535c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // We have an identifier followed by a '::'. Lookup this name
4545c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // as the name in a nested-name-specifier.
4556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Token Identifier = Tok;
4565c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation IdLoc = ConsumeToken();
45746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner      assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
45846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner             "NextToken() not working properly!");
4596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Token ColonColon = Tok;
4605c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation CCLoc = ConsumeToken();
4611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
462919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      CheckForLParenAfterColonColon();
463919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
4646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool IsCorrectedToColon = false;
4656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool *CorrectionFlagPtr = ColonIsSacred ? &IsCorrectedToColon : nullptr;
4662e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
4676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                              ObjectType, EnteringContext, SS,
4686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                              false, CorrectionFlagPtr)) {
4696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        // Identifier is not recognized as a nested name, but we can have
4706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        // mistyped '::' instead of ':'.
4716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (CorrectionFlagPtr && IsCorrectedToColon) {
4726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          ColonColon.setKind(tok::colon);
4736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          PP.EnterToken(Tok);
4746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          PP.EnterToken(ColonColon);
4756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          Tok = Identifier;
4766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          break;
4776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        }
4782e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        SS.SetInvalid(SourceRange(IdLoc, CCLoc));
4796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      }
4806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      HasScopeSpecifier = true;
4815c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      continue;
4825c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
4831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
484950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
485ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
4865c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
4875c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '<'
4885c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::less)) {
4895c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      TemplateTy Template;
490014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
491014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(&II, Tok.getLocation());
4921fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
49323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
4947c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                              /*hasTemplateKeyword=*/false,
495014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                                        TemplateName,
4962dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                                        ObjectType,
497495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        EnteringContext,
4981fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                                        Template,
4991fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                              MemberOfUnknownSpecialization)) {
5006796fc1adcaf57c38d072a238b016b2834afbe0dDavid Blaikie        // We have found a template name, so annotate this token
5015c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // with a template-id annotation. We do not permit the
5025c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // template-id to be translated into a type annotation,
5035c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // because some clients (e.g., the parsing of class template
5045c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // specializations) still want to see the original template-id
5055c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // token.
506ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
507e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
508e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName, false))
5099ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
5105c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
511ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      }
512ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
513d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
5144147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet          (IsTypename || IsTemplateArgumentList(1))) {
515d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // We have something like t::getAs<T>, where getAs is a
516d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // member of an unknown specialization. However, this will only
517d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
518d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
5194147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        unsigned DiagID = diag::err_missing_dependent_template_keyword;
5204e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (getLangOpts().MicrosoftExt)
521cf320c6388c90f1938c264e87d77a0e43946e2c3Francois Pichet          DiagID = diag::warn_missing_dependent_template_keyword;
5224147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet
5234147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        Diag(Tok.getLocation(), DiagID)
524d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << II.getName()
525d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
526d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
527d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TemplateNameKind TNK
52823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor              = Actions.ActOnDependentTemplateName(getCurScope(),
529e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                   SS, SourceLocation(),
530d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   TemplateName, ObjectType,
531d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   EnteringContext, Template)) {
532d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          // Consume the identifier.
533d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          ConsumeToken();
534e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara          if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
535e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      TemplateName, false))
536e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara            return true;
537d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        }
538d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        else
539d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          return true;
540d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor
541d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        continue;
5425c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
5435c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
5445c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
54539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // We don't have any tokens that form the beginning of a
54639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier, so we're done.
54739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    break;
54839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
550d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Even if we didn't see any pieces of a nested-name-specifier, we
551d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // still check whether there is a tilde in this position, which
552d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // indicates a potential pseudo-destructor.
553d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (CheckForDestructor && Tok.is(tok::tilde))
554d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = true;
555d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
5569ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
557eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
558eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
559eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression.
560eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
561eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       id-expression:
562eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         unqualified-id
563eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         qualified-id
564eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
565eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
566eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
567eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' identifier
568eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' operator-function-id
569edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor///         '::' template-id
570eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
571eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not
572eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect:
573eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
574eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' conversion-function-id
575eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' '~' class-name
576eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
577eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics:
578eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
579eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {};
580eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {}
581eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() {
582eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: A :: ~ C(); // Some Sema error about using destructor with a
583eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///                  // namespace.
584eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: ~ C(); // Some Parser error like 'unexpected ~'.
585eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// }
586eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
587eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like:
588eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
589eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
590eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
591eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' unqualified-id
592eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
593eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the
594eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope.
595eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
596ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a
597ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts,
598ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may
599ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear.
600ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl///
60160d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
602eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  // qualified-id:
603eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
604eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::' unqualified-id
605eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //
606eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
607efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
608e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
609e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SourceLocation TemplateKWLoc;
61002a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  UnqualifiedId Name;
611e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  if (ParseUnqualifiedId(SS,
612e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*EnteringContext=*/false,
613e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*AllowDestructorName=*/false,
614e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*AllowConstructorName=*/false,
615b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                         /*ObjectType=*/ ParsedType(),
616e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         TemplateKWLoc,
61702a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         Name))
61802a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor    return ExprError();
619b681b61fea36618778b8030360e90e3f4641233bJohn McCall
620b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // This is only the direct operand of an & operator if it is not
621b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // followed by a postfix-expression suffix.
6229c72c6088d591ace8503b842d39448c2040f3033John McCall  if (isAddressOfOperand && isPostfixExpressionSuffixStart())
6239c72c6088d591ace8503b842d39448c2040f3033John McCall    isAddressOfOperand = false;
624e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
625e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
626e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   Tok.is(tok::l_paren), isAddressOfOperand);
627eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
628eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
6290a664b863255065d960342dd074a77d63c753d35Richard Smith/// ParseLambdaExpression - Parse a C++11 lambda expression.
630ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
631ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-expression:
632ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         lambda-introducer lambda-declarator[opt] compound-statement
633ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
634ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-introducer:
635ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '[' lambda-capture[opt] ']'
636ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
637ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-capture:
638ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-default
639ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-list
640ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-default ',' capture-list
641ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
642ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture-default:
643ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '&'
644ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '='
645ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
646ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture-list:
647ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture
648ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-list ',' capture
649ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
650ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture:
6510a664b863255065d960342dd074a77d63c753d35Richard Smith///         simple-capture
6520a664b863255065d960342dd074a77d63c753d35Richard Smith///         init-capture     [C++1y]
6530a664b863255065d960342dd074a77d63c753d35Richard Smith///
6540a664b863255065d960342dd074a77d63c753d35Richard Smith///       simple-capture:
655ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         identifier
656ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '&' identifier
657ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         'this'
658ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
6590a664b863255065d960342dd074a77d63c753d35Richard Smith///       init-capture:      [C++1y]
6600a664b863255065d960342dd074a77d63c753d35Richard Smith///         identifier initializer
6610a664b863255065d960342dd074a77d63c753d35Richard Smith///         '&' identifier initializer
6620a664b863255065d960342dd074a77d63c753d35Richard Smith///
663ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-declarator:
664ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '(' parameter-declaration-clause ')' attribute-specifier[opt]
665ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///           'mutable'[opt] exception-specification[opt]
666ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///           trailing-return-type[opt]
667ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
668ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::ParseLambdaExpression() {
669ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse lambda-introducer.
670ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  LambdaIntroducer Intro;
6712434dcfb022778b06cfd257d830d0249680b87cfBill Wendling  Optional<unsigned> DiagID = ParseLambdaIntroducer(Intro);
672ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (DiagID) {
673ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Diag(Tok, DiagID.getValue());
6748fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_square, StopAtSemi);
6758fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::l_brace, StopAtSemi);
6768fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_brace, StopAtSemi);
677dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprError();
678ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
679ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
680ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return ParseLambdaExpressionAfterIntroducer(Intro);
681ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
682ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
683ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// TryParseLambdaExpression - Use lookahead and potentially tentative
684ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// parsing to determine if we are looking at a C++0x lambda expression, and parse
685ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// it if we are.
686ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
687ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// If we are not looking at a lambda expression, returns ExprError().
688ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::TryParseLambdaExpression() {
68980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  assert(getLangOpts().CPlusPlus11
690ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor         && Tok.is(tok::l_square)
691ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor         && "Not at the start of a possible lambda expression.");
692ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
693ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  const Token Next = NextToken(), After = GetLookAheadToken(2);
694ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
695ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // If lookahead indicates this is a lambda...
696ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Next.is(tok::r_square) ||     // []
697ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Next.is(tok::equal) ||        // [=
698ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (Next.is(tok::amp) &&         // [&] or [&,
699ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor       (After.is(tok::r_square) ||
700ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        After.is(tok::comma))) ||
701ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (Next.is(tok::identifier) &&  // [identifier]
702ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor       After.is(tok::r_square))) {
703ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    return ParseLambdaExpression();
704ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
705ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
706dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // If lookahead indicates an ObjC message send...
707dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // [identifier identifier
708ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Next.is(tok::identifier) && After.is(tok::identifier)) {
709dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprEmpty();
710ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
7112434dcfb022778b06cfd257d830d0249680b87cfBill Wendling
712dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // Here, we're stuck: lambda introducers and Objective-C message sends are
713dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // unambiguous, but it requires arbitrary lookhead.  [a,b,c,d,e,f,g] is a
714dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send.  Instead of
715dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // writing two routines to parse a lambda introducer, just try to parse
716dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // a lambda introducer first, and fall back if that fails.
717dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // (TryParseLambdaIntroducer never produces any diagnostic output.)
718ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  LambdaIntroducer Intro;
719ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (TryParseLambdaIntroducer(Intro))
720dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprEmpty();
7212434dcfb022778b06cfd257d830d0249680b87cfBill Wendling
722ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return ParseLambdaExpressionAfterIntroducer(Intro);
723ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
724ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
725440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith/// \brief Parse a lambda introducer.
726440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith/// \param Intro A LambdaIntroducer filled in with information about the
727440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith///        contents of the lambda-introducer.
728440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith/// \param SkippedInits If non-null, we are disambiguating between an Obj-C
729440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith///        message send and a lambda expression. In this mode, we will
730440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith///        sometimes skip the initializers for init-captures and not fully
731440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith///        populate \p Intro. This flag will be set to \c true if we do so.
732440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith/// \return A DiagnosticID if it hit something unexpected. The location for
733440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith///         for the diagnostic is that of the current token.
734440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard SmithOptional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
735440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith                                                 bool *SkippedInits) {
736dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie  typedef Optional<unsigned> DiagResult;
737ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
738ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
7394a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_square);
7404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
7414a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
7424a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Intro.Range.setBegin(T.getOpenLocation());
743ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
744ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  bool first = true;
745ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
746ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse capture-default.
747ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Tok.is(tok::amp) &&
748ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
749ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Intro.Default = LCD_ByRef;
7503ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor    Intro.DefaultLoc = ConsumeToken();
751ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    first = false;
752ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  } else if (Tok.is(tok::equal)) {
753ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Intro.Default = LCD_ByCopy;
7543ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor    Intro.DefaultLoc = ConsumeToken();
755ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    first = false;
756ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
757ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
758ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  while (Tok.isNot(tok::r_square)) {
759ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (!first) {
76081f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      if (Tok.isNot(tok::comma)) {
761437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // Provide a completion for a lambda introducer here. Except
762437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // in Objective-C, where this is Almost Surely meant to be a message
763437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // send. In that case, fail here and let the ObjC message
764437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // expression parser perform the completion.
765d48ab06b178e400ac31ef4fe649e9c33d2caf651Douglas Gregor        if (Tok.is(tok::code_completion) &&
766d48ab06b178e400ac31ef4fe649e9c33d2caf651Douglas Gregor            !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
767d48ab06b178e400ac31ef4fe649e9c33d2caf651Douglas Gregor              !Intro.Captures.empty())) {
76881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
76981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                               /*AfterAmpersand=*/false);
7706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          cutOffParsing();
77181f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          break;
77281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        }
77381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
774ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_expected_comma_or_rsquare);
77581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      }
776ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      ConsumeToken();
777ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
778ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
77981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    if (Tok.is(tok::code_completion)) {
78081f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      // If we're in Objective-C++ and we have a bare '[', then this is more
78181f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      // likely to be a message receiver.
7824e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (getLangOpts().ObjC1 && first)
78381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        Actions.CodeCompleteObjCMessageReceiver(getCurScope());
78481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      else
78581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
78681f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                             /*AfterAmpersand=*/false);
7876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      cutOffParsing();
78881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      break;
78981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    }
790ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
79181f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    first = false;
79281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
793ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse capture.
794ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    LambdaCaptureKind Kind = LCK_ByCopy;
795ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation Loc;
7966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    IdentifierInfo *Id = nullptr;
797a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor    SourceLocation EllipsisLoc;
7980a664b863255065d960342dd074a77d63c753d35Richard Smith    ExprResult Init;
799a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor
800ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::kw_this)) {
801ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Kind = LCK_This;
802ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Loc = ConsumeToken();
803ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    } else {
804ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Tok.is(tok::amp)) {
805ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Kind = LCK_ByRef;
806ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        ConsumeToken();
80781f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
80881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        if (Tok.is(tok::code_completion)) {
80981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
81081f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                               /*AfterAmpersand=*/true);
8116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          cutOffParsing();
81281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          break;
81381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        }
814ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      }
815ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
816ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Tok.is(tok::identifier)) {
817ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Id = Tok.getIdentifierInfo();
818ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Loc = ConsumeToken();
819ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      } else if (Tok.is(tok::kw_this)) {
820ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // FIXME: If we want to suggest a fixit here, will need to return more
821ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
822ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // Clear()ed to prevent emission in case of tentative parsing?
823ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_this_captured_by_reference);
824ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      } else {
825ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_expected_capture);
826ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      }
8270a664b863255065d960342dd074a77d63c753d35Richard Smith
8280a664b863255065d960342dd074a77d63c753d35Richard Smith      if (Tok.is(tok::l_paren)) {
8290a664b863255065d960342dd074a77d63c753d35Richard Smith        BalancedDelimiterTracker Parens(*this, tok::l_paren);
8300a664b863255065d960342dd074a77d63c753d35Richard Smith        Parens.consumeOpen();
8310a664b863255065d960342dd074a77d63c753d35Richard Smith
8320a664b863255065d960342dd074a77d63c753d35Richard Smith        ExprVector Exprs;
8330a664b863255065d960342dd074a77d63c753d35Richard Smith        CommaLocsTy Commas;
834440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        if (SkippedInits) {
835440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          Parens.skipToEnd();
836440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          *SkippedInits = true;
837440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        } else if (ParseExpressionList(Exprs, Commas)) {
8380a664b863255065d960342dd074a77d63c753d35Richard Smith          Parens.skipToEnd();
8390a664b863255065d960342dd074a77d63c753d35Richard Smith          Init = ExprError();
8400a664b863255065d960342dd074a77d63c753d35Richard Smith        } else {
8410a664b863255065d960342dd074a77d63c753d35Richard Smith          Parens.consumeClose();
8420a664b863255065d960342dd074a77d63c753d35Richard Smith          Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
8430a664b863255065d960342dd074a77d63c753d35Richard Smith                                            Parens.getCloseLocation(),
8440a664b863255065d960342dd074a77d63c753d35Richard Smith                                            Exprs);
8450a664b863255065d960342dd074a77d63c753d35Richard Smith        }
8460a664b863255065d960342dd074a77d63c753d35Richard Smith      } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
8472434dcfb022778b06cfd257d830d0249680b87cfBill Wendling        // Each lambda init-capture forms its own full expression, which clears
8482434dcfb022778b06cfd257d830d0249680b87cfBill Wendling        // Actions.MaybeODRUseExprs. So create an expression evaluation context
8492434dcfb022778b06cfd257d830d0249680b87cfBill Wendling        // to save the necessary state, and restore it later.
8502434dcfb022778b06cfd257d830d0249680b87cfBill Wendling        EnterExpressionEvaluationContext EC(Actions,
8512434dcfb022778b06cfd257d830d0249680b87cfBill Wendling                                            Sema::PotentiallyEvaluated);
852651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        TryConsumeToken(tok::equal);
8530a664b863255065d960342dd074a77d63c753d35Richard Smith
854440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        if (!SkippedInits)
855440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          Init = ParseInitializer();
856440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        else if (Tok.is(tok::l_brace)) {
857440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          BalancedDelimiterTracker Braces(*this, tok::l_brace);
858440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          Braces.consumeOpen();
859440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          Braces.skipToEnd();
860440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          *SkippedInits = true;
861440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        } else {
862440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // We're disambiguating this:
863440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          //
864440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          //   [..., x = expr
865440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          //
866440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // We need to find the end of the following expression in order to
8676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          // determine whether this is an Obj-C message send's receiver, a
8686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          // C99 designator, or a lambda init-capture.
869440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          //
870440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // Parse the expression to find where it ends, and annotate it back
871440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // onto the tokens. We would have parsed this expression the same way
872440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // in either case: both the RHS of an init-capture and the RHS of an
873440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // assignment expression are parsed as an initializer-clause, and in
874440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // neither case can anything be added to the scope between the '[' and
875440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // here.
876440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          //
877440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // FIXME: This is horrible. Adding a mechanism to skip an expression
878440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // would be much cleaner.
879440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // FIXME: If there is a ',' before the next ']' or ':', we can skip to
880440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // that instead. (And if we see a ':' with no matching '?', we can
881440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // classify this as an Obj-C message send.)
882440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          SourceLocation StartLoc = Tok.getLocation();
883440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
884440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          Init = ParseInitializer();
885440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith
886440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          if (Tok.getLocation() != StartLoc) {
887440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            // Back out the lexing of the token after the initializer.
888440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            PP.RevertCachedTokens(1);
889440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith
890440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            // Replace the consumed tokens with an appropriate annotation.
891440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            Tok.setLocation(StartLoc);
892440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            Tok.setKind(tok::annot_primary_expr);
893440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            setExprAnnotation(Tok, Init);
894440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
895440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            PP.AnnotateCachedTokens(Tok);
896440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith
897440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            // Consume the annotated initializer.
898440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            ConsumeToken();
899440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          }
900440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        }
901651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      } else
902651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        TryConsumeToken(tok::ellipsis, EllipsisLoc);
903ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
9042434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // If this is an init capture, process the initialization expression
9052434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // right away.  For lambda init-captures such as the following:
9062434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // const int x = 10;
9072434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //  auto L = [i = x+1](int a) {
9082434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //    return [j = x+2,
9092434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //           &k = x](char b) { };
9102434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //  };
9112434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // keep in mind that each lambda init-capture has to have:
9122434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //  - its initialization expression executed in the context
9132434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //    of the enclosing/parent decl-context.
9142434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //  - but the variable itself has to be 'injected' into the
9152434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //    decl-context of its lambda's call-operator (which has
9162434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //    not yet been created).
9172434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // Each init-expression is a full-expression that has to get
9182434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // Sema-analyzed (for capturing etc.) before its lambda's
9192434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // call-operator's decl-context, scope & scopeinfo are pushed on their
9202434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // respective stacks.  Thus if any variable is odr-used in the init-capture
9212434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // it will correctly get captured in the enclosing lambda, if one exists.
9222434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // The init-variables above are created later once the lambdascope and
9232434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // call-operators decl-context is pushed onto its respective stack.
9242434dcfb022778b06cfd257d830d0249680b87cfBill Wendling
9252434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // Since the lambda init-capture's initializer expression occurs in the
9262434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // context of the enclosing function or lambda, therefore we can not wait
9272434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // till a lambda scope has been pushed on before deciding whether the
9282434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // variable needs to be captured.  We also need to process all
9292434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // lvalue-to-rvalue conversions and discarded-value conversions,
9302434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // so that we can avoid capturing certain constant variables.
9312434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // For e.g.,
9322434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //  void test() {
9332434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //   const int x = 10;
9342434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //   auto L = [&z = x](char a) { <-- don't capture by the current lambda
9352434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //     return [y = x](int i) { <-- don't capture by enclosing lambda
9362434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //          return y;
9372434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //     }
9382434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    //   };
9392434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // If x was not const, the second use would require 'L' to capture, and
9402434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    // that would be an error.
9412434dcfb022778b06cfd257d830d0249680b87cfBill Wendling
9422434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    ParsedType InitCaptureParsedType;
9432434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    if (Init.isUsable()) {
9442434dcfb022778b06cfd257d830d0249680b87cfBill Wendling      // Get the pointer and store it in an lvalue, so we can use it as an
9452434dcfb022778b06cfd257d830d0249680b87cfBill Wendling      // out argument.
9462434dcfb022778b06cfd257d830d0249680b87cfBill Wendling      Expr *InitExpr = Init.get();
9472434dcfb022778b06cfd257d830d0249680b87cfBill Wendling      // This performs any lvalue-to-rvalue conversions if necessary, which
9482434dcfb022778b06cfd257d830d0249680b87cfBill Wendling      // can affect what gets captured in the containing decl-context.
9492434dcfb022778b06cfd257d830d0249680b87cfBill Wendling      QualType InitCaptureType = Actions.performLambdaInitCaptureInitialization(
9502434dcfb022778b06cfd257d830d0249680b87cfBill Wendling        Loc, Kind == LCK_ByRef, Id, InitExpr);
9512434dcfb022778b06cfd257d830d0249680b87cfBill Wendling      Init = InitExpr;
9522434dcfb022778b06cfd257d830d0249680b87cfBill Wendling      InitCaptureParsedType.set(InitCaptureType);
9532434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    }
9542434dcfb022778b06cfd257d830d0249680b87cfBill Wendling    Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init, InitCaptureParsedType);
955ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
956ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
9574a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
9584a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Intro.Range.setEnd(T.getCloseLocation());
959ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return DiagResult();
960ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
961ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
96281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
963ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
964ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// Returns true if it hit something unexpected.
965ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregorbool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
966ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  TentativeParsingAction PA(*this);
967ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
968440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  bool SkippedInits = false;
969440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
970ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
971ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (DiagID) {
972ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    PA.Revert();
973ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    return true;
974ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
975ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
976440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  if (SkippedInits) {
977440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    // Parse it again, but this time parse the init-captures too.
978440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    PA.Revert();
979440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    Intro = LambdaIntroducer();
980440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    DiagID = ParseLambdaIntroducer(Intro);
981440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    assert(!DiagID && "parsing lambda-introducer failed on reparse");
982440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    return false;
983440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  }
984440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith
985ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  PA.Commit();
986ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return false;
987ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
988ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
989ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
990ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// expression.
991ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::ParseLambdaExpressionAfterIntroducer(
992ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                     LambdaIntroducer &Intro) {
993dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
994dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
995dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
996dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
997dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman                                "lambda expression parsing");
998dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
999fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
1000fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
10010a664b863255065d960342dd074a77d63c753d35Richard Smith  // FIXME: Call into Actions to add any init-capture declarations to the
10020a664b863255065d960342dd074a77d63c753d35Richard Smith  // scope while parsing the lambda-declarator and compound-statement.
10030a664b863255065d960342dd074a77d63c753d35Richard Smith
1004ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse lambda-declarator[opt].
1005ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  DeclSpec DS(AttrFactory);
1006f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman  Declarator D(DS, Declarator::LambdaExprContext);
1007fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali  TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
1008fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali  Actions.PushLambdaScope();
1009ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1010ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Tok.is(tok::l_paren)) {
1011ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ParseScope PrototypeScope(this,
1012ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                              Scope::FunctionPrototypeScope |
10133a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith                              Scope::FunctionDeclarationScope |
1014ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                              Scope::DeclScope);
1015ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
101659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation DeclEndLoc;
10174a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
10184a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
101959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation LParenLoc = T.getOpenLocation();
1020ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1021ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse parameter-declaration-clause.
1022ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ParsedAttributes Attr(AttrFactory);
1023cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1024ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation EllipsisLoc;
1025fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
1026fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    if (Tok.isNot(tok::r_paren)) {
1027fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      Actions.RecordParsingTemplateParameterDepth(TemplateParameterDepth);
1028ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
1029fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // For a generic lambda, each 'auto' within the parameter declaration
1030fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // clause creates a template type parameter, so increment the depth.
1031fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      if (Actions.getCurGenericLambda())
1032fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        ++CurTemplateDepthTracker;
1033fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    }
10344a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
103559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation RParenLoc = T.getCloseLocation();
103659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    DeclEndLoc = RParenLoc;
1037ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1038651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // GNU-style attributes must be parsed before the mutable specifier to be
1039651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // compatible with GCC.
1040651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    MaybeParseGNUAttributes(Attr, &DeclEndLoc);
1041651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1042ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse 'mutable'[opt].
1043ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation MutableLoc;
1044651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (TryConsumeToken(tok::kw_mutable, MutableLoc))
1045ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      DeclEndLoc = MutableLoc;
1046ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1047ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse exception-specification[opt].
1048ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ExceptionSpecificationType ESpecType = EST_None;
1049ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceRange ESpecRange;
1050cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<ParsedType, 2> DynamicExceptions;
1051cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<SourceRange, 2> DynamicExceptionRanges;
1052ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ExprResult NoexceptExpr;
1053a058fd4f0a944174295f77169b438510dad389f8Richard Smith    ESpecType = tryParseExceptionSpecification(ESpecRange,
105474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                               DynamicExceptions,
105574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                               DynamicExceptionRanges,
1056a058fd4f0a944174295f77169b438510dad389f8Richard Smith                                               NoexceptExpr);
1057ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1058ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (ESpecType != EST_None)
1059ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      DeclEndLoc = ESpecRange.getEnd();
1060ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1061ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse attribute-specifier[opt].
10624e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
1063ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
106459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation FunLocalRangeEnd = DeclEndLoc;
106559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara
1066ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse trailing-return-type[opt].
106754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith    TypeResult TrailingReturnType;
1068ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::arrow)) {
106959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      FunLocalRangeEnd = Tok.getLocation();
1070ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      SourceRange Range;
107154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith      TrailingReturnType = ParseTrailingReturnType(Range);
1072ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Range.getEnd().isValid())
1073ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        DeclEndLoc = Range.getEnd();
1074ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
1075ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1076ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    PrototypeScope.Exit();
1077ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
107859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation NoLoc;
1079ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
108059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*isAmbiguous=*/false,
108159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           LParenLoc,
1082ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           ParamInfo.data(), ParamInfo.size(),
108359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           EllipsisLoc, RParenLoc,
1084ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DS.getTypeQualifiers(),
1085ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           /*RefQualifierIsLValueRef=*/true,
108659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*RefQualifierLoc=*/NoLoc,
108759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*ConstQualifierLoc=*/NoLoc,
108859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*VolatileQualifierLoc=*/NoLoc,
1089ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           MutableLoc,
1090ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           ESpecType, ESpecRange.getBegin(),
1091ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptions.data(),
1092ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptionRanges.data(),
1093ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptions.size(),
1094ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           NoexceptExpr.isUsable() ?
10956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                             NoexceptExpr.get() : nullptr,
109659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           LParenLoc, FunLocalRangeEnd, D,
1097ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           TrailingReturnType),
1098ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                  Attr, DeclEndLoc);
1099651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow) ||
1100651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines             Tok.is(tok::kw___attribute) ||
1101651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines             (Tok.is(tok::l_square) && NextToken().is(tok::l_square))) {
1102651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // It's common to forget that one needs '()' before 'mutable', an attribute
1103651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // specifier, or the result type. Deal with this.
1104651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned TokKind = 0;
1105651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    switch (Tok.getKind()) {
1106651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::kw_mutable: TokKind = 0; break;
1107651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::arrow: TokKind = 1; break;
1108651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::kw___attribute:
1109651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case tok::l_square: TokKind = 2; break;
1110651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    default: llvm_unreachable("Unknown token kind");
1111651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
1112651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1113c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    Diag(Tok, diag::err_lambda_missing_parens)
1114651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      << TokKind
1115c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
1116c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    SourceLocation DeclLoc = Tok.getLocation();
1117c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    SourceLocation DeclEndLoc = DeclLoc;
1118651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1119651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // GNU-style attributes must be parsed before the mutable specifier to be
1120651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // compatible with GCC.
1121651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ParsedAttributes Attr(AttrFactory);
1122651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    MaybeParseGNUAttributes(Attr, &DeclEndLoc);
1123651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1124c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    // Parse 'mutable', if it's there.
1125c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    SourceLocation MutableLoc;
1126c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    if (Tok.is(tok::kw_mutable)) {
1127c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      MutableLoc = ConsumeToken();
1128c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      DeclEndLoc = MutableLoc;
1129c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    }
1130651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1131651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // Parse attribute-specifier[opt].
1132651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
1133651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1134c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    // Parse the return type, if there is one.
113554655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith    TypeResult TrailingReturnType;
1136c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    if (Tok.is(tok::arrow)) {
1137c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      SourceRange Range;
113854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith      TrailingReturnType = ParseTrailingReturnType(Range);
1139c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      if (Range.getEnd().isValid())
1140c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor        DeclEndLoc = Range.getEnd();
1141c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    }
1142c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor
114359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation NoLoc;
1144c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
114559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*isAmbiguous=*/false,
114659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*LParenLoc=*/NoLoc,
11476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                               /*Params=*/nullptr,
114859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*NumParams=*/0,
114959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*EllipsisLoc=*/NoLoc,
115059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*RParenLoc=*/NoLoc,
115159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*TypeQuals=*/0,
115259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*RefQualifierIsLValueRef=*/true,
115359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*RefQualifierLoc=*/NoLoc,
115459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*ConstQualifierLoc=*/NoLoc,
115559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*VolatileQualifierLoc=*/NoLoc,
115659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               MutableLoc,
115759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               EST_None,
115859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*ESpecLoc=*/NoLoc,
11596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                               /*Exceptions=*/nullptr,
11606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                               /*ExceptionRanges=*/nullptr,
116159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*NumExceptions=*/0,
11626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                               /*NoexceptExpr=*/nullptr,
116359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               DeclLoc, DeclEndLoc, D,
116459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               TrailingReturnType),
1165c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor                  Attr, DeclEndLoc);
1166ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
1167c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor
1168ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1169906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1170906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  // it.
1171fccfb625e3090e77da9b6a79edcab159c7006685Douglas Gregor  unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
1172fccfb625e3090e77da9b6a79edcab159c7006685Douglas Gregor  ParseScope BodyScope(this, ScopeFlags);
1173906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman
1174ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman  Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1175ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman
1176ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse compound-statement.
1177dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  if (!Tok.is(tok::l_brace)) {
1178ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Diag(Tok, diag::err_expected_lambda_body);
1179dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1180dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprError();
1181ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
1182ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1183dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  StmtResult Stmt(ParseCompoundStatementBody());
1184dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  BodyScope.Exit();
1185dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
1186deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  if (!Stmt.isInvalid())
1187ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.get(), getCurScope());
1188dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
1189deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1190deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  return ExprError();
1191ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
1192ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type.
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       postfix-expression: [C++ 5.2p1]
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'dynamic_cast' '<' type-name '>' '(' expression ')'
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'static_cast' '<' type-name '>' '(' expression ')'
11995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
12005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'const_cast' '<' type-name '>' '(' expression ')'
12015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
120260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXCasts() {
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
12046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const char *CastName = nullptr; // For error messages
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Kind) {
1207eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie  default: llvm_unreachable("Unknown C++ cast!");
12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_const_cast:       CastName = "const_cast";       break;
12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
12115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_static_cast:      CastName = "static_cast";      break;
12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation OpLoc = ConsumeToken();
12155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LAngleBracketLoc = Tok.getLocation();
12165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1217ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Check for "<::" which is parsed as "[:".  If found, fix token stream,
1218ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // diagnose error, suggest fix, and recover parsing.
121978fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith  if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
122078fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith    Token Next = NextToken();
122178fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith    if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
122278fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith      FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
122378fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith  }
1224ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
122620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
122831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Parse the common declaration-specifiers piece.
122931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclSpec DS(AttrFactory);
123031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
123131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
123231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Parse the abstract-declarator, if present.
123331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
123431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
123531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation RAngleBracketLoc = Tok.getLocation();
12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1238651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (ExpectAndConsume(tok::greater))
1239651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << tok::less);
12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12414a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  SourceLocation LParenLoc, RParenLoc;
12424a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
124521e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    return ExprError();
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
124760d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = ParseExpression();
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124921e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  // Match the ')'.
12504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
125231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
125349badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
125431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                       LAngleBracketLoc, DeclaratorInfo,
1255809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                       RAngleBracketLoc,
1256ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                       T.getOpenLocation(), Result.get(),
12574a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
12585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12593fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
12605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1262c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression.
1263c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
1264c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///       postfix-expression: [C++ 5.2p1]
1265c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' expression ')'
1266c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' type-id ')'
1267c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
126860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXTypeid() {
1269c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1270c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1271c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation OpLoc = ConsumeToken();
12724a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  SourceLocation LParenLoc, RParenLoc;
12734a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
1274c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1275c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  // typeid expressions are always parenthesized.
12764a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
127720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
12784a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  LParenLoc = T.getOpenLocation();
1279c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
128060d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result;
1281c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
12820576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // C++0x [expr.typeid]p3:
12830576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //   When typeid is applied to an expression other than an lvalue of a
12840576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //   polymorphic class type [...] The expression is an unevaluated
12850576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //   operand (Clause 5).
12860576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //
12870576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Note that we can't tell whether the expression is an lvalue of a
12880576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // polymorphic class type until after we've parsed the expression; we
12890576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // speculatively assume the subexpression is unevaluated, and fix it up
12900576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // later.
12910576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //
12920576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // We enter the unevaluated context before trying to determine whether we
12930576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // have a type-id, because the tentative parse logic will try to resolve
12940576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // names, and must treat them as unevaluated.
129580bfa3d125fa0b9c636977ea37b4a55b2c9b1037Eli Friedman  EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
129680bfa3d125fa0b9c636977ea37b4a55b2c9b1037Eli Friedman                                               Sema::ReuseLambdaContextDecl);
12970576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
1298c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (isTypeIdInParens()) {
1299809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult Ty = ParseTypeName();
1300c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1301c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
13024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
13034a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    RParenLoc = T.getCloseLocation();
13044eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    if (Ty.isInvalid() || RParenLoc.isInvalid())
130520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1306c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1307c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
1308b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                    Ty.get().getAsOpaquePtr(), RParenLoc);
1309c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  } else {
1310c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = ParseExpression();
1311c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1312c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
13130e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Result.isInvalid())
13148fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::r_paren, StopAtSemi);
1315c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    else {
13164a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
13174a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      RParenLoc = T.getCloseLocation();
13184eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor      if (RParenLoc.isInvalid())
13194eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor        return ExprError();
1320fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor
1321c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
1322ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                      Result.get(), RParenLoc);
1323c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    }
1324c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  }
1325c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
13263fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
1327c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
1328c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
132901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
133001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
133101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' expression ')'
133201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' type-id ')'
133301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
133401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetExprResult Parser::ParseCXXUuidof() {
133501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
133601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
133701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  SourceLocation OpLoc = ConsumeToken();
13384a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
133901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
134001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  // __uuidof expressions are always parenthesized.
13414a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
134201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    return ExprError();
134301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
134401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  ExprResult Result;
134501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
134601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  if (isTypeIdInParens()) {
134701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    TypeResult Ty = ParseTypeName();
134801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
134901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
13504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
135101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
135201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Ty.isInvalid())
135301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      return ExprError();
135401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
13554a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
13564a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    Ty.get().getAsOpaquePtr(),
13574a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    T.getCloseLocation());
135801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  } else {
135901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
136001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    Result = ParseExpression();
136101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
136201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
136301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Result.isInvalid())
13648fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::r_paren, StopAtSemi);
136501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    else {
13664a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
136701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
13684a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
13694a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      /*isType=*/false,
1370ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                      Result.get(), T.getCloseLocation());
137101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    }
137201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  }
137301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
13743fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
137501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet}
137601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
1377d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \brief Parse a C++ pseudo-destructor expression after the base,
1378d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// . or -> operator, and nested-name-specifier have already been
1379d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// parsed.
1380d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
1381d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       postfix-expression: [C++ 5.2]
1382d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression . pseudo-destructor-name
1383d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression -> pseudo-destructor-name
1384d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
1385d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       pseudo-destructor-name:
1386d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1387d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier template simple-template-id ::
1388d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///                 ~type-name
1389d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] ~type-name
1390d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
139160d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
1392d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1393d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 tok::TokenKind OpKind,
1394d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 CXXScopeSpec &SS,
1395b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                 ParsedType ObjectType) {
1396d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // We're parsing either a pseudo-destructor-name or a dependent
1397d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // member access that has the same form as a
1398d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // pseudo-destructor-name. We parse both in the same way and let
1399d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // the action model sort them out.
1400d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  //
1401d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Note that the ::[opt] nested-name-specifier[opt] has already
1402d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been parsed, and if there was a simple-template-id, it has
1403d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been coalesced into a template-id annotation token.
1404d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId FirstTypeName;
1405d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation CCLoc;
1406d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::identifier)) {
1407d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1408d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
1409d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1410d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
1411d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else if (Tok.is(tok::annot_template_id)) {
1412e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    // FIXME: retrieve TemplateKWLoc from template-id annotation and
1413e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    // store it in the pseudo-dtor node (to be used when instantiating it).
1414d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setTemplateId(
1415d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                              (TemplateIdAnnotation *)Tok.getAnnotationValue());
1416d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
1417d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1418d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
1419d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else {
14206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    FirstTypeName.setIdentifier(nullptr, SourceLocation());
1421d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
1422d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1423d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the tilde.
1424d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1425d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation TildeLoc = ConsumeToken();
142691ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
142791ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
142891ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    DeclSpec DS(AttrFactory);
142985c60db2131c6d210d4777c3d50bdaf0e69bb8bfBenjamin Kramer    ParseDecltypeSpecifier(DS);
143091ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    if (DS.getTypeSpecType() == TST_error)
143191ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie      return ExprError();
143291ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
143391ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                             OpKind, TildeLoc, DS,
143491ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                             Tok.is(tok::l_paren));
143591ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  }
143691ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
1437d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (!Tok.is(tok::identifier)) {
1438d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    Diag(Tok, diag::err_destructor_tilde_identifier);
1439d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
1440d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
1441d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1442d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the second type.
1443d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId SecondTypeName;
1444d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  IdentifierInfo *Name = Tok.getIdentifierInfo();
1445d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation NameLoc = ConsumeToken();
1446d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SecondTypeName.setIdentifier(Name, NameLoc);
1447d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1448d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // If there is a '<', the second type name is a template-id. Parse
1449d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // it as such.
1450d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::less) &&
1451e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1452e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   Name, NameLoc,
1453e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   false, ObjectType, SecondTypeName,
1454e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   /*AssumeTemplateName=*/true))
1455d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
1456d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
14579ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
14589ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                           OpLoc, OpKind,
1459d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           SS, FirstTypeName, CCLoc,
1460d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           TildeLoc, SecondTypeName,
1461d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           Tok.is(tok::l_paren));
1462d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor}
1463d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
14645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
14655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
14665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       boolean-literal: [C++ 2.13.5]
14675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'true'
14685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'false'
146960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXBoolLiteral() {
14705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
1471f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
14725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
147350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
147450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression.
147550dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///
147650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///       throw-expression: [C++ 15]
147750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///         'throw' assignment-expression[opt]
147860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseThrowExpression() {
147950dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  assert(Tok.is(tok::kw_throw) && "Not throw!");
148050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
148120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl
14822a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // If the current token isn't the start of an assignment-expression,
14832a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // then the expression is not present.  This handles things like:
14842a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  //   "C ? throw : (void)42", which is crazy but legal.
14852a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
14862a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::semi:
14872a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_paren:
14882a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_square:
14892a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_brace:
14902a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::colon:
14912a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::comma:
14926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, nullptr);
149350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
14942a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  default:
149560d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Expr(ParseAssignmentExpression());
14963fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    if (Expr.isInvalid()) return Expr;
1497ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.get());
14982a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  }
149950dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
15004cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
15014cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer.
15024cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
15034cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
15044cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which
15054cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called.
150660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXThis() {
15074cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  assert(Tok.is(tok::kw_this) && "Not 'this'!");
15084cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  SourceLocation ThisLoc = ConsumeToken();
1509f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXThis(ThisLoc);
15104cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
1511987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1512987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1513987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)")
1514987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)")
1515987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()").
1516dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// See [C++ 5.2.3].
1517987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1518987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       postfix-expression: [C++ 5.2p1]
1519dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl///         simple-type-specifier '(' expression-list[opt] ')'
1520dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] simple-type-specifier braced-init-list
1521dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl///         typename-specifier '(' expression-list[opt] ')'
1522dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] typename-specifier braced-init-list
1523987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
152460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
152520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
1526987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1527b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
1528987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1529dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  assert((Tok.is(tok::l_paren) ||
153080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
1531dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl         && "Expected '(' or '{'!");
1532bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor
1533dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  if (Tok.is(tok::l_brace)) {
15346dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    ExprResult Init = ParseBraceInitializer();
15356dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    if (Init.isInvalid())
15366dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl      return Init;
1537ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    Expr *InitList = Init.get();
15386dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
15396dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl                                             MultiExprArg(&InitList, 1),
15406dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl                                             SourceLocation());
1541dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  } else {
15424a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
15434a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
1544dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
15454e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer    ExprVector Exprs;
1546dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    CommaLocsTy CommaLocs;
1547dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
1548dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (Tok.isNot(tok::r_paren)) {
1549dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      if (ParseExpressionList(Exprs, CommaLocs)) {
15508fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        SkipUntil(tok::r_paren, StopAtSemi);
1551dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl        return ExprError();
1552dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      }
1553987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    }
1554987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1555dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // Match the ')'.
15564a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
1557987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1558dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // TypeRep could be null, if it references an invalid typedef.
1559dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (!TypeRep)
1560dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      return ExprError();
1561ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl
1562dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1563dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl           "Unexpected number of commas!");
15644a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
15653fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                             Exprs,
15664a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                             T.getCloseLocation());
1567dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  }
1568987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
1569987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
157099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// ParseCXXCondition - if/switch/while condition expression.
157171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
157271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///       condition:
157371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         expression
157471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         type-specifier-seq declarator '=' assignment-expression
15750635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith/// [C++11] type-specifier-seq declarator '=' initializer-clause
15760635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith/// [C++11] type-specifier-seq declarator braced-init-list
157771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
157871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///             '=' assignment-expression
157971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
15801ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param ExprOut if the condition was parsed as an expression, the parsed
15811ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// expression.
158299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
15831ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param DeclOut if the condition was parsed as a declaration, the parsed
15841ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// declaration.
158599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
1586586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param Loc The location of the start of the statement that requires this
1587586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// condition, e.g., the "for" in a for loop.
1588586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
1589586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param ConvertToBoolean Whether the condition expression should be
1590586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// converted to a boolean value.
1591586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
159299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \returns true if there was a parsing, false otherwise.
159360d7b3a319d84d688752be3870615ac0f111fb16John McCallbool Parser::ParseCXXCondition(ExprResult &ExprOut,
159460d7b3a319d84d688752be3870615ac0f111fb16John McCall                               Decl *&DeclOut,
1595586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               SourceLocation Loc,
1596586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               bool ConvertToBoolean) {
159701dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  if (Tok.is(tok::code_completion)) {
1598f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
15997d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
16007d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return true;
160101dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  }
160201dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor
16032edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  ParsedAttributesWithRange attrs(AttrFactory);
16044e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  MaybeParseCXX11Attributes(attrs);
16052edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
160699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!isCXXConditionDeclaration()) {
16072edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ProhibitAttributes(attrs);
16082edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
1609586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // Parse the expression.
161060d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprOut = ParseExpression(); // expression
16116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    DeclOut = nullptr;
161260d7b3a319d84d688752be3870615ac0f111fb16John McCall    if (ExprOut.isInvalid())
1613586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return true;
1614586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
1615586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // If required, convert to a boolean value.
1616586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    if (ConvertToBoolean)
161760d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprOut
161860d7b3a319d84d688752be3870615ac0f111fb16John McCall        = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
161960d7b3a319d84d688752be3870615ac0f111fb16John McCall    return ExprOut.isInvalid();
162099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
162171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
162271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // type-specifier-seq
16230b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
16246b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  DS.takeAttributesFrom(attrs);
162571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
162671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
162771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // declarator
162871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
162971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
163071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
163171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // simple-asm-expr[opt]
163271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.is(tok::kw_asm)) {
1633ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
163460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult AsmLabel(ParseSimpleAsm(&Loc));
16350e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (AsmLabel.isInvalid()) {
16368fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopAtSemi);
163799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return true;
163871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    }
1639ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    DeclaratorInfo.setAsmLabel(AsmLabel.get());
1640ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.SetRangeEnd(Loc);
164171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  }
164271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
164371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // If attributes are present, parse them.
16447f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseGNUAttributes(DeclaratorInfo);
164571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
164699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  // Type-check the declaration itself.
164760d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
16487f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                        DeclaratorInfo);
164960d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclOut = Dcl.get();
165060d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprOut = ExprError();
1651a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
165271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // '=' assignment-expression
1653d6c7c67313634b317a0d63c32be0511a121bb33dRichard Trieu  // If a '==' or '+=' is found, suggest a fixit to '='.
16540635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  bool CopyInitialization = isTokenEqualOrEqualTypo();
16550635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  if (CopyInitialization)
1656dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeToken();
16570635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith
16580635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  ExprResult InitExpr = ExprError();
165980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
16600635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Diag(Tok.getLocation(),
16610635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith         diag::warn_cxx98_compat_generalized_initializer_lists);
16620635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    InitExpr = ParseBraceInitializer();
16630635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  } else if (CopyInitialization) {
16640635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    InitExpr = ParseAssignmentExpression();
16650635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  } else if (Tok.is(tok::l_paren)) {
16660635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    // This was probably an attempt to initialize the variable.
16670635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    SourceLocation LParen = ConsumeParen(), RParen = LParen;
16688fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch))
16690635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith      RParen = ConsumeParen();
16700635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Diag(DeclOut ? DeclOut->getLocation() : LParen,
16710635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith         diag::err_expected_init_in_condition_lparen)
16720635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith      << SourceRange(LParen, RParen);
167399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  } else {
16740635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
16750635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith         diag::err_expected_init_in_condition);
167699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
16770635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith
16780635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  if (!InitExpr.isInvalid())
1679ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    Actions.AddInitializerToDecl(DeclOut, InitExpr.get(), !CopyInitialization,
1680a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                                 DS.containsPlaceholderType());
1681dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith  else
1682dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith    Actions.ActOnInitializerError(DeclOut);
16830635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith
1684586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // FIXME: Build a reference to this declaration? Convert it to bool?
1685586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // (This is currently handled by Sema).
1686483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
1687483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Actions.FinalizeDeclaration(DeclOut);
1688586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
168999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return false;
169071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis}
169171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
1692987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1693987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of
1694987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier.
1695987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1696987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       simple-type-specifier:
1697eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier[opt] type-name
1698987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1699987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         char
1700987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         wchar_t
1701987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         bool
1702987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         short
1703987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         int
1704987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         long
1705987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         signed
1706987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         unsigned
1707987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         float
1708987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         double
1709987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         void
1710987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU]   typeof-specifier
1711987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto               [TODO]
1712987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1713987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       type-name:
1714987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         class-name
1715987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         enum-name
1716987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typedef-name
1717987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1718987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1719987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  DS.SetRangeStart(Tok.getLocation());
1720987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  const char *PrevSpec;
1721fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
1722987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation Loc = Tok.getLocation();
1723651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const clang::PrintingPolicy &Policy =
1724651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Actions.getASTContext().getPrintingPolicy();
17251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1726987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  switch (Tok.getKind()) {
172755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::identifier:   // foo::bar
172855a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::coloncolon:   // ::foo::bar
1729b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Annotation token should already be formed!");
17301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
1731b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Not a simple-type-specifier token!");
173255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner
1733987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // type-name
1734b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  case tok::annot_typename: {
17356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (getTypeAnnotation(Tok))
17366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1737651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         getTypeAnnotation(Tok), Policy);
17386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    else
17396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecError();
17409bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
17419bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
17429bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    ConsumeToken();
17439bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
17449bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
17459bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
17469bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C interface.  If we don't have Objective-C or a '<', this is
17479bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // just a normal reference to a typedef name.
17484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Tok.is(tok::less) && getLangOpts().ObjC1)
17499bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor      ParseObjCProtocolQualifiers(DS);
17509bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
1751651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.Finish(Diags, PP, Policy);
17529bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    return;
1753987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
17541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1755987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // builtin types
1756987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_short:
1757651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID, Policy);
1758987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1759987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_long:
1760651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID, Policy);
1761338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet    break;
1762338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet  case tok::kw___int64:
1763651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID, Policy);
1764987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1765987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_signed:
1766fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
1767987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1768987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_unsigned:
1769fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
1770987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1771987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_void:
1772651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID, Policy);
1773987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1774987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_char:
1775651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID, Policy);
1776987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1777987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_int:
1778651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID, Policy);
1779987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
17805a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case tok::kw___int128:
1781651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID, Policy);
17825a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
1783aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case tok::kw_half:
1784651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID, Policy);
1785aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    break;
1786987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_float:
1787651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID, Policy);
1788987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1789987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_double:
1790651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID, Policy);
1791987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1792987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_wchar_t:
1793651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID, Policy);
1794987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1795f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char16_t:
1796651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID, Policy);
1797f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1798f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char32_t:
1799651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID, Policy);
1800f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1801987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_bool:
1802651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID, Policy);
1803987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
18045e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie  case tok::annot_decltype:
18055e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie  case tok::kw_decltype:
18065e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie    DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1807651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return DS.Finish(Diags, PP, Policy);
18081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1809987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // GNU typeof support.
1810987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_typeof:
1811987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    ParseTypeofSpecifier(DS);
1812651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DS.Finish(Diags, PP, Policy);
1813987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    return;
1814987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
1815b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  if (Tok.is(tok::annot_typename))
1816eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1817eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  else
1818eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getLocation());
1819987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  ConsumeToken();
1820651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DS.Finish(Diags, PP, Policy);
1821987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
18221cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
18232f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
18242f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers,
18252f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished
18262f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are
18272f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and
18282f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false
18292f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise.
18302f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
18312f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///   type-specifier-seq: [C++ 8.1]
18322f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///     type-specifier type-specifier-seq[opt]
18332f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
18342f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
183569730c115c2d0fec2f20609d905d920a5a41b29bRichard Smith  ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
1836651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy());
18372f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  return false;
18382f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
18392f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
18403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of
18413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form.
18423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
18433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or
18443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
18453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will
18463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to
18473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller.
18483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
18493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if
18503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id.
18513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
18523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual
18533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name.
18543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
18553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or
18563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor.
18573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
18583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the
18593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
18603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
186146df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
186246df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
186346df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
18643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id
18653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully,
18663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id.
18673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1868d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param AssumeTemplateId When true, this routine will assume that the name
1869d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// refers to a template without performing name lookup to verify.
1870d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
18713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise.
18723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1873e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          SourceLocation TemplateKWLoc,
18743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          IdentifierInfo *Name,
18753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          SourceLocation NameLoc,
18763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          bool EnteringContext,
1877b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          ParsedType ObjectType,
1878d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                          UnqualifiedId &Id,
1879e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          bool AssumeTemplateId) {
18800278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  assert((AssumeTemplateId || Tok.is(tok::less)) &&
18810278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor         "Expected '<' to finish parsing a template-id");
18823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
18833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateTy Template;
18843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateNameKind TNK = TNK_Non_template;
18853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (Id.getKind()) {
18863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
1887014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
1888e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
1889d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    if (AssumeTemplateId) {
1890e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
1891d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Id, ObjectType, EnteringContext,
1892d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Template);
1893d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
1894d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        return true;
18951fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    } else {
18961fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
18977c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS,
18987c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateKWLoc.isValid(), Id,
18997c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   ObjectType, EnteringContext, Template,
19001fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
19011fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor
19021fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
19031fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          ObjectType && IsTemplateArgumentList()) {
19041fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // We have something like t->getAs<T>(), where getAs is a
19051fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // member of an unknown specialization. However, this will only
19061fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
19071fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
19081fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        std::string Name;
19091fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        if (Id.getKind() == UnqualifiedId::IK_Identifier)
19101fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = Id.Identifier->getName();
19111fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        else {
19121fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = "operator ";
19131fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
19141fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
19151fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          else
19161fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += Id.Identifier->getName();
19171fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        }
19181fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
19191fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << Name
19201fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << FixItHint::CreateInsertion(Id.StartLocation, "template ");
1921e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1922e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 SS, TemplateKWLoc, Id,
1923e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 ObjectType, EnteringContext,
1924e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 Template);
1925d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TNK == TNK_Non_template)
19261fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          return true;
19271fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      }
19281fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    }
19293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
19303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1931014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_ConstructorName: {
1932014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
19331fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1934014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
19357c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara    TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
19367c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                 TemplateName, ObjectType,
19371fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 EnteringContext, Template,
19381fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 MemberOfUnknownSpecialization);
19393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1940014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
19413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1942014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_DestructorName: {
1943014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
19441fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1945014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
19462d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (ObjectType) {
1947e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1948e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               SS, TemplateKWLoc, TemplateName,
1949e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               ObjectType, EnteringContext,
1950e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               Template);
1951d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
19522d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
19532d1c21414199a7452f122598189363a3922605b1Douglas Gregor    } else {
19547c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
19557c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateName, ObjectType,
19561fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   EnteringContext, Template,
19571fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
19582d1c21414199a7452f122598189363a3922605b1Douglas Gregor
1959b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
1960124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        Diag(NameLoc, diag::err_destructor_template_id)
1961124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          << Name << SS.getRange();
19622d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
19632d1c21414199a7452f122598189363a3922605b1Douglas Gregor      }
19642d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
19653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1966014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
19673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  default:
19693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
19703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
19713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (TNK == TNK_Non_template)
19733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
19743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Parse the enclosed template argument list.
19763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
19773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgList TemplateArgs;
19780278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (Tok.is(tok::less) &&
19790278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
1980059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                       SS, true, LAngleLoc,
19813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgs,
19823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       RAngleLoc))
19833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
19843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
1986e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1987e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
19883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Form a parsed representation of the template-id to be stored in the
19893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // UnqualifiedId.
19903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateIdAnnotation *TemplateId
199113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
19923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1993651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // FIXME: Store name for literal operator too.
19943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
19953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->Name = Id.Identifier;
1996014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = OO_None;
19973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
19983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
19996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TemplateId->Name = nullptr;
2000014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = Id.OperatorFunctionId.Operator;
2001014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
20023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
20033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
2004059101f922de6eb765601459925f4c8914420b23Douglas Gregor    TemplateId->SS = SS;
20052b28bf1a8fa6e1c598805374f29e4fbf45e751feBenjamin Kramer    TemplateId->TemplateKWLoc = TemplateKWLoc;
20062b5289b6fd7e3d9899868410a498c081c9595662John McCall    TemplateId->Template = Template;
20073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Kind = TNK;
20083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->LAngleLoc = LAngleLoc;
20093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->RAngleLoc = RAngleLoc;
2010314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
20113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
2012314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor         Arg != ArgEnd; ++Arg)
20133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Args[Arg] = TemplateArgs[Arg];
20143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setTemplateId(TemplateId);
20163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
20173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
20183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Bundle the template arguments together.
20205354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
2021fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara
20223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Constructor and destructor names.
2023f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Type
202455d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
202555d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara                                  Template, NameLoc,
2026fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                  LAngleLoc, TemplateArgsPtr, RAngleLoc,
2027fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                  /*IsCtorOrDtorName=*/true);
20283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Type.isInvalid())
20293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
20303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
20323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
20333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  else
20343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
20353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return false;
20373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
20383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
2039ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part
2040ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id.
20413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
2042ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or
2043ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way.
20443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
2045ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
20463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       operator-function-id: [C++ 13.5]
20473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         'operator' operator
20483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
2049ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       operator: one of
20503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            new   delete  new[]   delete[]
20513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            +     -    *  /    %  ^    &   |   ~
20523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            !     =    <  >    += -=   *=  /=  %=
20533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
20543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
20553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ()    []
20563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
20573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-function-id: [C++ 12.3.2]
20583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator conversion-type-id
20593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
20603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-type-id:
20613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         type-specifier-seq conversion-declarator[opt]
20623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
20633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-declarator:
20643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ptr-operator conversion-declarator[opt]
20653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode
20663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
20671ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param SS The nested-name-specifier that preceded this unqualified-id. If
20683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
20693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
20703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the
20713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
20723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
2073ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
2074ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed.
2075ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2076ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
2077ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2078ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise.
2079ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
2080b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType ObjectType,
2081ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        UnqualifiedId &Result) {
2082ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
2083ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2084ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Consume the 'operator' keyword.
2085ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation KeywordLoc = ConsumeToken();
2086ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2087ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Determine what kind of operator name we have.
2088ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  unsigned SymbolIdx = 0;
2089ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation SymbolLocations[3];
2090ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  OverloadedOperatorKind Op = OO_None;
2091ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  switch (Tok.getKind()) {
2092ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_new:
2093ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_delete: {
2094ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      bool isNew = Tok.getKind() == tok::kw_new;
2095ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the 'new' or 'delete'.
2096ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();
20976ee326af4e77e6f05973486097884d7431f2108dRichard Smith      // Check for array new/delete.
20986ee326af4e77e6f05973486097884d7431f2108dRichard Smith      if (Tok.is(tok::l_square) &&
209980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
21004a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        // Consume the '[' and ']'.
21014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        BalancedDelimiterTracker T(*this, tok::l_square);
21024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeOpen();
21034a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeClose();
21044a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        if (T.getCloseLocation().isInvalid())
2105ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          return true;
2106ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
21074a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        SymbolLocations[SymbolIdx++] = T.getOpenLocation();
21084a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        SymbolLocations[SymbolIdx++] = T.getCloseLocation();
2109ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_Array_New : OO_Array_Delete;
2110ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
2111ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_New : OO_Delete;
2112ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
2113ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
2114ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
2115ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2116ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2117ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::Token:                                                     \
2118ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
2119ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_##Name;                                                    \
2120ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
2121ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2122ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def"
2123ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2124ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_paren: {
21254a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      // Consume the '(' and ')'.
21264a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      BalancedDelimiterTracker T(*this, tok::l_paren);
21274a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeOpen();
21284a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
21294a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      if (T.getCloseLocation().isInvalid())
2130ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
2131ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
21324a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getOpenLocation();
21334a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getCloseLocation();
2134ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Call;
2135ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
2136ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
2137ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2138ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_square: {
21394a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      // Consume the '[' and ']'.
21404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      BalancedDelimiterTracker T(*this, tok::l_square);
21414a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeOpen();
21424a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
21434a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      if (T.getCloseLocation().isInvalid())
2144ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
2145ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
21464a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getOpenLocation();
21474a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getCloseLocation();
2148ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Subscript;
2149ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
2150ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
2151ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2152ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::code_completion: {
2153ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Code completion for the operator name.
215423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOperatorName(getCurScope());
21557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
2156ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Don't try to parse any further.
2157ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return true;
2158ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
2159ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2160ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    default:
2161ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
2162ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
2163ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2164ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Op != OO_None) {
2165ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // We have parsed an operator-function-id.
2166ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2167ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return false;
2168ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
21690486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
21700486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  // Parse a literal-operator-id.
21710486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //
2172aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith  //   literal-operator-id: C++11 [over.literal]
2173aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith  //     operator string-literal identifier
2174aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith  //     operator user-defined-string-literal
21750486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
217680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
21777fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith    Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
217833762775706e81c17ca774102ceda36049ecc593Richard Smith
217933762775706e81c17ca774102ceda36049ecc593Richard Smith    SourceLocation DiagLoc;
218033762775706e81c17ca774102ceda36049ecc593Richard Smith    unsigned DiagId = 0;
218133762775706e81c17ca774102ceda36049ecc593Richard Smith
218233762775706e81c17ca774102ceda36049ecc593Richard Smith    // We're past translation phase 6, so perform string literal concatenation
218333762775706e81c17ca774102ceda36049ecc593Richard Smith    // before checking for "".
2184cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<Token, 4> Toks;
2185cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<SourceLocation, 4> TokLocs;
218633762775706e81c17ca774102ceda36049ecc593Richard Smith    while (isTokenStringLiteral()) {
218733762775706e81c17ca774102ceda36049ecc593Richard Smith      if (!Tok.is(tok::string_literal) && !DiagId) {
2188aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith        // C++11 [over.literal]p1:
2189aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith        //   The string-literal or user-defined-string-literal in a
2190aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith        //   literal-operator-id shall have no encoding-prefix [...].
219133762775706e81c17ca774102ceda36049ecc593Richard Smith        DiagLoc = Tok.getLocation();
219233762775706e81c17ca774102ceda36049ecc593Richard Smith        DiagId = diag::err_literal_operator_string_prefix;
219333762775706e81c17ca774102ceda36049ecc593Richard Smith      }
219433762775706e81c17ca774102ceda36049ecc593Richard Smith      Toks.push_back(Tok);
219533762775706e81c17ca774102ceda36049ecc593Richard Smith      TokLocs.push_back(ConsumeStringToken());
219699831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    }
21970486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
2198ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    StringLiteralParser Literal(Toks, PP);
219933762775706e81c17ca774102ceda36049ecc593Richard Smith    if (Literal.hadError)
220033762775706e81c17ca774102ceda36049ecc593Richard Smith      return true;
220133762775706e81c17ca774102ceda36049ecc593Richard Smith
220233762775706e81c17ca774102ceda36049ecc593Richard Smith    // Grab the literal operator's suffix, which will be either the next token
220333762775706e81c17ca774102ceda36049ecc593Richard Smith    // or a ud-suffix from the string literal.
22046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    IdentifierInfo *II = nullptr;
220533762775706e81c17ca774102ceda36049ecc593Richard Smith    SourceLocation SuffixLoc;
220633762775706e81c17ca774102ceda36049ecc593Richard Smith    if (!Literal.getUDSuffix().empty()) {
220733762775706e81c17ca774102ceda36049ecc593Richard Smith      II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
220833762775706e81c17ca774102ceda36049ecc593Richard Smith      SuffixLoc =
220933762775706e81c17ca774102ceda36049ecc593Richard Smith        Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
221033762775706e81c17ca774102ceda36049ecc593Richard Smith                                       Literal.getUDSuffixOffset(),
22114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                       PP.getSourceManager(), getLangOpts());
221233762775706e81c17ca774102ceda36049ecc593Richard Smith    } else if (Tok.is(tok::identifier)) {
221333762775706e81c17ca774102ceda36049ecc593Richard Smith      II = Tok.getIdentifierInfo();
221433762775706e81c17ca774102ceda36049ecc593Richard Smith      SuffixLoc = ConsumeToken();
221533762775706e81c17ca774102ceda36049ecc593Richard Smith      TokLocs.push_back(SuffixLoc);
221633762775706e81c17ca774102ceda36049ecc593Richard Smith    } else {
2217651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
22180486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      return true;
22190486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    }
22200486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
222133762775706e81c17ca774102ceda36049ecc593Richard Smith    // The string literal must be empty.
222233762775706e81c17ca774102ceda36049ecc593Richard Smith    if (!Literal.GetString().empty() || Literal.Pascal) {
2223aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      // C++11 [over.literal]p1:
2224aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      //   The string-literal or user-defined-string-literal in a
2225aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      //   literal-operator-id shall [...] contain no characters
2226aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      //   other than the implicit terminating '\0'.
222733762775706e81c17ca774102ceda36049ecc593Richard Smith      DiagLoc = TokLocs.front();
222833762775706e81c17ca774102ceda36049ecc593Richard Smith      DiagId = diag::err_literal_operator_string_not_empty;
222933762775706e81c17ca774102ceda36049ecc593Richard Smith    }
223033762775706e81c17ca774102ceda36049ecc593Richard Smith
223133762775706e81c17ca774102ceda36049ecc593Richard Smith    if (DiagId) {
223233762775706e81c17ca774102ceda36049ecc593Richard Smith      // This isn't a valid literal-operator-id, but we think we know
223333762775706e81c17ca774102ceda36049ecc593Richard Smith      // what the user meant. Tell them what they should have written.
2234cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko      SmallString<32> Str;
223533762775706e81c17ca774102ceda36049ecc593Richard Smith      Str += "\"\" ";
223633762775706e81c17ca774102ceda36049ecc593Richard Smith      Str += II->getName();
223733762775706e81c17ca774102ceda36049ecc593Richard Smith      Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
223833762775706e81c17ca774102ceda36049ecc593Richard Smith          SourceRange(TokLocs.front(), TokLocs.back()), Str);
223933762775706e81c17ca774102ceda36049ecc593Richard Smith    }
224033762775706e81c17ca774102ceda36049ecc593Richard Smith
224133762775706e81c17ca774102ceda36049ecc593Richard Smith    Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
2242651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2243651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return Actions.checkLiteralOperatorId(SS, Result);
22440486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  }
2245651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2246ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse a conversion-function-id.
2247ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
2248ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-function-id: [C++ 12.3.2]
2249ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     operator conversion-type-id
2250ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
2251ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-type-id:
2252ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     type-specifier-seq conversion-declarator[opt]
2253ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
2254ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-declarator:
2255ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     ptr-operator conversion-declarator[opt]
2256ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2257ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the type-specifier-seq.
22580b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
2259f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor  if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
2260ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
2261ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2262ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the conversion-declarator, which is merely a sequence of
2263ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // ptr-operators.
226414f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith  Declarator D(DS, Declarator::ConversionIdContext);
22656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ParseDeclaratorInternal(D, /*DirectDeclParser=*/nullptr);
22666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
2267ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Finish up the type.
2268f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
2269ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Ty.isInvalid())
2270ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
2271ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2272ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Note that this is a conversion-function-id.
2273ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2274ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                 D.getSourceRange().getEnd());
2275ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return false;
2276ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
2277ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2278ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2279ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity.
2280ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2281ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
2282ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       unqualified-id: [C++ expr.prim.general]
2283ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         identifier
2284ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         operator-function-id
2285ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         conversion-function-id
2286ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO]
2287ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         ~ class-name
2288ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         template-id
2289ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2290ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode
2291ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
22921ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param SS The nested-name-specifier that preceded this unqualified-id. If
2293ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2294ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2295ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the
2296ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier.
2297ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
22983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name.
22993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
23003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name.
23013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
230246df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
230346df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
230446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
23053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
23063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
23073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise.
23083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
23093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowDestructorName,
23103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowConstructorName,
2311b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                ParsedType ObjectType,
2312e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                SourceLocation& TemplateKWLoc,
23133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                UnqualifiedId &Result) {
23140278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
23150278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // Handle 'A::template B'. This is for template-ids which have not
23160278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // already been annotated by ParseOptionalCXXScopeSpecifier().
23170278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  bool TemplateSpecified = false;
23184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
23190278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      (ObjectType || SS.isSet())) {
23200278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateSpecified = true;
23210278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateKWLoc = ConsumeToken();
23220278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  }
23230278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
23243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
23253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   identifier
23263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (when it hasn't already been annotated)
23273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::identifier)) {
23283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the identifier.
23293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *Id = Tok.getIdentifierInfo();
23303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation IdLoc = ConsumeToken();
23313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus) {
2333b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // If we're not in C++, only identifiers matter. Record the
2334b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // identifier and return.
2335b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      Result.setIdentifier(Id, IdLoc);
2336b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      return false;
2337b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    }
2338b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor
23393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (AllowConstructorName &&
234023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
23413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed a constructor name.
2342fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara      ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2343fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          &SS, false, false,
2344fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          ParsedType(),
2345fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          /*IsCtorOrDtorName=*/true,
2346fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          /*NonTrivialTypeSourceInfo=*/true);
2347fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara      Result.setConstructorName(Ty, IdLoc, IdLoc);
23483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
23493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an identifier.
23503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setIdentifier(Id, IdLoc);
23513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
23523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // If the next token is a '<', we may have a template.
23540278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less))
2355e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2356e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2357e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
23583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
23603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
23613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
23633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (already parsed and annotated)
23643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::annot_template_id)) {
236525a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
23660efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
23670efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    // If the template-name names the current class, then this is a constructor
23680efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    if (AllowConstructorName && TemplateId->Name &&
236923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
23700efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      if (SS.isSet()) {
23710efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // C++ [class.qual]p2 specifies that a qualified template-name
23720efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // is taken as the constructor name where a constructor can be
23730efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // declared. Thus, the template arguments are extraneous, so
23740efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // complain about them and remove them entirely.
23750efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Diag(TemplateId->TemplateNameLoc,
23760efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor             diag::err_out_of_line_constructor_template_id)
23770efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor          << TemplateId->Name
2378849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateRemoval(
23790efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                    SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
2380fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara        ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2381fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            TemplateId->TemplateNameLoc,
2382fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            getCurScope(),
2383fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            &SS, false, false,
2384fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            ParsedType(),
2385fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*IsCtorOrDtorName=*/true,
2386fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*NontrivialTypeSourceInfo=*/true);
2387fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara        Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
23880efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->RAngleLoc);
23890efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        ConsumeToken();
23900efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        return false;
23910efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      }
23920efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
23930efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      Result.setConstructorTemplateId(TemplateId);
23940efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      ConsumeToken();
23950efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      return false;
23960efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    }
23970efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
23983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // We have already parsed a template-id; consume the annotation token as
23993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // our unqualified-id.
24000efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    Result.setTemplateId(TemplateId);
2401e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    TemplateKWLoc = TemplateId->TemplateKWLoc;
24023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ConsumeToken();
24033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
24043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
24053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
24063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
24073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   operator-function-id
24083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   conversion-function-id
24093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::kw_operator)) {
2410ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
24113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
24123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
2413e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // If we have an operator-function-id or a literal-operator-id and the next
2414e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // token is a '<', we may have a
2415ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //
2416ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //   template-id:
2417ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //     operator-function-id < template-argument-list[opt] >
2418e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2419e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt         Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
24200278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor        (TemplateSpecified || Tok.is(tok::less)))
2421e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
24226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                          nullptr, SourceLocation(),
2423e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2424e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
24256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
24263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
24273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
24283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
24294e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus &&
2430b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
24313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // C++ [expr.unary.op]p10:
24323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   There is an ambiguity in the unary-expression ~X(), where X is a
24333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   class-name. The ambiguity is resolved in favor of treating ~ as a
24343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //    unary complement rather than treating ~X as referring to a destructor.
24353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
24363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the '~'.
24373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation TildeLoc = ConsumeToken();
243853a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie
243953a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
244053a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      DeclSpec DS(AttrFactory);
244153a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
244253a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
244353a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie        Result.setDestructorName(TildeLoc, Type, EndLoc);
244453a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie        return false;
244553a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      }
244653a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      return true;
244753a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    }
24483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
24493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name.
24503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.isNot(tok::identifier)) {
2451124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      Diag(Tok, diag::err_destructor_tilde_identifier);
24523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
24533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
24543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
24553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name (or template-name in a simple-template-id).
24563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
24573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation ClassNameLoc = ConsumeToken();
24583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
24590278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less)) {
2460b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
2461e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2462e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          ClassName, ClassNameLoc,
2463e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2464e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
24652d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
24662d1c21414199a7452f122598189363a3922605b1Douglas Gregor
24673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a destructor name.
2468b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2469b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              ClassNameLoc, getCurScope(),
2470b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              SS, ObjectType,
2471b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              EnteringContext);
2472124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (!Ty)
24733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
2474124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
24753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
24763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
24773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
24783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
24792d1c21414199a7452f122598189363a3922605b1Douglas Gregor  Diag(Tok, diag::err_expected_unqualified_id)
24804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    << getLangOpts().CPlusPlus;
24813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return true;
24823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
24833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
24844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
24854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors.
24861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
248759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has
248859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
248959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location.  Otherwise, "Start" is the location of the 'new' token.
24904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
24914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
24924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] new-type-id
24934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
24944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
24954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
24964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
24974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
24984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
24994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
2500cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-type-id:
2501cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   type-specifier-seq new-declarator[opt]
2502893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor/// [GNU]             attributes type-specifier-seq new-declarator[opt]
2503cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
2504cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-declarator:
2505cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   ptr-operator new-declarator[opt]
2506cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   direct-new-declarator
2507cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
25084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-initializer:
25094c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list[opt] ')'
2510dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x]           braced-init-list
25114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
251260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
251359232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
251459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_new) && "expected 'new' token");
251559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken();   // Consume 'new'
25164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
25184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // second form of new-expression. It can't be a new-type-id.
25194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25204e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer  ExprVector PlacementArgs;
25214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation PlacementLParen, PlacementRParen;
25224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25234bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor  SourceRange TypeIdParens;
25240b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
25250b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
25264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
25274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // If it turns out to be a placement, we change the type location.
25284a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
25294a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
25304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    PlacementLParen = T.getOpenLocation();
2531cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
25328fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
253320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2534cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
25354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25364a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
25374a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    PlacementRParen = T.getCloseLocation();
2538cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementRParen.isInvalid()) {
25398fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
254020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2541cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
25424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2543cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementArgs.empty()) {
25444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Reset the placement locations. There was no placement.
25454a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      TypeIdParens = T.getRange();
25464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      PlacementLParen = PlacementRParen = SourceLocation();
25474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    } else {
25484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // We still need the type.
25494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      if (Tok.is(tok::l_paren)) {
25504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        BalancedDelimiterTracker T(*this, tok::l_paren);
25514a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeOpen();
2552893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor        MaybeParseGNUAttributes(DeclaratorInfo);
2553cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseSpecifierQualifierList(DS);
2554ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2555cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseDeclarator(DeclaratorInfo);
25564a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeClose();
25574a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        TypeIdParens = T.getRange();
25584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      } else {
2559893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor        MaybeParseGNUAttributes(DeclaratorInfo);
2560cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        if (ParseCXXTypeSpecifierSeq(DS))
2561cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          DeclaratorInfo.setInvalidType(true);
2562ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        else {
2563ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2564cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          ParseDeclaratorInternal(DeclaratorInfo,
2565cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                                  &Parser::ParseDirectNewDeclarator);
2566ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        }
25674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      }
25684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
25694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  } else {
2570cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // A new-type-id is a simplified type-id, where essentially the
2571cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // direct-declarator is replaced by a direct-new-declarator.
2572893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor    MaybeParseGNUAttributes(DeclaratorInfo);
2573cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseCXXTypeSpecifierSeq(DS))
2574cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      DeclaratorInfo.setInvalidType(true);
2575ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    else {
2576ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2577cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      ParseDeclaratorInternal(DeclaratorInfo,
2578cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                              &Parser::ParseDirectNewDeclarator);
2579ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    }
25804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
2581eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (DeclaratorInfo.isInvalidType()) {
25828fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
258320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
2584cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
25854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25862aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  ExprResult Initializer;
25874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
25892aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    SourceLocation ConstructorLParen, ConstructorRParen;
25904e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer    ExprVector ConstructorArgs;
25914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
25924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
25934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    ConstructorLParen = T.getOpenLocation();
25944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (Tok.isNot(tok::r_paren)) {
25954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      CommaLocsTy CommaLocs;
2596cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
25978fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
259820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl        return ExprError();
2599cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      }
26004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
26014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
26024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    ConstructorRParen = T.getCloseLocation();
2603cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ConstructorRParen.isInvalid()) {
26048fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
260520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2606cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
26072aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
26082aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                                             ConstructorRParen,
26093fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                             ConstructorArgs);
261080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
26117fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith    Diag(Tok.getLocation(),
26127fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith         diag::warn_cxx98_compat_generalized_initializer_lists);
26132aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    Initializer = ParseBraceInitializer();
26144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
26152aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  if (Initializer.isInvalid())
26162aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    return Initializer;
26174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2618f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
26193fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                             PlacementArgs, PlacementRParen,
2620ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                             TypeIdParens, DeclaratorInfo, Initializer.get());
26214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
26224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
26234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
26244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal.
26254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
26264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        direct-new-declarator:
26274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '[' expression ']'
26284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   direct-new-declarator '[' constant-expression ']'
26294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
263059232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) {
26314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Parse the array dimensions.
26324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool first = true;
26334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  while (Tok.is(tok::l_square)) {
26346ee326af4e77e6f05973486097884d7431f2108dRichard Smith    // An array-size expression can't start with a lambda.
26356ee326af4e77e6f05973486097884d7431f2108dRichard Smith    if (CheckProhibitedCXX11Attribute())
26366ee326af4e77e6f05973486097884d7431f2108dRichard Smith      continue;
26376ee326af4e77e6f05973486097884d7431f2108dRichard Smith
26384a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_square);
26394a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
26404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
264160d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Size(first ? ParseExpression()
26422f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl                                : ParseConstantExpression());
26430e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Size.isInvalid()) {
26444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Recover
26458fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::r_square, StopAtSemi);
26464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
26474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
26484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    first = false;
26494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
26504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
26510b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
2652ad017fa7a4df7389d245d02a49b3c79ed70bedb9Bill Wendling    // Attributes here appertain to the array type. C++11 [expr.new]p5.
26536ee326af4e77e6f05973486097884d7431f2108dRichard Smith    ParsedAttributes Attrs(AttrFactory);
26544e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    MaybeParseCXX11Attributes(Attrs);
26556ee326af4e77e6f05973486097884d7431f2108dRichard Smith
26560b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    D.AddTypeInfo(DeclaratorChunk::getArray(0,
26577f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                            /*static=*/false, /*star=*/false,
2658ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                            Size.get(),
26594a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            T.getOpenLocation(),
26604a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            T.getCloseLocation()),
26616ee326af4e77e6f05973486097884d7431f2108dRichard Smith                  Attrs, T.getCloseLocation());
26624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
26634a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
26644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
26654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
26664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
26674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
26684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
26694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator.
26704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
26714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
26724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
26734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
26744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
26754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
26764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
26774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
2678ca0408fb49c1370430672acf2d770b7151cf71deJohn McCallbool Parser::ParseExpressionListOrTypeId(
26795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   SmallVectorImpl<Expr*> &PlacementArgs,
268059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner                                         Declarator &D) {
26814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // The '(' was already consumed.
26824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (isTypeIdInParens()) {
2683cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseSpecifierQualifierList(D.getMutableDeclSpec());
2684ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetSourceRange(D.getDeclSpec().getSourceRange());
2685cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseDeclarator(D);
2686eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    return D.isInvalidType();
26874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
26884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
26894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // It's not a type, it has to be an expression list.
26904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Discard the comma locations - ActOnCXXNew has enough parameters.
26914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  CommaLocsTy CommaLocs;
26924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return ParseExpressionList(PlacementArgs, CommaLocs);
26934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
26944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
26954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
26964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new.
26974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
269859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional
269959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
270059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location.  Otherwise, "Start" is the location of the
270159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token.
270259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner///
27034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        delete-expression:
27044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' cast-expression
27054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' '[' ']' cast-expression
270660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
270759232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
270859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
270959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken(); // Consume 'delete'
27104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
27114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Array delete?
27124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ArrayDelete = false;
27136ee326af4e77e6f05973486097884d7431f2108dRichard Smith  if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
2714950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    // C++11 [expr.delete]p1:
2715950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //   Whenever the delete keyword is followed by empty square brackets, it
2716950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //   shall be interpreted as [array delete].
2717950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //   [Footnote: A lambda expression with a lambda-introducer that consists
2718950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //              of empty square brackets can follow the delete keyword if
2719950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //              the lambda expression is enclosed in parentheses.]
2720950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2721950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //        lambda-introducer.
27224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ArrayDelete = true;
27234a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_square);
27244a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
27254a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
27264a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
27274a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
272820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
27294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
27304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
273160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Operand(ParseCastExpression(false));
27320e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Operand.isInvalid())
27333fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    return Operand;
27344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2735ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.get());
27364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
273764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
27384ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregorstatic TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
27394ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  switch (kind) {
27404ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  default: llvm_unreachable("Not a known type trait");
2741651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#define TYPE_TRAIT_1(Spelling, Name, Key) \
2742651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinescase tok::kw_ ## Spelling: return UTT_ ## Name;
2743651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#define TYPE_TRAIT_2(Spelling, Name, Key) \
2744651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinescase tok::kw_ ## Spelling: return BTT_ ## Name;
2745651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/Basic/TokenKinds.def"
2746651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#define TYPE_TRAIT_N(Spelling, Name, Key) \
2747651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  case tok::kw_ ## Spelling: return TT_ ## Name;
2748651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/Basic/TokenKinds.def"
27494ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  }
27504ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor}
27514ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
275221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegleystatic ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
275321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch(kind) {
275421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  default: llvm_unreachable("Not a known binary type trait");
275521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case tok::kw___array_rank:                 return ATT_ArrayRank;
275621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case tok::kw___array_extent:               return ATT_ArrayExtent;
275721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
275821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
275921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
2760552622067dc45013d240f73952fece703f5e63bdJohn Wiegleystatic ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2761552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  switch(kind) {
2762b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a known unary expression trait.");
2763552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case tok::kw___is_lvalue_expr:             return ET_IsLValueExpr;
2764552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case tok::kw___is_rvalue_expr:             return ET_IsRValueExpr;
2765552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  }
2766552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
2767552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2768651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstatic unsigned TypeTraitArity(tok::TokenKind kind) {
2769651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  switch (kind) {
2770651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    default: llvm_unreachable("Not a known type trait");
2771651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#define TYPE_TRAIT(N,Spelling,K) case tok::kw_##Spelling: return N;
2772651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/Basic/TokenKinds.def"
27736ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
27746ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
27756ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
27764ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor/// \brief Parse the built-in type-trait pseudo-functions that allow
27774ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor/// implementation of the TR1/C++11 type traits templates.
27784ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///
27794ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///       primary-expression:
2780651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///          unary-type-trait '(' type-id ')'
2781651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///          binary-type-trait '(' type-id ',' type-id ')'
27824ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///          type-trait '(' type-id-seq ')'
27834ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///
27844ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///       type-id-seq:
27854ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///          type-id ...[opt] type-id-seq[opt]
27864ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///
27874ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas GregorExprResult Parser::ParseTypeTrait() {
2788651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  tok::TokenKind Kind = Tok.getKind();
2789651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  unsigned Arity = TypeTraitArity(Kind);
2790651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
27914ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  SourceLocation Loc = ConsumeToken();
27924ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
27934ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  BalancedDelimiterTracker Parens(*this, tok::l_paren);
2794651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Parens.expectAndConsume())
27954ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return ExprError();
27964ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
2797cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<ParsedType, 2> Args;
27984ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  do {
27994ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // Parse the next type.
28004ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    TypeResult Ty = ParseTypeName();
28014ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Ty.isInvalid()) {
28024ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      Parens.skipToEnd();
28034ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      return ExprError();
28044ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
28054ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
28064ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // Parse the ellipsis, if present.
28074ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Tok.is(tok::ellipsis)) {
28084ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
28094ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      if (Ty.isInvalid()) {
28104ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        Parens.skipToEnd();
28114ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        return ExprError();
28124ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      }
28134ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
28144ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
28154ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // Add this type to the list of arguments.
28164ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    Args.push_back(Ty.get());
2817651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  } while (TryConsumeToken(tok::comma));
2818651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
28194ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  if (Parens.consumeClose())
28204ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return ExprError();
2821651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2822651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SourceLocation EndLoc = Parens.getCloseLocation();
2823651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2824651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Arity && Args.size() != Arity) {
2825651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Diag(EndLoc, diag::err_type_trait_arity)
2826651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      << Arity << 0 << (Arity > 1) << (int)Args.size() << SourceRange(Loc);
2827651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return ExprError();
2828651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
2829651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2830651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!Arity && Args.empty()) {
2831651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Diag(EndLoc, diag::err_type_trait_arity)
2832651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      << 1 << 1 << 1 << (int)Args.size() << SourceRange(Loc);
2833651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return ExprError();
2834651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
2835651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2836651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return Actions.ActOnTypeTrait(TypeTraitFromTokKind(Kind), Loc, Args, EndLoc);
28374ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor}
28384ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
283921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// ParseArrayTypeTrait - Parse the built-in array type-trait
284021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// pseudo-functions.
284121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///
284221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///       primary-expression:
284321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// [Embarcadero]     '__array_rank' '(' type-id ')'
284421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// [Embarcadero]     '__array_extent' '(' type-id ',' expression ')'
284521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///
284621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John WiegleyExprResult Parser::ParseArrayTypeTrait() {
284721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
284821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  SourceLocation Loc = ConsumeToken();
284921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
28504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
2851651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (T.expectAndConsume())
285221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return ExprError();
285321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
285421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  TypeResult Ty = ParseTypeName();
285521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  if (Ty.isInvalid()) {
28568fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::comma, StopAtSemi);
28578fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_paren, StopAtSemi);
285821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return ExprError();
285921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
286021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
286121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch (ATT) {
286221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayRank: {
28634a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
28646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), nullptr,
28654a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
286621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
286721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayExtent: {
2868651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (ExpectAndConsume(tok::comma)) {
28698fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::r_paren, StopAtSemi);
287021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      return ExprError();
287121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    }
287221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
287321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    ExprResult DimExpr = ParseExpression();
28744a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
287521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
28764a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
28774a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
287821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
287921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
28803026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid ArrayTypeTrait!");
288121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
288221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
2883552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// ParseExpressionTrait - Parse built-in expression-trait
2884552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// pseudo-functions like __is_lvalue_expr( xxx ).
2885552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///
2886552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///       primary-expression:
2887552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// [Embarcadero]     expression-trait '(' expression ')'
2888552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///
2889552622067dc45013d240f73952fece703f5e63bdJohn WiegleyExprResult Parser::ParseExpressionTrait() {
2890552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2891552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  SourceLocation Loc = ConsumeToken();
2892552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
28934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
2894651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (T.expectAndConsume())
2895552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    return ExprError();
2896552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2897552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  ExprResult Expr = ParseExpression();
2898552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
28994a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
2900552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
29014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
29024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      T.getCloseLocation());
2903552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
2904552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2905552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2906f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2907f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2908f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens.
290960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2910f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
2911b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                         ParsedType &CastTy,
29126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                         BalancedDelimiterTracker &Tracker,
29136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                         ColonProtectionRAIIObject &ColonProt) {
29144e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
2915f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2916f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(isTypeIdInParens() && "Not a type-id!");
2917f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
291860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(true);
2919b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  CastTy = ParsedType();
2920f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2921f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // We need to disambiguate a very ugly part of the C++ syntax:
2922f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2923f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())x;  - type-id
2924f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())*x; - type-id
2925f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())/x; - expression
2926f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T());   - expression
2927f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2928f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The bad news is that we cannot use the specialized tentative parser, since
2929f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // it can only verify that the thing inside the parens can be parsed as
2930f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // type-id, it is not useful for determining the context past the parens.
2931f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2932f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The good news is that the parser can disambiguate this part without
2933a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis  // making any unnecessary Action calls.
2934f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  //
2935f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // It uses a scheme similar to parsing inline methods. The parenthesized
2936f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // tokens are cached, the context that follows is determined (possibly by
2937f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parsing a cast-expression), and then we re-introduce the cached tokens
2938f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // into the token stream and parse them appropriately.
2939f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
29401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParenParseOption ParseAs;
2941f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  CachedTokens Toks;
2942f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2943f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Store the tokens of the parentheses. We will parse them after we determine
2944f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // the context that follows them.
294514b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis  if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
2946f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We didn't find the ')' we expected.
29474a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Tracker.consumeClose();
2948f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
2949f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
2950f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2951f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::l_brace)) {
2952f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = CompoundLiteral;
2953f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  } else {
2954f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    bool NotCastExpr;
2955b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2956b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2957b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      NotCastExpr = true;
2958b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    } else {
2959b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // Try parsing the cast-expression that may follow.
2960b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // If it is not a cast-expression, NotCastExpr will be true and no token
2961b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // will be consumed.
29626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ColonProt.restore();
2963b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      Result = ParseCastExpression(false/*isUnaryExpression*/,
2964b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman                                   false/*isAddressofOperand*/,
2965b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   NotCastExpr,
29660a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis                                   // type-id has priority.
2967cd78e612d6fa8e214e6a6bb0e739a0c3e419df91Kaelyn Uhrain                                   IsTypeCast);
2968b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    }
2969f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2970f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // If we parsed a cast-expression, it's really a type-id, otherwise it's
2971f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // an expression.
2972f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
2973f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
2974f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
29751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The current token should go after the cached tokens.
2976f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Toks.push_back(Tok);
2977f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Re-enter the stored parenthesized tokens into the token stream, so we may
2978f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parse them now.
2979f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  PP.EnterTokenStream(Toks.data(), Toks.size(),
2980f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2981f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Drop the current token and bring the first cached one. It's the same token
2982f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // as when we entered this function.
2983f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  ConsumeAnyToken();
2984f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2985f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (ParseAs >= CompoundLiteral) {
29860a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    // Parse the type declarator.
29870a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    DeclSpec DS(AttrFactory);
29880a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
29896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    {
29906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ColonProtectionRAIIObject InnerColonProtection(*this);
29916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ParseSpecifierQualifierList(DS);
29926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ParseDeclarator(DeclaratorInfo);
29936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
2994f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2995f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Match the ')'.
29964a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Tracker.consumeClose();
29976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ColonProt.restore();
2998f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2999f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (ParseAs == CompoundLiteral) {
3000f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      ExprType = CompoundLiteral;
30016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (DeclaratorInfo.isInvalidType())
30026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return ExprError();
30036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
30046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
30056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return ParseCompoundLiteralExpression(Ty.get(),
30064a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Tracker.getOpenLocation(),
30074a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Tracker.getCloseLocation());
3008f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    }
30091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3010f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
3011f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    assert(ParseAs == CastExpr);
3012f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
30130a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    if (DeclaratorInfo.isInvalidType())
3014f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ExprError();
3015f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
3016f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Result is what ParseCastExpression returned earlier.
3017f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    if (!Result.isInvalid())
30184a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
30194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    DeclaratorInfo, CastTy,
3020ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                    Tracker.getCloseLocation(), Result.get());
30213fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    return Result;
3022f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
30231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3024f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Not a compound literal, and not followed by a cast-expression.
3025f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  assert(ParseAs == SimpleExpr);
3026f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
3027f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  ExprType = SimpleExpr;
3028f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Result = ParseExpression();
3029f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (!Result.isInvalid() && Tok.is(tok::r_paren))
30304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
3031ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                    Tok.getLocation(), Result.get());
3032f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
3033f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // Match the ')'.
3034f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Result.isInvalid()) {
30358fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_paren, StopAtSemi);
3036f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
3037f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
30381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30394a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Tracker.consumeClose();
30403fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
3041f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis}
3042