ParseExprCXX.cpp revision 8fe2475a4b4c00475709c13d43eb9a57cce87cbc
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//===----------------------------------------------------------------------===//
13fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali#include "clang/AST/DeclTemplate.h"
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h"
15bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor#include "RAIIObjectsForParser.h"
16dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman#include "clang/Basic/PrettyStackTrace.h"
1733762775706e81c17ca774102ceda36049ecc593Richard Smith#include "clang/Lex/LiteralSupport.h"
1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Parse/ParseDiagnostic.h"
1919510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
2019510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h"
2155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Scope.h"
223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h"
233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
24fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
27ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic int SelectDigraphErrorMessage(tok::TokenKind Kind) {
28ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  switch (Kind) {
29ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_template:         return 0;
30ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_const_cast:       return 1;
31ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_dynamic_cast:     return 2;
32ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_reinterpret_cast: return 3;
33ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_static_cast:      return 4;
34ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    default:
35b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unknown type for digraph error message.");
36ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  }
37ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
38ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
39ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith// Are the two tokens adjacent in the same source file?
4019a2702042b7e3ee838cca458b35f607111a3897Richard Smithbool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
41ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceManager &SM = PP.getSourceManager();
42ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
43a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
44ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  return FirstEnd == SM.getSpellingLoc(Second.getLocation());
45ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
46ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
47ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith// Suggest fixit for "<::" after a cast.
48ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
49ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                       Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
50ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Pull '<:' and ':' off token stream.
51ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (!AtDigraph)
52ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    PP.Lex(DigraphToken);
53ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  PP.Lex(ColonToken);
54ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
55ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceRange Range;
56ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Range.setBegin(DigraphToken.getLocation());
57ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Range.setEnd(ColonToken.getLocation());
58ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
59ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      << SelectDigraphErrorMessage(Kind)
60ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      << FixItHint::CreateReplacement(Range, "< ::");
61ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
62ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Update token information to reflect their change in token type.
63ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setKind(tok::coloncolon);
64a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
65ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setLength(2);
66ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  DigraphToken.setKind(tok::less);
67ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  DigraphToken.setLength(1);
68ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
69ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Push new tokens back to token stream.
70ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  PP.EnterToken(ColonToken);
71ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (!AtDigraph)
72ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    PP.EnterToken(DigraphToken);
73ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
74ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
75950be71c745409e373ae8a834490f9026c8ac222Richard Trieu// Check for '<::' which should be '< ::' instead of '[:' when following
76950be71c745409e373ae8a834490f9026c8ac222Richard Trieu// a template name.
77950be71c745409e373ae8a834490f9026c8ac222Richard Trieuvoid Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
78950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                                        bool EnteringContext,
79950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                                        IdentifierInfo &II, CXXScopeSpec &SS) {
80c11030ea936f6952deb5a1423ce1648173cd417eRichard Trieu  if (!Next.is(tok::l_square) || Next.getLength() != 2)
81950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
82950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
83950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  Token SecondToken = GetLookAheadToken(2);
8419a2702042b7e3ee838cca458b35f607111a3897Richard Smith  if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
85950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
86950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
87950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  TemplateTy Template;
88950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  UnqualifiedId TemplateName;
89950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  TemplateName.setIdentifier(&II, Tok.getLocation());
90950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  bool MemberOfUnknownSpecialization;
91950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
92950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                              TemplateName, ObjectType, EnteringContext,
93950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                              Template, MemberOfUnknownSpecialization))
94950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
95950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
96950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
97950be71c745409e373ae8a834490f9026c8ac222Richard Trieu             /*AtDigraph*/false);
98950be71c745409e373ae8a834490f9026c8ac222Richard Trieu}
99950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
100919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu/// \brief Emits an error for a left parentheses after a double colon.
101919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu///
102919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu/// When a '(' is found after a '::', emit an error.  Attempt to fix the token
103bba91b881c946cbcd200c87dc0a83553506e2458Nico Weber/// stream by removing the '(', and the matching ')' if found.
104919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieuvoid Parser::CheckForLParenAfterColonColon() {
105919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  if (!Tok.is(tok::l_paren))
106919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    return;
107919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
108919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
109919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  Token Tok1 = getCurToken();
110919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
111919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    return;
112919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
113919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  if (Tok1.is(tok::identifier)) {
114919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    Token Tok2 = GetLookAheadToken(1);
115919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    if (Tok2.is(tok::r_paren)) {
116919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      ConsumeToken();
117919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      PP.EnterToken(Tok1);
118919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      r_parenLoc = ConsumeParen();
119919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    }
120919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  } else if (Tok1.is(tok::star)) {
121919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    Token Tok2 = GetLookAheadToken(1);
122919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    if (Tok2.is(tok::identifier)) {
123919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      Token Tok3 = GetLookAheadToken(2);
124919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      if (Tok3.is(tok::r_paren)) {
125919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        ConsumeToken();
126919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        ConsumeToken();
127919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        PP.EnterToken(Tok2);
128919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        PP.EnterToken(Tok1);
129919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu        r_parenLoc = ConsumeParen();
130919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      }
131919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    }
132919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  }
133919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
134919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu  Diag(l_parenLoc, diag::err_paren_after_colon_colon)
135919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      << FixItHint::CreateRemoval(l_parenLoc)
136919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      << FixItHint::CreateRemoval(r_parenLoc);
137919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu}
138919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
1391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present.
1402dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1412dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or
1432dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream.
144eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
145eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'[opt] nested-name-specifier
146eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'
147eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
148eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       nested-name-specifier:
149eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         type-name '::'
150eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         namespace-name '::'
151eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         nested-name-specifier identifier '::'
1522dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///         nested-name-specifier 'template'[opt] simple-template-id '::'
1532dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1542dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed
1562dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty)
1572dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following
1592dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the
1602dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed.
161eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
1622dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of
1632dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it.
1642dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
165d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param MayBePseudoDestructor When non-NULL, points to a flag that
166d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// indicates whether this nested-name-specifier may be part of a
167d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// pseudo-destructor name. In this case, the flag will be set false
168d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we don't actually end up parsing a destructor name. Moreorover,
169d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we do end up determining that we are parsing a destructor name,
170d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// the last component of the nested-name-specifier is not parsed as
171d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// part of the scope specifier.
1722db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith///
1732db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// \param IsTypename If \c true, this nested-name-specifier is known to be
1742db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// part of a type name. This is used to improve error recovery.
1752db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith///
1762db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
1772db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// filled in with the leading identifier in the last component of the
1782db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith/// nested-name-specifier, if any.
179b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor///
1809ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// \returns true if there was an error parsing a scope specifier
181495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
182b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType ObjectType,
183b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            bool EnteringContext,
1844147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                            bool *MayBePseudoDestructor,
1852db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith                                            bool IsTypename,
1862db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith                                            IdentifierInfo **LastII) {
1874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus &&
1887452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Call sites of this function should be guarded by checking for C++");
1891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
190eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::annot_cxxscope)) {
1912db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith    assert(!LastII && "want last identifier but have already annotated scope");
192c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
193c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 Tok.getAnnotationRange(),
194c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 SS);
195eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    ConsumeToken();
1969ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
197eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
198e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner
1999c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo  if (Tok.is(tok::annot_template_id)) {
2009c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo    // If the current token is an annotated template id, it may already have
2019c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo    // a scope specifier. Restore it.
2029c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
2039c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo    SS = TemplateId->SS;
2049c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo  }
2059c90f7f1b8f52a49254c2337df6f0e8034e19551Larisse Voufo
2062db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith  if (LastII)
2072db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith    *LastII = 0;
2082db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith
20939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  bool HasScopeSpecifier = false;
21039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
2115b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner  if (Tok.is(tok::coloncolon)) {
2125b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    // ::new and ::delete aren't nested-name-specifiers.
2135b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    tok::TokenKind NextKind = NextToken().getKind();
2145b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
2155b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner      return false;
2161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    // '::' - Global scope qualifier.
2182e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
2192e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
220919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
221919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu    CheckForLParenAfterColonColon();
222919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
22339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    HasScopeSpecifier = true;
224eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
225eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
226d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  bool CheckForDestructor = false;
227d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (MayBePseudoDestructor && *MayBePseudoDestructor) {
228d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CheckForDestructor = true;
229d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = false;
230d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
231d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
23242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
23342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    DeclSpec DS(AttrFactory);
23442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation DeclLoc = Tok.getLocation();
23542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation EndLoc  = ParseDecltypeSpecifier(DS);
23642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    if (Tok.isNot(tok::coloncolon)) {
23742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
23842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      return false;
23942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    }
24042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
24142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation CCLoc = ConsumeToken();
24242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
24342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
24442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
24542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    HasScopeSpecifier = true;
24642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  }
24742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
24839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  while (true) {
2492dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (HasScopeSpecifier) {
2502dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p5:
2512dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the qualified-id has the form
2523b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
2532dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //       ::class-name-or-namespace-name::...
2543b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
2552dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the class-name-or-namespace-name is looked up in global scope as a
2562dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name.
2572dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
2582dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // To implement this, we clear out the object type as soon as we've
2592dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // seen a leading '::' or part of a nested-name-specifier.
260b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      ObjectType = ParsedType();
26181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
26281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      if (Tok.is(tok::code_completion)) {
26381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // Code completion for a nested-name-specifier, where the code
26481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // code completion token follows the '::'.
26523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
266b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // Include code completion token into the range of the scope otherwise
267b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // when we try to annotate the scope tokens the dangling code completion
268b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // token will cause assertion in
269b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // Preprocessor::AnnotatePreviousCachedTokens.
2707d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        SS.setEndLoc(Tok.getLocation());
2717d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
2727d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return true;
27381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      }
2742dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
2751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier:
27777cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    //   nested-name-specifier 'template'[opt] simple-template-id '::'
27877cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner
27977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // Parse the optional 'template' keyword, then make sure we have
28077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // 'identifier <' after it.
28177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    if (Tok.is(tok::kw_template)) {
2822dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // If we don't have a scope specifier or an object type, this isn't a
283eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // nested-name-specifier, since they aren't allowed to start with
284eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // 'template'.
2852dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      if (!HasScopeSpecifier && !ObjectType)
286eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
287eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman
2887bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TentativeParsingAction TPA(*this);
28977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      SourceLocation TemplateKWLoc = ConsumeToken();
290ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
291ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      UnqualifiedId TemplateName;
292ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::identifier)) {
293ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
2947bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
295ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
296ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else if (Tok.is(tok::kw_operator)) {
297ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
2987bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor                                       TemplateName)) {
2997bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
300ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
3017bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        }
302ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
303e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt        if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
304e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt            TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
305ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          Diag(TemplateName.getSourceRange().getBegin(),
306ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor               diag::err_id_after_template_in_nested_name_spec)
307ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor            << TemplateName.getSourceRange();
3087bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
309ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
310ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        }
311ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
3127bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
31377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        break;
31477cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      }
3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3167bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // If the next token is not '<', we have a qualified-id that refers
3177bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // to a template name, such as T::template apply, but is not a
3187bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // template-id.
3197bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      if (Tok.isNot(tok::less)) {
3207bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
3217bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        break;
3227bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      }
3237bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor
3247bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // Commit to parsing the template-id.
3257bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TPA.Commit();
326d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      TemplateTy Template;
327e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      if (TemplateNameKind TNK
328e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara          = Actions.ActOnDependentTemplateName(getCurScope(),
329e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               SS, TemplateKWLoc, TemplateName,
330e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               ObjectType, EnteringContext,
331e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               Template)) {
332e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
333e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName, false))
334d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          return true;
335d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      } else
3369ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
3371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33877cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      continue;
33977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    }
3401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
3421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We have
34339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
34439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //   simple-template-id '::'
34539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
34639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // So we need to check whether the simple-template-id is of the
347c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // right kind (it should name a type or be dependent), and then
348c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // convert it into a type within the nested-name-specifier.
34925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
350d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
351d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
3529ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
353d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
354d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
3552db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith      if (LastII)
3562db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith        *LastII = TemplateId->Name;
3572db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith
3586cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      // Consume the template-id token.
3596cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      ConsumeToken();
3606cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
3616cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
3626cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      SourceLocation CCLoc = ConsumeToken();
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3646796fc1adcaf57c38d072a238b016b2834afbe0dDavid Blaikie      HasScopeSpecifier = true;
3656cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
3665354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer      ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
3676cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                         TemplateId->NumArgs);
3686cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
3696cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
370e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                              SS,
371e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                              TemplateId->TemplateKWLoc,
3726cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->Template,
3736cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->TemplateNameLoc,
3746cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->LAngleLoc,
3756cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateArgsPtr,
3766cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->RAngleLoc,
3776cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              CCLoc,
3786cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              EnteringContext)) {
3796cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        SourceLocation StartLoc
3806cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor          = SS.getBeginLoc().isValid()? SS.getBeginLoc()
3816cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                      : TemplateId->TemplateNameLoc;
3826cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        SS.SetInvalid(SourceRange(StartLoc, CCLoc));
38367b9e831943300ce54e564e601971828ce4def15Chris Lattner      }
384eccce7e246a17e12a2afd6eabb9ac7c8d582db4eArgyrios Kyrtzidis
3856cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      continue;
38639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
38739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
3885c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
3895c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // The rest of the nested-name-specifier possibilities start with
3905c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // tok::identifier.
3915c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Tok.isNot(tok::identifier))
3925c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      break;
3935c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
3945c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    IdentifierInfo &II = *Tok.getIdentifierInfo();
3955c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
3965c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
3975c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '::'
3985c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   namespace-name '::'
3995c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   nested-name-specifier identifier '::'
4005c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    Token Next = NextToken();
40146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
40246646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // If we get foo:bar, this is almost certainly a typo for foo::bar.  Recover
40346646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // and emit a fixit hint for it.
404b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor    if (Next.is(tok::colon) && !ColonIsSacred) {
4052e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
4062e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Tok.getLocation(),
4072e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Next.getLocation(), ObjectType,
408b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            EnteringContext) &&
409b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // If the token after the colon isn't an identifier, it's still an
410b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // error, but they probably meant something else strange so don't
411b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // recover like this.
412b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          PP.LookAhead(1).is(tok::identifier)) {
413b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
414849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateReplacement(Next.getLocation(), "::");
415b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor
416b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        // Recover as if the user wrote '::'.
417b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Next.setKind(tok::coloncolon);
418b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor      }
41946646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    }
42046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
4215c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::coloncolon)) {
42277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
42323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
42477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                                II, ObjectType)) {
425d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
4269ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
427d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
428d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
4292db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith      if (LastII)
4302db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith        *LastII = &II;
4312db075b1d3b16f0100fe06408dfb4ab7d50700a4Richard Smith
4325c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // We have an identifier followed by a '::'. Lookup this name
4335c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // as the name in a nested-name-specifier.
4345c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation IdLoc = ConsumeToken();
43546646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner      assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
43646646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner             "NextToken() not working properly!");
4375c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation CCLoc = ConsumeToken();
4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
439919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu      CheckForLParenAfterColonColon();
440919b9557b4f0155dfaaea309493ff2a702fca676Richard Trieu
4412e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      HasScopeSpecifier = true;
4422e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
4432e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                              ObjectType, EnteringContext, SS))
4442e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        SS.SetInvalid(SourceRange(IdLoc, CCLoc));
4452e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
4465c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      continue;
4475c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
449950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
450ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
4515c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
4525c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '<'
4535c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::less)) {
4545c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      TemplateTy Template;
455014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
456014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(&II, Tok.getLocation());
4571fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
45823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
4597c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                              /*hasTemplateKeyword=*/false,
460014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                                        TemplateName,
4612dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                                        ObjectType,
462495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        EnteringContext,
4631fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                                        Template,
4641fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                              MemberOfUnknownSpecialization)) {
4656796fc1adcaf57c38d072a238b016b2834afbe0dDavid Blaikie        // We have found a template name, so annotate this token
4665c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // with a template-id annotation. We do not permit the
4675c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // template-id to be translated into a type annotation,
4685c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // because some clients (e.g., the parsing of class template
4695c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // specializations) still want to see the original template-id
4705c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // token.
471ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
472e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
473e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName, false))
4749ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
4755c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
476ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      }
477ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
478d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
4794147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet          (IsTypename || IsTemplateArgumentList(1))) {
480d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // We have something like t::getAs<T>, where getAs is a
481d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // member of an unknown specialization. However, this will only
482d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
483d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
4844147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        unsigned DiagID = diag::err_missing_dependent_template_keyword;
4854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (getLangOpts().MicrosoftExt)
486cf320c6388c90f1938c264e87d77a0e43946e2c3Francois Pichet          DiagID = diag::warn_missing_dependent_template_keyword;
4874147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet
4884147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        Diag(Tok.getLocation(), DiagID)
489d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << II.getName()
490d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
491d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
492d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TemplateNameKind TNK
49323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor              = Actions.ActOnDependentTemplateName(getCurScope(),
494e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                   SS, SourceLocation(),
495d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   TemplateName, ObjectType,
496d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   EnteringContext, Template)) {
497d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          // Consume the identifier.
498d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          ConsumeToken();
499e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara          if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
500e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      TemplateName, false))
501e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara            return true;
502d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        }
503d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        else
504d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          return true;
505d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor
506d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        continue;
5075c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
5085c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
5095c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
51039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // We don't have any tokens that form the beginning of a
51139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier, so we're done.
51239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    break;
51339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
5141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
515d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Even if we didn't see any pieces of a nested-name-specifier, we
516d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // still check whether there is a tilde in this position, which
517d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // indicates a potential pseudo-destructor.
518d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (CheckForDestructor && Tok.is(tok::tilde))
519d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = true;
520d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
5219ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
522eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
523eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
524eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression.
525eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
526eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       id-expression:
527eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         unqualified-id
528eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         qualified-id
529eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
530eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
531eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
532eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' identifier
533eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' operator-function-id
534edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor///         '::' template-id
535eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
536eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not
537eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect:
538eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
539eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' conversion-function-id
540eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' '~' class-name
541eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
542eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics:
543eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
544eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {};
545eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {}
546eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() {
547eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: A :: ~ C(); // Some Sema error about using destructor with a
548eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///                  // namespace.
549eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: ~ C(); // Some Parser error like 'unexpected ~'.
550eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// }
551eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
552eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like:
553eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
554eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
555eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
556eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' unqualified-id
557eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
558eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the
559eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope.
560eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
561ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a
562ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts,
563ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may
564ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear.
565ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl///
56660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
567eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  // qualified-id:
568eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
569eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::' unqualified-id
570eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //
571eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
572efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
573e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
574e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SourceLocation TemplateKWLoc;
57502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  UnqualifiedId Name;
576e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  if (ParseUnqualifiedId(SS,
577e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*EnteringContext=*/false,
578e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*AllowDestructorName=*/false,
579e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*AllowConstructorName=*/false,
580b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                         /*ObjectType=*/ ParsedType(),
581e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         TemplateKWLoc,
58202a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         Name))
58302a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor    return ExprError();
584b681b61fea36618778b8030360e90e3f4641233bJohn McCall
585b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // This is only the direct operand of an & operator if it is not
586b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // followed by a postfix-expression suffix.
5879c72c6088d591ace8503b842d39448c2040f3033John McCall  if (isAddressOfOperand && isPostfixExpressionSuffixStart())
5889c72c6088d591ace8503b842d39448c2040f3033John McCall    isAddressOfOperand = false;
589e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
590e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
591e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   Tok.is(tok::l_paren), isAddressOfOperand);
592eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
593eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
5940a664b863255065d960342dd074a77d63c753d35Richard Smith/// ParseLambdaExpression - Parse a C++11 lambda expression.
595ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
596ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-expression:
597ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         lambda-introducer lambda-declarator[opt] compound-statement
598ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
599ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-introducer:
600ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '[' lambda-capture[opt] ']'
601ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
602ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-capture:
603ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-default
604ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-list
605ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-default ',' capture-list
606ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
607ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture-default:
608ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '&'
609ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '='
610ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
611ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture-list:
612ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture
613ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-list ',' capture
614ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
615ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture:
6160a664b863255065d960342dd074a77d63c753d35Richard Smith///         simple-capture
6170a664b863255065d960342dd074a77d63c753d35Richard Smith///         init-capture     [C++1y]
6180a664b863255065d960342dd074a77d63c753d35Richard Smith///
6190a664b863255065d960342dd074a77d63c753d35Richard Smith///       simple-capture:
620ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         identifier
621ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '&' identifier
622ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         'this'
623ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
6240a664b863255065d960342dd074a77d63c753d35Richard Smith///       init-capture:      [C++1y]
6250a664b863255065d960342dd074a77d63c753d35Richard Smith///         identifier initializer
6260a664b863255065d960342dd074a77d63c753d35Richard Smith///         '&' identifier initializer
6270a664b863255065d960342dd074a77d63c753d35Richard Smith///
628ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-declarator:
629ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '(' parameter-declaration-clause ')' attribute-specifier[opt]
630ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///           'mutable'[opt] exception-specification[opt]
631ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///           trailing-return-type[opt]
632ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
633ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::ParseLambdaExpression() {
634ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse lambda-introducer.
635ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  LambdaIntroducer Intro;
636ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
637dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie  Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
638ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (DiagID) {
639ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Diag(Tok, DiagID.getValue());
6408fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_square, StopAtSemi);
6418fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::l_brace, StopAtSemi);
6428fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_brace, StopAtSemi);
643dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprError();
644ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
645ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
646ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return ParseLambdaExpressionAfterIntroducer(Intro);
647ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
648ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
649ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// TryParseLambdaExpression - Use lookahead and potentially tentative
650ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// parsing to determine if we are looking at a C++0x lambda expression, and parse
651ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// it if we are.
652ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
653ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// If we are not looking at a lambda expression, returns ExprError().
654ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::TryParseLambdaExpression() {
65580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  assert(getLangOpts().CPlusPlus11
656ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor         && Tok.is(tok::l_square)
657ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor         && "Not at the start of a possible lambda expression.");
658ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
659ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  const Token Next = NextToken(), After = GetLookAheadToken(2);
660ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
661ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // If lookahead indicates this is a lambda...
662ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Next.is(tok::r_square) ||     // []
663ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Next.is(tok::equal) ||        // [=
664ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (Next.is(tok::amp) &&         // [&] or [&,
665ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor       (After.is(tok::r_square) ||
666ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        After.is(tok::comma))) ||
667ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (Next.is(tok::identifier) &&  // [identifier]
668ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor       After.is(tok::r_square))) {
669ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    return ParseLambdaExpression();
670ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
671ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
672dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // If lookahead indicates an ObjC message send...
673dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // [identifier identifier
674ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Next.is(tok::identifier) && After.is(tok::identifier)) {
675dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprEmpty();
676ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
677ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
678dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // Here, we're stuck: lambda introducers and Objective-C message sends are
679dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // unambiguous, but it requires arbitrary lookhead.  [a,b,c,d,e,f,g] is a
680dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send.  Instead of
681dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // writing two routines to parse a lambda introducer, just try to parse
682dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // a lambda introducer first, and fall back if that fails.
683dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // (TryParseLambdaIntroducer never produces any diagnostic output.)
684ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  LambdaIntroducer Intro;
685ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (TryParseLambdaIntroducer(Intro))
686dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprEmpty();
687ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return ParseLambdaExpressionAfterIntroducer(Intro);
688ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
689ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
690440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith/// \brief Parse a lambda introducer.
691440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith/// \param Intro A LambdaIntroducer filled in with information about the
692440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith///        contents of the lambda-introducer.
693440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith/// \param SkippedInits If non-null, we are disambiguating between an Obj-C
694440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith///        message send and a lambda expression. In this mode, we will
695440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith///        sometimes skip the initializers for init-captures and not fully
696440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith///        populate \p Intro. This flag will be set to \c true if we do so.
697440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith/// \return A DiagnosticID if it hit something unexpected. The location for
698440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith///         for the diagnostic is that of the current token.
699440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard SmithOptional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
700440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith                                                 bool *SkippedInits) {
701dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie  typedef Optional<unsigned> DiagResult;
702ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
703ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
7044a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_square);
7054a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
7064a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
7074a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Intro.Range.setBegin(T.getOpenLocation());
708ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
709ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  bool first = true;
710ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
711ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse capture-default.
712ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Tok.is(tok::amp) &&
713ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
714ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Intro.Default = LCD_ByRef;
7153ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor    Intro.DefaultLoc = ConsumeToken();
716ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    first = false;
717ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  } else if (Tok.is(tok::equal)) {
718ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Intro.Default = LCD_ByCopy;
7193ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor    Intro.DefaultLoc = ConsumeToken();
720ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    first = false;
721ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
722ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
723ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  while (Tok.isNot(tok::r_square)) {
724ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (!first) {
72581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      if (Tok.isNot(tok::comma)) {
726437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // Provide a completion for a lambda introducer here. Except
727437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // in Objective-C, where this is Almost Surely meant to be a message
728437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // send. In that case, fail here and let the ObjC message
729437fbc51c260780af2f639da6af1c1f91e66f2abDouglas Gregor        // expression parser perform the completion.
730d48ab06b178e400ac31ef4fe649e9c33d2caf651Douglas Gregor        if (Tok.is(tok::code_completion) &&
731d48ab06b178e400ac31ef4fe649e9c33d2caf651Douglas Gregor            !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
732d48ab06b178e400ac31ef4fe649e9c33d2caf651Douglas Gregor              !Intro.Captures.empty())) {
73381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
73481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                               /*AfterAmpersand=*/false);
73581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          ConsumeCodeCompletionToken();
73681f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          break;
73781f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        }
73881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
739ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_expected_comma_or_rsquare);
74081f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      }
741ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      ConsumeToken();
742ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
743ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
74481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    if (Tok.is(tok::code_completion)) {
74581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      // If we're in Objective-C++ and we have a bare '[', then this is more
74681f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      // likely to be a message receiver.
7474e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (getLangOpts().ObjC1 && first)
74881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        Actions.CodeCompleteObjCMessageReceiver(getCurScope());
74981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      else
75081f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
75181f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                             /*AfterAmpersand=*/false);
75281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      ConsumeCodeCompletionToken();
75381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      break;
75481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    }
755ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
75681f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    first = false;
75781f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
758ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse capture.
759ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    LambdaCaptureKind Kind = LCK_ByCopy;
760ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation Loc;
761ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    IdentifierInfo* Id = 0;
762a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor    SourceLocation EllipsisLoc;
7630a664b863255065d960342dd074a77d63c753d35Richard Smith    ExprResult Init;
764a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor
765ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::kw_this)) {
766ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Kind = LCK_This;
767ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Loc = ConsumeToken();
768ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    } else {
769ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Tok.is(tok::amp)) {
770ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Kind = LCK_ByRef;
771ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        ConsumeToken();
77281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
77381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        if (Tok.is(tok::code_completion)) {
77481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
77581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                               /*AfterAmpersand=*/true);
77681f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          ConsumeCodeCompletionToken();
77781f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          break;
77881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        }
779ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      }
780ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
781ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Tok.is(tok::identifier)) {
782ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Id = Tok.getIdentifierInfo();
783ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Loc = ConsumeToken();
784ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      } else if (Tok.is(tok::kw_this)) {
785ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // FIXME: If we want to suggest a fixit here, will need to return more
786ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
787ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // Clear()ed to prevent emission in case of tentative parsing?
788ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_this_captured_by_reference);
789ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      } else {
790ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_expected_capture);
791ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      }
7920a664b863255065d960342dd074a77d63c753d35Richard Smith
7930a664b863255065d960342dd074a77d63c753d35Richard Smith      if (Tok.is(tok::l_paren)) {
7940a664b863255065d960342dd074a77d63c753d35Richard Smith        BalancedDelimiterTracker Parens(*this, tok::l_paren);
7950a664b863255065d960342dd074a77d63c753d35Richard Smith        Parens.consumeOpen();
7960a664b863255065d960342dd074a77d63c753d35Richard Smith
7970a664b863255065d960342dd074a77d63c753d35Richard Smith        ExprVector Exprs;
7980a664b863255065d960342dd074a77d63c753d35Richard Smith        CommaLocsTy Commas;
799440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        if (SkippedInits) {
800440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          Parens.skipToEnd();
801440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          *SkippedInits = true;
802440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        } else if (ParseExpressionList(Exprs, Commas)) {
8030a664b863255065d960342dd074a77d63c753d35Richard Smith          Parens.skipToEnd();
8040a664b863255065d960342dd074a77d63c753d35Richard Smith          Init = ExprError();
8050a664b863255065d960342dd074a77d63c753d35Richard Smith        } else {
8060a664b863255065d960342dd074a77d63c753d35Richard Smith          Parens.consumeClose();
8070a664b863255065d960342dd074a77d63c753d35Richard Smith          Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
8080a664b863255065d960342dd074a77d63c753d35Richard Smith                                            Parens.getCloseLocation(),
8090a664b863255065d960342dd074a77d63c753d35Richard Smith                                            Exprs);
8100a664b863255065d960342dd074a77d63c753d35Richard Smith        }
8110a664b863255065d960342dd074a77d63c753d35Richard Smith      } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
8120a664b863255065d960342dd074a77d63c753d35Richard Smith        if (Tok.is(tok::equal))
8130a664b863255065d960342dd074a77d63c753d35Richard Smith          ConsumeToken();
8140a664b863255065d960342dd074a77d63c753d35Richard Smith
815440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        if (!SkippedInits)
816440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          Init = ParseInitializer();
817440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        else if (Tok.is(tok::l_brace)) {
818440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          BalancedDelimiterTracker Braces(*this, tok::l_brace);
819440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          Braces.consumeOpen();
820440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          Braces.skipToEnd();
821440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          *SkippedInits = true;
822440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        } else {
823440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // We're disambiguating this:
824440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          //
825440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          //   [..., x = expr
826440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          //
827440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // We need to find the end of the following expression in order to
828440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // determine whether this is an Obj-C message send's receiver, or a
829440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // lambda init-capture.
830440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          //
831440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // Parse the expression to find where it ends, and annotate it back
832440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // onto the tokens. We would have parsed this expression the same way
833440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // in either case: both the RHS of an init-capture and the RHS of an
834440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // assignment expression are parsed as an initializer-clause, and in
835440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // neither case can anything be added to the scope between the '[' and
836440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // here.
837440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          //
838440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // FIXME: This is horrible. Adding a mechanism to skip an expression
839440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // would be much cleaner.
840440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // FIXME: If there is a ',' before the next ']' or ':', we can skip to
841440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // that instead. (And if we see a ':' with no matching '?', we can
842440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          // classify this as an Obj-C message send.)
843440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          SourceLocation StartLoc = Tok.getLocation();
844440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
845440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          Init = ParseInitializer();
846440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith
847440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          if (Tok.getLocation() != StartLoc) {
848440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            // Back out the lexing of the token after the initializer.
849440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            PP.RevertCachedTokens(1);
850440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith
851440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            // Replace the consumed tokens with an appropriate annotation.
852440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            Tok.setLocation(StartLoc);
853440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            Tok.setKind(tok::annot_primary_expr);
854440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            setExprAnnotation(Tok, Init);
855440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
856440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            PP.AnnotateCachedTokens(Tok);
857440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith
858440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            // Consume the annotated initializer.
859440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith            ConsumeToken();
860440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith          }
861440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith        }
8620d8e9646bc000bab521ce52ed294209a92298cefRichard Smith      } else if (Tok.is(tok::ellipsis))
8630d8e9646bc000bab521ce52ed294209a92298cefRichard Smith        EllipsisLoc = ConsumeToken();
864ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
865ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
8660a664b863255065d960342dd074a77d63c753d35Richard Smith    Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init);
867ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
868ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
8694a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
8704a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Intro.Range.setEnd(T.getCloseLocation());
871ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
872ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return DiagResult();
873ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
874ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
87581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
876ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
877ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// Returns true if it hit something unexpected.
878ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregorbool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
879ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  TentativeParsingAction PA(*this);
880ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
881440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  bool SkippedInits = false;
882440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
883ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
884ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (DiagID) {
885ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    PA.Revert();
886ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    return true;
887ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
888ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
889440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  if (SkippedInits) {
890440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    // Parse it again, but this time parse the init-captures too.
891440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    PA.Revert();
892440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    Intro = LambdaIntroducer();
893440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    DiagID = ParseLambdaIntroducer(Intro);
894440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    assert(!DiagID && "parsing lambda-introducer failed on reparse");
895440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    return false;
896440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  }
897440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith
898ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  PA.Commit();
899ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return false;
900ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
901ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
902ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
903ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// expression.
904ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::ParseLambdaExpressionAfterIntroducer(
905ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                     LambdaIntroducer &Intro) {
906dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
907dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
908dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
909dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
910dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman                                "lambda expression parsing");
911dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
912fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
913fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
9140a664b863255065d960342dd074a77d63c753d35Richard Smith  // FIXME: Call into Actions to add any init-capture declarations to the
9150a664b863255065d960342dd074a77d63c753d35Richard Smith  // scope while parsing the lambda-declarator and compound-statement.
9160a664b863255065d960342dd074a77d63c753d35Richard Smith
917ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse lambda-declarator[opt].
918ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  DeclSpec DS(AttrFactory);
919f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman  Declarator D(DS, Declarator::LambdaExprContext);
920fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali  TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
921fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali  Actions.PushLambdaScope();
922ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
923ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Tok.is(tok::l_paren)) {
924ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ParseScope PrototypeScope(this,
925ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                              Scope::FunctionPrototypeScope |
9263a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith                              Scope::FunctionDeclarationScope |
927ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                              Scope::DeclScope);
928ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
92959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation DeclEndLoc;
9304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
9314a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
93259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation LParenLoc = T.getOpenLocation();
933ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
934ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse parameter-declaration-clause.
935ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ParsedAttributes Attr(AttrFactory);
936cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
937ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation EllipsisLoc;
938ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
939fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
940fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    if (Tok.isNot(tok::r_paren)) {
941fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      Actions.RecordParsingTemplateParameterDepth(TemplateParameterDepth);
942ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
943fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // For a generic lambda, each 'auto' within the parameter declaration
944fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // clause creates a template type parameter, so increment the depth.
945fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      if (Actions.getCurGenericLambda())
946fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        ++CurTemplateDepthTracker;
947fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    }
9484a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
94959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation RParenLoc = T.getCloseLocation();
95059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    DeclEndLoc = RParenLoc;
951ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
952ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse 'mutable'[opt].
953ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation MutableLoc;
954ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::kw_mutable)) {
955ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      MutableLoc = ConsumeToken();
956ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      DeclEndLoc = MutableLoc;
957ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
958ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
959ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse exception-specification[opt].
960ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ExceptionSpecificationType ESpecType = EST_None;
961ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceRange ESpecRange;
962cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<ParsedType, 2> DynamicExceptions;
963cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<SourceRange, 2> DynamicExceptionRanges;
964ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ExprResult NoexceptExpr;
965a058fd4f0a944174295f77169b438510dad389f8Richard Smith    ESpecType = tryParseExceptionSpecification(ESpecRange,
96674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                               DynamicExceptions,
96774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                               DynamicExceptionRanges,
968a058fd4f0a944174295f77169b438510dad389f8Richard Smith                                               NoexceptExpr);
969ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
970ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (ESpecType != EST_None)
971ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      DeclEndLoc = ESpecRange.getEnd();
972ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
973ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse attribute-specifier[opt].
9744e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
975ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
97659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation FunLocalRangeEnd = DeclEndLoc;
97759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara
978ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse trailing-return-type[opt].
97954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith    TypeResult TrailingReturnType;
980ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::arrow)) {
98159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      FunLocalRangeEnd = Tok.getLocation();
982ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      SourceRange Range;
98354655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith      TrailingReturnType = ParseTrailingReturnType(Range);
984ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Range.getEnd().isValid())
985ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        DeclEndLoc = Range.getEnd();
986ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
987ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
988ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    PrototypeScope.Exit();
989ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
99059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation NoLoc;
991ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
99259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*isAmbiguous=*/false,
99359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           LParenLoc,
994ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           ParamInfo.data(), ParamInfo.size(),
99559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           EllipsisLoc, RParenLoc,
996ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DS.getTypeQualifiers(),
997ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           /*RefQualifierIsLValueRef=*/true,
99859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*RefQualifierLoc=*/NoLoc,
99959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*ConstQualifierLoc=*/NoLoc,
100059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           /*VolatileQualifierLoc=*/NoLoc,
1001ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           MutableLoc,
1002ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           ESpecType, ESpecRange.getBegin(),
1003ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptions.data(),
1004ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptionRanges.data(),
1005ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptions.size(),
1006ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           NoexceptExpr.isUsable() ?
1007ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                             NoexceptExpr.get() : 0,
100859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                           LParenLoc, FunLocalRangeEnd, D,
1009ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           TrailingReturnType),
1010ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                  Attr, DeclEndLoc);
1011c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor  } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
1012c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    // It's common to forget that one needs '()' before 'mutable' or the
1013c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    // result type. Deal with this.
1014c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    Diag(Tok, diag::err_lambda_missing_parens)
1015c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      << Tok.is(tok::arrow)
1016c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
1017c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    SourceLocation DeclLoc = Tok.getLocation();
1018c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    SourceLocation DeclEndLoc = DeclLoc;
1019c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor
1020c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    // Parse 'mutable', if it's there.
1021c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    SourceLocation MutableLoc;
1022c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    if (Tok.is(tok::kw_mutable)) {
1023c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      MutableLoc = ConsumeToken();
1024c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      DeclEndLoc = MutableLoc;
1025c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    }
1026c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor
1027c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    // Parse the return type, if there is one.
102854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith    TypeResult TrailingReturnType;
1029c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    if (Tok.is(tok::arrow)) {
1030c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      SourceRange Range;
103154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith      TrailingReturnType = ParseTrailingReturnType(Range);
1032c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor      if (Range.getEnd().isValid())
1033c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor        DeclEndLoc = Range.getEnd();
1034c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    }
1035c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor
1036c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    ParsedAttributes Attr(AttrFactory);
103759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara    SourceLocation NoLoc;
1038c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
103959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*isAmbiguous=*/false,
104059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*LParenLoc=*/NoLoc,
104159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*Params=*/0,
104259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*NumParams=*/0,
104359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*EllipsisLoc=*/NoLoc,
104459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*RParenLoc=*/NoLoc,
104559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*TypeQuals=*/0,
104659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*RefQualifierIsLValueRef=*/true,
104759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*RefQualifierLoc=*/NoLoc,
104859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*ConstQualifierLoc=*/NoLoc,
104959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*VolatileQualifierLoc=*/NoLoc,
105059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               MutableLoc,
105159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               EST_None,
105259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*ESpecLoc=*/NoLoc,
105359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*Exceptions=*/0,
105459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*ExceptionRanges=*/0,
105559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*NumExceptions=*/0,
105659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               /*NoexceptExpr=*/0,
105759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               DeclLoc, DeclEndLoc, D,
105859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                                               TrailingReturnType),
1059c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor                  Attr, DeclEndLoc);
1060ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
1061c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor
1062ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1063906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1064906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  // it.
1065fccfb625e3090e77da9b6a79edcab159c7006685Douglas Gregor  unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
1066fccfb625e3090e77da9b6a79edcab159c7006685Douglas Gregor  ParseScope BodyScope(this, ScopeFlags);
1067906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman
1068ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman  Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1069ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman
1070ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse compound-statement.
1071dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  if (!Tok.is(tok::l_brace)) {
1072ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Diag(Tok, diag::err_expected_lambda_body);
1073dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1074dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprError();
1075ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
1076ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
1077dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  StmtResult Stmt(ParseCompoundStatementBody());
1078dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  BodyScope.Exit();
1079dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
1080deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  if (!Stmt.isInvalid())
10819e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor    return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
1082dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
1083deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1084deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  return ExprError();
1085ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
1086ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type.
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       postfix-expression: [C++ 5.2p1]
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'dynamic_cast' '<' type-name '>' '(' expression ')'
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'static_cast' '<' type-name '>' '(' expression ')'
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'const_cast' '<' type-name '>' '(' expression ')'
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
109660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXCasts() {
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CastName = 0;     // For error messages
10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Kind) {
1101eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie  default: llvm_unreachable("Unknown C++ cast!");
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_const_cast:       CastName = "const_cast";       break;
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_static_cast:      CastName = "static_cast";      break;
11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation OpLoc = ConsumeToken();
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LAngleBracketLoc = Tok.getLocation();
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1111ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Check for "<::" which is parsed as "[:".  If found, fix token stream,
1112ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // diagnose error, suggest fix, and recover parsing.
111378fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith  if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
111478fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith    Token Next = NextToken();
111578fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith    if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
111678fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith      FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
111778fe3e05a9ea1fc670e5cb0bc54f54e064595e2cRichard Smith  }
1118ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
112020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
112231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Parse the common declaration-specifiers piece.
112331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclSpec DS(AttrFactory);
112431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
112531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
112631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Parse the abstract-declarator, if present.
112731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
112831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
112931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation RAngleBracketLoc = Tok.getLocation();
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11321ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
113320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  SourceLocation LParenLoc, RParenLoc;
11364a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11384a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
113921e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    return ExprError();
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
114160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = ParseExpression();
11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
114321e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  // Match the ')'.
11444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
114631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
114749badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
114831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                       LAngleBracketLoc, DeclaratorInfo,
1149809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                       RAngleBracketLoc,
11504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getOpenLocation(), Result.take(),
11514a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
11525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11533fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1156c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression.
1157c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
1158c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///       postfix-expression: [C++ 5.2p1]
1159c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' expression ')'
1160c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' type-id ')'
1161c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
116260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXTypeid() {
1163c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1164c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1165c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation OpLoc = ConsumeToken();
11664a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  SourceLocation LParenLoc, RParenLoc;
11674a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
1168c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1169c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  // typeid expressions are always parenthesized.
11704a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
117120df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
11724a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  LParenLoc = T.getOpenLocation();
1173c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
117460d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result;
1175c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
11760576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // C++0x [expr.typeid]p3:
11770576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //   When typeid is applied to an expression other than an lvalue of a
11780576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //   polymorphic class type [...] The expression is an unevaluated
11790576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //   operand (Clause 5).
11800576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //
11810576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Note that we can't tell whether the expression is an lvalue of a
11820576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // polymorphic class type until after we've parsed the expression; we
11830576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // speculatively assume the subexpression is unevaluated, and fix it up
11840576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // later.
11850576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  //
11860576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // We enter the unevaluated context before trying to determine whether we
11870576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // have a type-id, because the tentative parse logic will try to resolve
11880576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // names, and must treat them as unevaluated.
118980bfa3d125fa0b9c636977ea37b4a55b2c9b1037Eli Friedman  EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
119080bfa3d125fa0b9c636977ea37b4a55b2c9b1037Eli Friedman                                               Sema::ReuseLambdaContextDecl);
11910576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
1192c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (isTypeIdInParens()) {
1193809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult Ty = ParseTypeName();
1194c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1195c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
11964a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
11974a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    RParenLoc = T.getCloseLocation();
11984eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    if (Ty.isInvalid() || RParenLoc.isInvalid())
119920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
1200c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1201c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
1202b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                    Ty.get().getAsOpaquePtr(), RParenLoc);
1203c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  } else {
1204c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = ParseExpression();
1205c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
1206c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
12070e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Result.isInvalid())
12088fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::r_paren, StopAtSemi);
1209c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    else {
12104a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
12114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      RParenLoc = T.getCloseLocation();
12124eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor      if (RParenLoc.isInvalid())
12134eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor        return ExprError();
1214fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor
1215c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
1216effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl                                      Result.release(), RParenLoc);
1217c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    }
1218c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  }
1219c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
12203fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
1221c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
1222c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
122301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
122401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
122501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' expression ')'
122601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' type-id ')'
122701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
122801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetExprResult Parser::ParseCXXUuidof() {
122901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
123001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
123101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  SourceLocation OpLoc = ConsumeToken();
12324a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
123301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
123401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  // __uuidof expressions are always parenthesized.
12354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
123601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    return ExprError();
123701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
123801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  ExprResult Result;
123901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
124001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  if (isTypeIdInParens()) {
124101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    TypeResult Ty = ParseTypeName();
124201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
124301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
12444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
124501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
124601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Ty.isInvalid())
124701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      return ExprError();
124801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
12494a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
12504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    Ty.get().getAsOpaquePtr(),
12514a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    T.getCloseLocation());
125201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  } else {
125301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
125401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    Result = ParseExpression();
125501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
125601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
125701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Result.isInvalid())
12588fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::r_paren, StopAtSemi);
125901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    else {
12604a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
126101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
12624a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
12634a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      /*isType=*/false,
12644a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      Result.release(), T.getCloseLocation());
126501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    }
126601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  }
126701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
12683fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
126901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet}
127001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
1271d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \brief Parse a C++ pseudo-destructor expression after the base,
1272d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// . or -> operator, and nested-name-specifier have already been
1273d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// parsed.
1274d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
1275d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       postfix-expression: [C++ 5.2]
1276d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression . pseudo-destructor-name
1277d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression -> pseudo-destructor-name
1278d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
1279d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       pseudo-destructor-name:
1280d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1281d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier template simple-template-id ::
1282d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///                 ~type-name
1283d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] ~type-name
1284d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
128560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
1286d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1287d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 tok::TokenKind OpKind,
1288d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 CXXScopeSpec &SS,
1289b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                 ParsedType ObjectType) {
1290d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // We're parsing either a pseudo-destructor-name or a dependent
1291d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // member access that has the same form as a
1292d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // pseudo-destructor-name. We parse both in the same way and let
1293d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // the action model sort them out.
1294d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  //
1295d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Note that the ::[opt] nested-name-specifier[opt] has already
1296d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been parsed, and if there was a simple-template-id, it has
1297d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been coalesced into a template-id annotation token.
1298d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId FirstTypeName;
1299d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation CCLoc;
1300d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::identifier)) {
1301d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1302d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
1303d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1304d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
1305d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else if (Tok.is(tok::annot_template_id)) {
1306e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    // FIXME: retrieve TemplateKWLoc from template-id annotation and
1307e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    // store it in the pseudo-dtor node (to be used when instantiating it).
1308d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setTemplateId(
1309d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                              (TemplateIdAnnotation *)Tok.getAnnotationValue());
1310d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
1311d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1312d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
1313d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else {
1314d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(0, SourceLocation());
1315d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
1316d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1317d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the tilde.
1318d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1319d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation TildeLoc = ConsumeToken();
132091ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
132191ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
132291ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    DeclSpec DS(AttrFactory);
132385c60db2131c6d210d4777c3d50bdaf0e69bb8bfBenjamin Kramer    ParseDecltypeSpecifier(DS);
132491ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    if (DS.getTypeSpecType() == TST_error)
132591ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie      return ExprError();
132691ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
132791ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                             OpKind, TildeLoc, DS,
132891ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                             Tok.is(tok::l_paren));
132991ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  }
133091ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
1331d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (!Tok.is(tok::identifier)) {
1332d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    Diag(Tok, diag::err_destructor_tilde_identifier);
1333d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
1334d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
1335d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1336d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the second type.
1337d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId SecondTypeName;
1338d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  IdentifierInfo *Name = Tok.getIdentifierInfo();
1339d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation NameLoc = ConsumeToken();
1340d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SecondTypeName.setIdentifier(Name, NameLoc);
1341d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1342d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // If there is a '<', the second type name is a template-id. Parse
1343d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // it as such.
1344d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::less) &&
1345e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1346e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   Name, NameLoc,
1347e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   false, ObjectType, SecondTypeName,
1348e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   /*AssumeTemplateName=*/true))
1349d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
1350d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
13519ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
13529ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                           OpLoc, OpKind,
1353d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           SS, FirstTypeName, CCLoc,
1354d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           TildeLoc, SecondTypeName,
1355d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           Tok.is(tok::l_paren));
1356d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor}
1357d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
13585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
13595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
13605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       boolean-literal: [C++ 2.13.5]
13615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'true'
13625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'false'
136360d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXBoolLiteral() {
13645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
1365f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
13665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
136750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
136850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression.
136950dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///
137050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///       throw-expression: [C++ 15]
137150dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///         'throw' assignment-expression[opt]
137260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseThrowExpression() {
137350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  assert(Tok.is(tok::kw_throw) && "Not throw!");
137450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
137520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl
13762a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // If the current token isn't the start of an assignment-expression,
13772a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // then the expression is not present.  This handles things like:
13782a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  //   "C ? throw : (void)42", which is crazy but legal.
13792a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
13802a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::semi:
13812a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_paren:
13822a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_square:
13832a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_brace:
13842a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::colon:
13852a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::comma:
1386bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
138750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
13882a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  default:
138960d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Expr(ParseAssignmentExpression());
13903fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    if (Expr.isInvalid()) return Expr;
1391bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
13922a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  }
139350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
13944cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
13954cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer.
13964cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
13974cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
13984cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which
13994cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called.
140060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXThis() {
14014cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  assert(Tok.is(tok::kw_this) && "Not 'this'!");
14024cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  SourceLocation ThisLoc = ConsumeToken();
1403f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXThis(ThisLoc);
14044cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
1405987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1406987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1407987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)")
1408987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)")
1409987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()").
1410dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// See [C++ 5.2.3].
1411987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1412987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       postfix-expression: [C++ 5.2p1]
1413dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl///         simple-type-specifier '(' expression-list[opt] ')'
1414dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] simple-type-specifier braced-init-list
1415dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl///         typename-specifier '(' expression-list[opt] ')'
1416dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] typename-specifier braced-init-list
1417987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
141860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
141920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
1420987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1421b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
1422987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1423dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  assert((Tok.is(tok::l_paren) ||
142480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
1425dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl         && "Expected '(' or '{'!");
1426bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor
1427dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  if (Tok.is(tok::l_brace)) {
14286dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    ExprResult Init = ParseBraceInitializer();
14296dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    if (Init.isInvalid())
14306dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl      return Init;
14316dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    Expr *InitList = Init.take();
14326dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
14336dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl                                             MultiExprArg(&InitList, 1),
14346dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl                                             SourceLocation());
1435dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  } else {
14364a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
14374a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
1438dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
14394e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer    ExprVector Exprs;
1440dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    CommaLocsTy CommaLocs;
1441dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
1442dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (Tok.isNot(tok::r_paren)) {
1443dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      if (ParseExpressionList(Exprs, CommaLocs)) {
14448fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        SkipUntil(tok::r_paren, StopAtSemi);
1445dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl        return ExprError();
1446dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      }
1447987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    }
1448987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1449dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // Match the ')'.
14504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
1451987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1452dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // TypeRep could be null, if it references an invalid typedef.
1453dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (!TypeRep)
1454dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      return ExprError();
1455ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl
1456dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1457dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl           "Unexpected number of commas!");
14584a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
14593fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                             Exprs,
14604a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                             T.getCloseLocation());
1461dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  }
1462987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
1463987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
146499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// ParseCXXCondition - if/switch/while condition expression.
146571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
146671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///       condition:
146771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         expression
146871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         type-specifier-seq declarator '=' assignment-expression
14690635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith/// [C++11] type-specifier-seq declarator '=' initializer-clause
14700635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith/// [C++11] type-specifier-seq declarator braced-init-list
147171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
147271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///             '=' assignment-expression
147371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
14741ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param ExprOut if the condition was parsed as an expression, the parsed
14751ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// expression.
147699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
14771ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param DeclOut if the condition was parsed as a declaration, the parsed
14781ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// declaration.
147999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
1480586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param Loc The location of the start of the statement that requires this
1481586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// condition, e.g., the "for" in a for loop.
1482586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
1483586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param ConvertToBoolean Whether the condition expression should be
1484586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// converted to a boolean value.
1485586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
148699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \returns true if there was a parsing, false otherwise.
148760d7b3a319d84d688752be3870615ac0f111fb16John McCallbool Parser::ParseCXXCondition(ExprResult &ExprOut,
148860d7b3a319d84d688752be3870615ac0f111fb16John McCall                               Decl *&DeclOut,
1489586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               SourceLocation Loc,
1490586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               bool ConvertToBoolean) {
149101dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  if (Tok.is(tok::code_completion)) {
1492f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
14937d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
14947d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return true;
149501dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  }
149601dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor
14972edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  ParsedAttributesWithRange attrs(AttrFactory);
14984e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  MaybeParseCXX11Attributes(attrs);
14992edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
150099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!isCXXConditionDeclaration()) {
15012edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ProhibitAttributes(attrs);
15022edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
1503586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // Parse the expression.
150460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprOut = ParseExpression(); // expression
150560d7b3a319d84d688752be3870615ac0f111fb16John McCall    DeclOut = 0;
150660d7b3a319d84d688752be3870615ac0f111fb16John McCall    if (ExprOut.isInvalid())
1507586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return true;
1508586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
1509586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // If required, convert to a boolean value.
1510586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    if (ConvertToBoolean)
151160d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprOut
151260d7b3a319d84d688752be3870615ac0f111fb16John McCall        = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
151360d7b3a319d84d688752be3870615ac0f111fb16John McCall    return ExprOut.isInvalid();
151499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
151571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
151671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // type-specifier-seq
15170b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
15186b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  DS.takeAttributesFrom(attrs);
151971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
152071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
152171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // declarator
152271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
152371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
152471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
152571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // simple-asm-expr[opt]
152671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.is(tok::kw_asm)) {
1527ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
152860d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult AsmLabel(ParseSimpleAsm(&Loc));
15290e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (AsmLabel.isInvalid()) {
15308fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopAtSemi);
153199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return true;
153271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    }
1533effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    DeclaratorInfo.setAsmLabel(AsmLabel.release());
1534ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.SetRangeEnd(Loc);
153571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  }
153671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
153771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // If attributes are present, parse them.
15387f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseGNUAttributes(DeclaratorInfo);
153971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
154099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  // Type-check the declaration itself.
154160d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
15427f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                        DeclaratorInfo);
154360d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclOut = Dcl.get();
154460d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprOut = ExprError();
1545a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
154671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // '=' assignment-expression
1547d6c7c67313634b317a0d63c32be0511a121bb33dRichard Trieu  // If a '==' or '+=' is found, suggest a fixit to '='.
15480635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  bool CopyInitialization = isTokenEqualOrEqualTypo();
15490635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  if (CopyInitialization)
1550dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeToken();
15510635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith
15520635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  ExprResult InitExpr = ExprError();
155380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
15540635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Diag(Tok.getLocation(),
15550635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith         diag::warn_cxx98_compat_generalized_initializer_lists);
15560635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    InitExpr = ParseBraceInitializer();
15570635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  } else if (CopyInitialization) {
15580635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    InitExpr = ParseAssignmentExpression();
15590635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  } else if (Tok.is(tok::l_paren)) {
15600635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    // This was probably an attempt to initialize the variable.
15610635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    SourceLocation LParen = ConsumeParen(), RParen = LParen;
15628fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch))
15630635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith      RParen = ConsumeParen();
15640635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Diag(DeclOut ? DeclOut->getLocation() : LParen,
15650635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith         diag::err_expected_init_in_condition_lparen)
15660635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith      << SourceRange(LParen, RParen);
156799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  } else {
15680635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
15690635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith         diag::err_expected_init_in_condition);
157099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
15710635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith
15720635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith  if (!InitExpr.isInvalid())
15730635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith    Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
1574a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                                 DS.containsPlaceholderType());
1575dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith  else
1576dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith    Actions.ActOnInitializerError(DeclOut);
15770635aa75ab48c9c3b4269d266305aba77b6ec58eRichard Smith
1578586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // FIXME: Build a reference to this declaration? Convert it to bool?
1579586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // (This is currently handled by Sema).
1580483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
1581483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Actions.FinalizeDeclaration(DeclOut);
1582586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
158399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return false;
158471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis}
158571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
1586987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1587987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of
1588987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier.
1589987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1590987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       simple-type-specifier:
1591eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier[opt] type-name
1592987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1593987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         char
1594987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         wchar_t
1595987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         bool
1596987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         short
1597987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         int
1598987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         long
1599987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         signed
1600987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         unsigned
1601987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         float
1602987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         double
1603987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         void
1604987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU]   typeof-specifier
1605987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto               [TODO]
1606987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1607987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       type-name:
1608987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         class-name
1609987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         enum-name
1610987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typedef-name
1611987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1612987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1613987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  DS.SetRangeStart(Tok.getLocation());
1614987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  const char *PrevSpec;
1615fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
1616987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation Loc = Tok.getLocation();
16171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1618987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  switch (Tok.getKind()) {
161955a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::identifier:   // foo::bar
162055a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::coloncolon:   // ::foo::bar
1621b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Annotation token should already be formed!");
16221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
1623b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Not a simple-type-specifier token!");
162455a7cefc846765ac7d142a63f773747a20518d71Chris Lattner
1625987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // type-name
1626b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  case tok::annot_typename: {
16276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (getTypeAnnotation(Tok))
16286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
16296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                         getTypeAnnotation(Tok));
16306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    else
16316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecError();
16329bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
16339bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
16349bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    ConsumeToken();
16359bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
16369bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
16379bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
16389bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C interface.  If we don't have Objective-C or a '<', this is
16399bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // just a normal reference to a typedef name.
16404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Tok.is(tok::less) && getLangOpts().ObjC1)
16419bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor      ParseObjCProtocolQualifiers(DS);
16429bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
16439bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.Finish(Diags, PP);
16449bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    return;
1645987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
16461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1647987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // builtin types
1648987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_short:
1649fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
1650987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1651987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_long:
1652fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
1653338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet    break;
1654338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet  case tok::kw___int64:
1655338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet    DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1656987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1657987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_signed:
1658fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
1659987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1660987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_unsigned:
1661fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
1662987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1663987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_void:
1664fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
1665987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1666987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_char:
1667fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
1668987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1669987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_int:
1670fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
1671987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
16725a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case tok::kw___int128:
16735a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
16745a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
1675aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case tok::kw_half:
1676aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1677aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    break;
1678987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_float:
1679fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
1680987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1681987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_double:
1682fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
1683987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1684987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_wchar_t:
1685fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
1686987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1687f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char16_t:
1688fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
1689f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1690f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char32_t:
1691fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
1692f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1693987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_bool:
1694fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
1695987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
16965e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie  case tok::annot_decltype:
16975e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie  case tok::kw_decltype:
16985e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie    DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
16995e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie    return DS.Finish(Diags, PP);
17001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1701987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // GNU typeof support.
1702987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_typeof:
1703987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    ParseTypeofSpecifier(DS);
17049b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor    DS.Finish(Diags, PP);
1705987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    return;
1706987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
1707b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  if (Tok.is(tok::annot_typename))
1708eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1709eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  else
1710eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getLocation());
1711987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  ConsumeToken();
17129b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor  DS.Finish(Diags, PP);
1713987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
17141cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
17152f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
17162f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers,
17172f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished
17182f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are
17192f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and
17202f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false
17212f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise.
17222f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
17232f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///   type-specifier-seq: [C++ 8.1]
17242f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///     type-specifier type-specifier-seq[opt]
17252f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
17262f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
172769730c115c2d0fec2f20609d905d920a5a41b29bRichard Smith  ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
1728396a9f235e160093b5f803f7a6a18fad7b68bdbeDouglas Gregor  DS.Finish(Diags, PP);
17292f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  return false;
17302f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
17312f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
17323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of
17333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form.
17343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
17353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or
17363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
17373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will
17383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to
17393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller.
17403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
17413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if
17423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id.
17433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
17443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual
17453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name.
17463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
17473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or
17483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor.
17493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
17503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the
17513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
17523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
175346df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
175446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
175546df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
17563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id
17573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully,
17583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id.
17593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1760d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param AssumeTemplateId When true, this routine will assume that the name
1761d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// refers to a template without performing name lookup to verify.
1762d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
17633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise.
17643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1765e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          SourceLocation TemplateKWLoc,
17663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          IdentifierInfo *Name,
17673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          SourceLocation NameLoc,
17683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          bool EnteringContext,
1769b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          ParsedType ObjectType,
1770d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                          UnqualifiedId &Id,
1771e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          bool AssumeTemplateId) {
17720278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  assert((AssumeTemplateId || Tok.is(tok::less)) &&
17730278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor         "Expected '<' to finish parsing a template-id");
17743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateTy Template;
17763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateNameKind TNK = TNK_Non_template;
17773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (Id.getKind()) {
17783f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
1779014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
1780e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
1781d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    if (AssumeTemplateId) {
1782e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
1783d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Id, ObjectType, EnteringContext,
1784d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Template);
1785d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
1786d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        return true;
17871fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    } else {
17881fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
17897c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS,
17907c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateKWLoc.isValid(), Id,
17917c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   ObjectType, EnteringContext, Template,
17921fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
17931fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor
17941fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
17951fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          ObjectType && IsTemplateArgumentList()) {
17961fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // We have something like t->getAs<T>(), where getAs is a
17971fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // member of an unknown specialization. However, this will only
17981fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
17991fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
18001fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        std::string Name;
18011fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        if (Id.getKind() == UnqualifiedId::IK_Identifier)
18021fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = Id.Identifier->getName();
18031fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        else {
18041fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = "operator ";
18051fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
18061fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
18071fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          else
18081fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += Id.Identifier->getName();
18091fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        }
18101fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
18111fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << Name
18121fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << FixItHint::CreateInsertion(Id.StartLocation, "template ");
1813e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1814e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 SS, TemplateKWLoc, Id,
1815e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 ObjectType, EnteringContext,
1816e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 Template);
1817d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TNK == TNK_Non_template)
18181fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          return true;
18191fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      }
18201fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    }
18213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
18223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1823014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_ConstructorName: {
1824014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
18251fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1826014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
18277c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara    TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
18287c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                 TemplateName, ObjectType,
18291fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 EnteringContext, Template,
18301fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 MemberOfUnknownSpecialization);
18313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1832014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
18333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1834014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_DestructorName: {
1835014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
18361fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1837014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
18382d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (ObjectType) {
1839e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1840e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               SS, TemplateKWLoc, TemplateName,
1841e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               ObjectType, EnteringContext,
1842e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               Template);
1843d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
18442d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
18452d1c21414199a7452f122598189363a3922605b1Douglas Gregor    } else {
18467c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
18477c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateName, ObjectType,
18481fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   EnteringContext, Template,
18491fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
18502d1c21414199a7452f122598189363a3922605b1Douglas Gregor
1851b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
1852124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        Diag(NameLoc, diag::err_destructor_template_id)
1853124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          << Name << SS.getRange();
18542d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
18552d1c21414199a7452f122598189363a3922605b1Douglas Gregor      }
18562d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
18573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1858014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
18593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
18603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  default:
18613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
18623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
18633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
18643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (TNK == TNK_Non_template)
18653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
18663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
18673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Parse the enclosed template argument list.
18683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
18693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgList TemplateArgs;
18700278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (Tok.is(tok::less) &&
18710278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
1872059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                       SS, true, LAngleLoc,
18733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgs,
18743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       RAngleLoc))
18753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
18763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
18773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
1878e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1879e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
18803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Form a parsed representation of the template-id to be stored in the
18813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // UnqualifiedId.
18823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateIdAnnotation *TemplateId
188313bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
18843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
18853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
18863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->Name = Id.Identifier;
1887014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = OO_None;
18883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
18893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
1890014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Name = 0;
1891014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = Id.OperatorFunctionId.Operator;
1892014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
18933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
18943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1895059101f922de6eb765601459925f4c8914420b23Douglas Gregor    TemplateId->SS = SS;
18962b28bf1a8fa6e1c598805374f29e4fbf45e751feBenjamin Kramer    TemplateId->TemplateKWLoc = TemplateKWLoc;
18972b5289b6fd7e3d9899868410a498c081c9595662John McCall    TemplateId->Template = Template;
18983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Kind = TNK;
18993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->LAngleLoc = LAngleLoc;
19003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->RAngleLoc = RAngleLoc;
1901314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
19023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
1903314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor         Arg != ArgEnd; ++Arg)
19043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Args[Arg] = TemplateArgs[Arg];
19053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setTemplateId(TemplateId);
19073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
19083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
19093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Bundle the template arguments together.
19115354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
1912fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara
19133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Constructor and destructor names.
1914f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Type
191555d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
191655d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara                                  Template, NameLoc,
1917fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                  LAngleLoc, TemplateArgsPtr, RAngleLoc,
1918fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                  /*IsCtorOrDtorName=*/true);
19193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Type.isInvalid())
19203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
19213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
19233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
19243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  else
19253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
19263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return false;
19283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
19293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1930ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part
1931ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id.
19323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1933ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or
1934ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way.
19353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1936ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
19373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       operator-function-id: [C++ 13.5]
19383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         'operator' operator
19393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1940ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       operator: one of
19413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            new   delete  new[]   delete[]
19423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            +     -    *  /    %  ^    &   |   ~
19433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            !     =    <  >    += -=   *=  /=  %=
19443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
19453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
19463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ()    []
19473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
19483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-function-id: [C++ 12.3.2]
19493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator conversion-type-id
19503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
19513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-type-id:
19523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         type-specifier-seq conversion-declarator[opt]
19533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
19543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-declarator:
19553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ptr-operator conversion-declarator[opt]
19563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode
19573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
19581ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param SS The nested-name-specifier that preceded this unqualified-id. If
19593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
19603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
19613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the
19623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
19633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1964ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
1965ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed.
1966ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1967ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
1968ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1969ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise.
1970ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1971b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType ObjectType,
1972ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        UnqualifiedId &Result) {
1973ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1974ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1975ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Consume the 'operator' keyword.
1976ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation KeywordLoc = ConsumeToken();
1977ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1978ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Determine what kind of operator name we have.
1979ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  unsigned SymbolIdx = 0;
1980ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation SymbolLocations[3];
1981ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  OverloadedOperatorKind Op = OO_None;
1982ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  switch (Tok.getKind()) {
1983ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_new:
1984ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_delete: {
1985ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      bool isNew = Tok.getKind() == tok::kw_new;
1986ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the 'new' or 'delete'.
1987ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();
19886ee326af4e77e6f05973486097884d7431f2108dRichard Smith      // Check for array new/delete.
19896ee326af4e77e6f05973486097884d7431f2108dRichard Smith      if (Tok.is(tok::l_square) &&
199080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
19914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        // Consume the '[' and ']'.
19924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        BalancedDelimiterTracker T(*this, tok::l_square);
19934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeOpen();
19944a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeClose();
19954a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        if (T.getCloseLocation().isInvalid())
1996ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          return true;
1997ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
19984a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        SymbolLocations[SymbolIdx++] = T.getOpenLocation();
19994a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        SymbolLocations[SymbolIdx++] = T.getCloseLocation();
2000ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_Array_New : OO_Array_Delete;
2001ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
2002ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_New : OO_Delete;
2003ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
2004ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
2005ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
2006ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2007ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2008ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::Token:                                                     \
2009ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
2010ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_##Name;                                                    \
2011ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
2012ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2013ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def"
2014ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2015ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_paren: {
20164a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      // Consume the '(' and ')'.
20174a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      BalancedDelimiterTracker T(*this, tok::l_paren);
20184a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeOpen();
20194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
20204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      if (T.getCloseLocation().isInvalid())
2021ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
2022ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
20234a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getOpenLocation();
20244a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getCloseLocation();
2025ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Call;
2026ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
2027ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
2028ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2029ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_square: {
20304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      // Consume the '[' and ']'.
20314a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      BalancedDelimiterTracker T(*this, tok::l_square);
20324a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeOpen();
20334a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
20344a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      if (T.getCloseLocation().isInvalid())
2035ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
2036ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
20374a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getOpenLocation();
20384a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getCloseLocation();
2039ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Subscript;
2040ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
2041ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
2042ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2043ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::code_completion: {
2044ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Code completion for the operator name.
204523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOperatorName(getCurScope());
20467d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
2047ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Don't try to parse any further.
2048ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return true;
2049ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
2050ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2051ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    default:
2052ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
2053ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
2054ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2055ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Op != OO_None) {
2056ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // We have parsed an operator-function-id.
2057ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2058ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return false;
2059ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
20600486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
20610486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  // Parse a literal-operator-id.
20620486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //
2063aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith  //   literal-operator-id: C++11 [over.literal]
2064aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith  //     operator string-literal identifier
2065aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith  //     operator user-defined-string-literal
20660486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
206780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
20687fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith    Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
206933762775706e81c17ca774102ceda36049ecc593Richard Smith
207033762775706e81c17ca774102ceda36049ecc593Richard Smith    SourceLocation DiagLoc;
207133762775706e81c17ca774102ceda36049ecc593Richard Smith    unsigned DiagId = 0;
207233762775706e81c17ca774102ceda36049ecc593Richard Smith
207333762775706e81c17ca774102ceda36049ecc593Richard Smith    // We're past translation phase 6, so perform string literal concatenation
207433762775706e81c17ca774102ceda36049ecc593Richard Smith    // before checking for "".
2075cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<Token, 4> Toks;
2076cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<SourceLocation, 4> TokLocs;
207733762775706e81c17ca774102ceda36049ecc593Richard Smith    while (isTokenStringLiteral()) {
207833762775706e81c17ca774102ceda36049ecc593Richard Smith      if (!Tok.is(tok::string_literal) && !DiagId) {
2079aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith        // C++11 [over.literal]p1:
2080aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith        //   The string-literal or user-defined-string-literal in a
2081aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith        //   literal-operator-id shall have no encoding-prefix [...].
208233762775706e81c17ca774102ceda36049ecc593Richard Smith        DiagLoc = Tok.getLocation();
208333762775706e81c17ca774102ceda36049ecc593Richard Smith        DiagId = diag::err_literal_operator_string_prefix;
208433762775706e81c17ca774102ceda36049ecc593Richard Smith      }
208533762775706e81c17ca774102ceda36049ecc593Richard Smith      Toks.push_back(Tok);
208633762775706e81c17ca774102ceda36049ecc593Richard Smith      TokLocs.push_back(ConsumeStringToken());
208799831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    }
20880486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
208933762775706e81c17ca774102ceda36049ecc593Richard Smith    StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
209033762775706e81c17ca774102ceda36049ecc593Richard Smith    if (Literal.hadError)
209133762775706e81c17ca774102ceda36049ecc593Richard Smith      return true;
209233762775706e81c17ca774102ceda36049ecc593Richard Smith
209333762775706e81c17ca774102ceda36049ecc593Richard Smith    // Grab the literal operator's suffix, which will be either the next token
209433762775706e81c17ca774102ceda36049ecc593Richard Smith    // or a ud-suffix from the string literal.
209533762775706e81c17ca774102ceda36049ecc593Richard Smith    IdentifierInfo *II = 0;
209633762775706e81c17ca774102ceda36049ecc593Richard Smith    SourceLocation SuffixLoc;
209733762775706e81c17ca774102ceda36049ecc593Richard Smith    if (!Literal.getUDSuffix().empty()) {
209833762775706e81c17ca774102ceda36049ecc593Richard Smith      II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
209933762775706e81c17ca774102ceda36049ecc593Richard Smith      SuffixLoc =
210033762775706e81c17ca774102ceda36049ecc593Richard Smith        Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
210133762775706e81c17ca774102ceda36049ecc593Richard Smith                                       Literal.getUDSuffixOffset(),
21024e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                       PP.getSourceManager(), getLangOpts());
210333762775706e81c17ca774102ceda36049ecc593Richard Smith    } else if (Tok.is(tok::identifier)) {
210433762775706e81c17ca774102ceda36049ecc593Richard Smith      II = Tok.getIdentifierInfo();
210533762775706e81c17ca774102ceda36049ecc593Richard Smith      SuffixLoc = ConsumeToken();
210633762775706e81c17ca774102ceda36049ecc593Richard Smith      TokLocs.push_back(SuffixLoc);
210733762775706e81c17ca774102ceda36049ecc593Richard Smith    } else {
21080486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_expected_ident);
21090486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      return true;
21100486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    }
21110486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
211233762775706e81c17ca774102ceda36049ecc593Richard Smith    // The string literal must be empty.
211333762775706e81c17ca774102ceda36049ecc593Richard Smith    if (!Literal.GetString().empty() || Literal.Pascal) {
2114aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      // C++11 [over.literal]p1:
2115aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      //   The string-literal or user-defined-string-literal in a
2116aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      //   literal-operator-id shall [...] contain no characters
2117aa9a8ce5d31975900c7243b1508f5111baddacbeRichard Smith      //   other than the implicit terminating '\0'.
211833762775706e81c17ca774102ceda36049ecc593Richard Smith      DiagLoc = TokLocs.front();
211933762775706e81c17ca774102ceda36049ecc593Richard Smith      DiagId = diag::err_literal_operator_string_not_empty;
212033762775706e81c17ca774102ceda36049ecc593Richard Smith    }
212133762775706e81c17ca774102ceda36049ecc593Richard Smith
212233762775706e81c17ca774102ceda36049ecc593Richard Smith    if (DiagId) {
212333762775706e81c17ca774102ceda36049ecc593Richard Smith      // This isn't a valid literal-operator-id, but we think we know
212433762775706e81c17ca774102ceda36049ecc593Richard Smith      // what the user meant. Tell them what they should have written.
2125cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko      SmallString<32> Str;
212633762775706e81c17ca774102ceda36049ecc593Richard Smith      Str += "\"\" ";
212733762775706e81c17ca774102ceda36049ecc593Richard Smith      Str += II->getName();
212833762775706e81c17ca774102ceda36049ecc593Richard Smith      Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
212933762775706e81c17ca774102ceda36049ecc593Richard Smith          SourceRange(TokLocs.front(), TokLocs.back()), Str);
213033762775706e81c17ca774102ceda36049ecc593Richard Smith    }
213133762775706e81c17ca774102ceda36049ecc593Richard Smith
213233762775706e81c17ca774102ceda36049ecc593Richard Smith    Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
21333e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    return false;
21340486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  }
2135ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2136ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse a conversion-function-id.
2137ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
2138ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-function-id: [C++ 12.3.2]
2139ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     operator conversion-type-id
2140ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
2141ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-type-id:
2142ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     type-specifier-seq conversion-declarator[opt]
2143ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
2144ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-declarator:
2145ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     ptr-operator conversion-declarator[opt]
2146ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2147ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the type-specifier-seq.
21480b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
2149f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor  if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
2150ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
2151ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2152ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the conversion-declarator, which is merely a sequence of
2153ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // ptr-operators.
215414f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith  Declarator D(DS, Declarator::ConversionIdContext);
2155ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2156ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2157ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Finish up the type.
2158f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
2159ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Ty.isInvalid())
2160ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
2161ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2162ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Note that this is a conversion-function-id.
2163ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2164ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                 D.getSourceRange().getEnd());
2165ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return false;
2166ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
2167ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
2168ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2169ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity.
2170ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2171ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
2172ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       unqualified-id: [C++ expr.prim.general]
2173ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         identifier
2174ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         operator-function-id
2175ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         conversion-function-id
2176ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO]
2177ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         ~ class-name
2178ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         template-id
2179ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2180ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode
2181ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
21821ddbd89bb397988dd1a4e96d8d8c2c7705a2af75Dmitri Gribenko/// \param SS The nested-name-specifier that preceded this unqualified-id. If
2183ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2184ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
2185ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the
2186ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier.
2187ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
21883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name.
21893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
21903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name.
21913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
219246df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
219346df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
219446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
21953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
21963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
21973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise.
21983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
21993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowDestructorName,
22003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowConstructorName,
2201b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                ParsedType ObjectType,
2202e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                SourceLocation& TemplateKWLoc,
22033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                UnqualifiedId &Result) {
22040278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
22050278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // Handle 'A::template B'. This is for template-ids which have not
22060278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // already been annotated by ParseOptionalCXXScopeSpecifier().
22070278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  bool TemplateSpecified = false;
22084e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
22090278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      (ObjectType || SS.isSet())) {
22100278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateSpecified = true;
22110278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateKWLoc = ConsumeToken();
22120278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  }
22130278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
22143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
22153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   identifier
22163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (when it hasn't already been annotated)
22173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::identifier)) {
22183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the identifier.
22193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *Id = Tok.getIdentifierInfo();
22203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation IdLoc = ConsumeToken();
22213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22224e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus) {
2223b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // If we're not in C++, only identifiers matter. Record the
2224b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // identifier and return.
2225b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      Result.setIdentifier(Id, IdLoc);
2226b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      return false;
2227b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    }
2228b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor
22293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (AllowConstructorName &&
223023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
22313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed a constructor name.
2232fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara      ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2233fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          &SS, false, false,
2234fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          ParsedType(),
2235fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          /*IsCtorOrDtorName=*/true,
2236fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          /*NonTrivialTypeSourceInfo=*/true);
2237fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara      Result.setConstructorName(Ty, IdLoc, IdLoc);
22383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
22393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an identifier.
22403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setIdentifier(Id, IdLoc);
22413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
22423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // If the next token is a '<', we may have a template.
22440278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less))
2245e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2246e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2247e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
22483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
22503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
22513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
22533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (already parsed and annotated)
22543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::annot_template_id)) {
225525a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
22560efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
22570efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    // If the template-name names the current class, then this is a constructor
22580efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    if (AllowConstructorName && TemplateId->Name &&
225923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
22600efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      if (SS.isSet()) {
22610efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // C++ [class.qual]p2 specifies that a qualified template-name
22620efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // is taken as the constructor name where a constructor can be
22630efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // declared. Thus, the template arguments are extraneous, so
22640efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // complain about them and remove them entirely.
22650efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Diag(TemplateId->TemplateNameLoc,
22660efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor             diag::err_out_of_line_constructor_template_id)
22670efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor          << TemplateId->Name
2268849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateRemoval(
22690efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                    SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
2270fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara        ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2271fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            TemplateId->TemplateNameLoc,
2272fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            getCurScope(),
2273fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            &SS, false, false,
2274fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            ParsedType(),
2275fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*IsCtorOrDtorName=*/true,
2276fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*NontrivialTypeSourceInfo=*/true);
2277fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara        Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
22780efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->RAngleLoc);
22790efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        ConsumeToken();
22800efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        return false;
22810efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      }
22820efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
22830efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      Result.setConstructorTemplateId(TemplateId);
22840efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      ConsumeToken();
22850efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      return false;
22860efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    }
22870efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
22883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // We have already parsed a template-id; consume the annotation token as
22893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // our unqualified-id.
22900efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    Result.setTemplateId(TemplateId);
2291e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    TemplateKWLoc = TemplateId->TemplateKWLoc;
22923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ConsumeToken();
22933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
22943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
22953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
22963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
22973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   operator-function-id
22983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   conversion-function-id
22993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::kw_operator)) {
2300ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
23013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
23023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
2303e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // If we have an operator-function-id or a literal-operator-id and the next
2304e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // token is a '<', we may have a
2305ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //
2306ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //   template-id:
2307ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //     operator-function-id < template-argument-list[opt] >
2308e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2309e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt         Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
23100278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor        (TemplateSpecified || Tok.is(tok::less)))
2311e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2312e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          0, SourceLocation(),
2313e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2314e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
23153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
23173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
23183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus &&
2320b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
23213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // C++ [expr.unary.op]p10:
23223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   There is an ambiguity in the unary-expression ~X(), where X is a
23233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   class-name. The ambiguity is resolved in favor of treating ~ as a
23243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //    unary complement rather than treating ~X as referring to a destructor.
23253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the '~'.
23273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation TildeLoc = ConsumeToken();
232853a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie
232953a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
233053a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      DeclSpec DS(AttrFactory);
233153a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
233253a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
233353a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie        Result.setDestructorName(TildeLoc, Type, EndLoc);
233453a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie        return false;
233553a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      }
233653a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      return true;
233753a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    }
23383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name.
23403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.isNot(tok::identifier)) {
2341124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      Diag(Tok, diag::err_destructor_tilde_identifier);
23423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
23433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
23443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name (or template-name in a simple-template-id).
23463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
23473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation ClassNameLoc = ConsumeToken();
23483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23490278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less)) {
2350b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
2351e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2352e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          ClassName, ClassNameLoc,
2353e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2354e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
23552d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
23562d1c21414199a7452f122598189363a3922605b1Douglas Gregor
23573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a destructor name.
2358b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2359b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              ClassNameLoc, getCurScope(),
2360b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              SS, ObjectType,
2361b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              EnteringContext);
2362124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (!Ty)
23633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
2364124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
23653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
23663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
23673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
23683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23692d1c21414199a7452f122598189363a3922605b1Douglas Gregor  Diag(Tok, diag::err_expected_unqualified_id)
23704e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    << getLangOpts().CPlusPlus;
23713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return true;
23723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
23733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
23744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
23754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors.
23761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
237759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has
237859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
237959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location.  Otherwise, "Start" is the location of the 'new' token.
23804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
23814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
23824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] new-type-id
23834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
23844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
23854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
23864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
23874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
23884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
23894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
2390cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-type-id:
2391cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   type-specifier-seq new-declarator[opt]
2392893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor/// [GNU]             attributes type-specifier-seq new-declarator[opt]
2393cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
2394cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-declarator:
2395cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   ptr-operator new-declarator[opt]
2396cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   direct-new-declarator
2397cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
23984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-initializer:
23994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list[opt] ')'
2400dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x]           braced-init-list
24014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
240260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
240359232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
240459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_new) && "expected 'new' token");
240559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken();   // Consume 'new'
24064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
24084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // second form of new-expression. It can't be a new-type-id.
24094c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24104e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer  ExprVector PlacementArgs;
24114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation PlacementLParen, PlacementRParen;
24124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24134bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor  SourceRange TypeIdParens;
24140b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
24150b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
24164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
24174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // If it turns out to be a placement, we change the type location.
24184a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
24194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
24204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    PlacementLParen = T.getOpenLocation();
2421cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
24228fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
242320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2424cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
24254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24264a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
24274a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    PlacementRParen = T.getCloseLocation();
2428cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementRParen.isInvalid()) {
24298fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
243020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2431cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
24324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2433cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementArgs.empty()) {
24344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Reset the placement locations. There was no placement.
24354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      TypeIdParens = T.getRange();
24364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      PlacementLParen = PlacementRParen = SourceLocation();
24374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    } else {
24384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // We still need the type.
24394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      if (Tok.is(tok::l_paren)) {
24404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        BalancedDelimiterTracker T(*this, tok::l_paren);
24414a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeOpen();
2442893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor        MaybeParseGNUAttributes(DeclaratorInfo);
2443cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseSpecifierQualifierList(DS);
2444ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2445cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseDeclarator(DeclaratorInfo);
24464a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeClose();
24474a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        TypeIdParens = T.getRange();
24484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      } else {
2449893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor        MaybeParseGNUAttributes(DeclaratorInfo);
2450cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        if (ParseCXXTypeSpecifierSeq(DS))
2451cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          DeclaratorInfo.setInvalidType(true);
2452ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        else {
2453ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2454cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          ParseDeclaratorInternal(DeclaratorInfo,
2455cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                                  &Parser::ParseDirectNewDeclarator);
2456ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        }
24574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      }
24584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
24594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  } else {
2460cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // A new-type-id is a simplified type-id, where essentially the
2461cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // direct-declarator is replaced by a direct-new-declarator.
2462893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor    MaybeParseGNUAttributes(DeclaratorInfo);
2463cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseCXXTypeSpecifierSeq(DS))
2464cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      DeclaratorInfo.setInvalidType(true);
2465ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    else {
2466ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2467cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      ParseDeclaratorInternal(DeclaratorInfo,
2468cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                              &Parser::ParseDirectNewDeclarator);
2469ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    }
24704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
2471eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (DeclaratorInfo.isInvalidType()) {
24728fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
247320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
2474cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
24754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24762aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  ExprResult Initializer;
24774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
24784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
24792aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    SourceLocation ConstructorLParen, ConstructorRParen;
24804e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer    ExprVector ConstructorArgs;
24814a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
24824a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
24834a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    ConstructorLParen = T.getOpenLocation();
24844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (Tok.isNot(tok::r_paren)) {
24854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      CommaLocsTy CommaLocs;
2486cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
24878fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev        SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
248820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl        return ExprError();
2489cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      }
24904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
24914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
24924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    ConstructorRParen = T.getCloseLocation();
2493cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ConstructorRParen.isInvalid()) {
24948fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
249520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2496cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
24972aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
24982aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                                             ConstructorRParen,
24993fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                             ConstructorArgs);
250080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
25017fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith    Diag(Tok.getLocation(),
25027fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith         diag::warn_cxx98_compat_generalized_initializer_lists);
25032aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    Initializer = ParseBraceInitializer();
25044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
25052aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  if (Initializer.isInvalid())
25062aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    return Initializer;
25074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2508f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
25093fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                             PlacementArgs, PlacementRParen,
25102aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                             TypeIdParens, DeclaratorInfo, Initializer.take());
25114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
25124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
25144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal.
25154c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
25164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        direct-new-declarator:
25174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '[' expression ']'
25184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   direct-new-declarator '[' constant-expression ']'
25194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
252059232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) {
25214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Parse the array dimensions.
25224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool first = true;
25234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  while (Tok.is(tok::l_square)) {
25246ee326af4e77e6f05973486097884d7431f2108dRichard Smith    // An array-size expression can't start with a lambda.
25256ee326af4e77e6f05973486097884d7431f2108dRichard Smith    if (CheckProhibitedCXX11Attribute())
25266ee326af4e77e6f05973486097884d7431f2108dRichard Smith      continue;
25276ee326af4e77e6f05973486097884d7431f2108dRichard Smith
25284a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_square);
25294a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
25304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
253160d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Size(first ? ParseExpression()
25322f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl                                : ParseConstantExpression());
25330e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Size.isInvalid()) {
25344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Recover
25358fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::r_square, StopAtSemi);
25364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
25374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
25384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    first = false;
25394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
25410b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
2542ad017fa7a4df7389d245d02a49b3c79ed70bedb9Bill Wendling    // Attributes here appertain to the array type. C++11 [expr.new]p5.
25436ee326af4e77e6f05973486097884d7431f2108dRichard Smith    ParsedAttributes Attrs(AttrFactory);
25444e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    MaybeParseCXX11Attributes(Attrs);
25456ee326af4e77e6f05973486097884d7431f2108dRichard Smith
25460b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    D.AddTypeInfo(DeclaratorChunk::getArray(0,
25477f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                            /*static=*/false, /*star=*/false,
25484a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Size.release(),
25494a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            T.getOpenLocation(),
25504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            T.getCloseLocation()),
25516ee326af4e77e6f05973486097884d7431f2108dRichard Smith                  Attrs, T.getCloseLocation());
25524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25534a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
25544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
25554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
25564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
25574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
25594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator.
25604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
25614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
25624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
25634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
25644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
25654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
25664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
25674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
2568ca0408fb49c1370430672acf2d770b7151cf71deJohn McCallbool Parser::ParseExpressionListOrTypeId(
25695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   SmallVectorImpl<Expr*> &PlacementArgs,
257059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner                                         Declarator &D) {
25714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // The '(' was already consumed.
25724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (isTypeIdInParens()) {
2573cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseSpecifierQualifierList(D.getMutableDeclSpec());
2574ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetSourceRange(D.getDeclSpec().getSourceRange());
2575cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseDeclarator(D);
2576eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    return D.isInvalidType();
25774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
25784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // It's not a type, it has to be an expression list.
25804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Discard the comma locations - ActOnCXXNew has enough parameters.
25814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  CommaLocsTy CommaLocs;
25824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return ParseExpressionList(PlacementArgs, CommaLocs);
25834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
25844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
25854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
25864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new.
25874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
258859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional
258959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
259059232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location.  Otherwise, "Start" is the location of the
259159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token.
259259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner///
25934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        delete-expression:
25944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' cast-expression
25954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' '[' ']' cast-expression
259660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
259759232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
259859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
259959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken(); // Consume 'delete'
26004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
26014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Array delete?
26024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ArrayDelete = false;
26036ee326af4e77e6f05973486097884d7431f2108dRichard Smith  if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
2604950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    // C++11 [expr.delete]p1:
2605950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //   Whenever the delete keyword is followed by empty square brackets, it
2606950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //   shall be interpreted as [array delete].
2607950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //   [Footnote: A lambda expression with a lambda-introducer that consists
2608950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //              of empty square brackets can follow the delete keyword if
2609950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //              the lambda expression is enclosed in parentheses.]
2610950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2611950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    //        lambda-introducer.
26124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ArrayDelete = true;
26134a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_square);
26144a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
26154a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
26164a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
26174a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
261820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
26194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
26204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
262160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Operand(ParseCastExpression(false));
26220e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Operand.isInvalid())
26233fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    return Operand;
26244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
26259ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
26264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
262764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
26281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
262964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch(kind) {
2630b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a known unary type trait.");
263164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign;
26329ef9875bbe19dc9f73c6c95b803d9a4945168690Joao Matos  case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
263364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
263420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___has_nothrow_copy:           return UTT_HasNothrowCopy;
263564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign;
26369ef9875bbe19dc9f73c6c95b803d9a4945168690Joao Matos  case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
2637023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  case tok::kw___has_trivial_constructor:
2638023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                    return UTT_HasTrivialDefaultConstructor;
26399ef9875bbe19dc9f73c6c95b803d9a4945168690Joao Matos  case tok::kw___has_trivial_move_constructor:
26409ef9875bbe19dc9f73c6c95b803d9a4945168690Joao Matos                                    return UTT_HasTrivialMoveConstructor;
264120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___has_trivial_copy:           return UTT_HasTrivialCopy;
264264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor;
264364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor;
264464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_abstract:             return UTT_IsAbstract;
264520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_arithmetic:              return UTT_IsArithmetic;
264620c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_array:                   return UTT_IsArray;
264764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_class:                return UTT_IsClass;
264820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_complete_type:           return UTT_IsCompleteType;
264920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_compound:                return UTT_IsCompound;
265020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_const:                   return UTT_IsConst;
265164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_empty:                return UTT_IsEmpty;
265264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_enum:                 return UTT_IsEnum;
26535e9392ba18f5925e26cc5714d1412eda0d219826Douglas Gregor  case tok::kw___is_final:                 return UTT_IsFinal;
265420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_floating_point:          return UTT_IsFloatingPoint;
265520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_function:                return UTT_IsFunction;
265620c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_fundamental:             return UTT_IsFundamental;
265720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_integral:                return UTT_IsIntegral;
2658ea30e2f8667668173cf7433c3c80cf603bd922a4John McCall  case tok::kw___is_interface_class:         return UTT_IsInterfaceClass;
265920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_lvalue_reference:        return UTT_IsLvalueReference;
266020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
266120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_object_pointer:   return UTT_IsMemberObjectPointer;
266220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_pointer:          return UTT_IsMemberPointer;
266320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_object:                  return UTT_IsObject;
26644e61ddd644e9c6293697a966d98d7c1905cf63a8Chandler Carruth  case tok::kw___is_literal:              return UTT_IsLiteral;
26653840281126e7d10552c55f6fd8b1ec9483898906Chandler Carruth  case tok::kw___is_literal_type:         return UTT_IsLiteral;
266664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_pod:                  return UTT_IsPOD;
266720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_pointer:                 return UTT_IsPointer;
266864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_polymorphic:          return UTT_IsPolymorphic;
266920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_reference:               return UTT_IsReference;
267020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_rvalue_reference:        return UTT_IsRvalueReference;
267120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_scalar:                  return UTT_IsScalar;
26727121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer  case tok::kw___is_sealed:                  return UTT_IsSealed;
267320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_signed:                  return UTT_IsSigned;
267420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_standard_layout:         return UTT_IsStandardLayout;
267520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_trivial:                 return UTT_IsTrivial;
2676feb375d31b7e9108b04a9f55b721d5e0c793a558Sean Hunt  case tok::kw___is_trivially_copyable:      return UTT_IsTriviallyCopyable;
267764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_union:                return UTT_IsUnion;
267820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_unsigned:                return UTT_IsUnsigned;
267920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_void:                    return UTT_IsVoid;
268020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_volatile:                return UTT_IsVolatile;
268164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
26826ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
26836ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
26846ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichetstatic BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
26856ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  switch(kind) {
268638c2b730a8553fa1cf369d0c5567f8b5d0a3dda8Francois Pichet  default: llvm_unreachable("Not a known binary type trait");
2687f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case tok::kw___is_base_of:                 return BTT_IsBaseOf;
268820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_convertible:             return BTT_IsConvertible;
268920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_same:                    return BTT_IsSame;
2690f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
26919f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor  case tok::kw___is_convertible_to:          return BTT_IsConvertibleTo;
269225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  case tok::kw___is_trivially_assignable:    return BTT_IsTriviallyAssignable;
26936ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
269464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
269564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
26964ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregorstatic TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
26974ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  switch (kind) {
26984ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  default: llvm_unreachable("Not a known type trait");
26994ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  case tok::kw___is_trivially_constructible:
27004ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return TT_IsTriviallyConstructible;
27014ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  }
27024ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor}
27034ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
270421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegleystatic ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
270521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch(kind) {
270621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  default: llvm_unreachable("Not a known binary type trait");
270721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case tok::kw___array_rank:                 return ATT_ArrayRank;
270821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case tok::kw___array_extent:               return ATT_ArrayExtent;
270921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
271021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
271121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
2712552622067dc45013d240f73952fece703f5e63bdJohn Wiegleystatic ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2713552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  switch(kind) {
2714b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a known unary expression trait.");
2715552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case tok::kw___is_lvalue_expr:             return ET_IsLValueExpr;
2716552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case tok::kw___is_rvalue_expr:             return ET_IsRValueExpr;
2717552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  }
2718552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
2719552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
272064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
272164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits
272264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates.
272364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
272464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///       primary-expression:
272564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU]             unary-type-trait '(' type-id ')'
272664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
272760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseUnaryTypeTrait() {
272864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
272964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation Loc = ConsumeToken();
273064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
27314a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
27324a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
273364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return ExprError();
273464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
273564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
273664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // there will be cryptic errors about mismatched parentheses and missing
273764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // specifiers.
2738809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
273964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
27404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
274164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
2742809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2743809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2744809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
27454a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
274664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
2747f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
27486ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
27496ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// pseudo-functions that allow implementation of the TR1/C++0x type traits
27506ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// templates.
27516ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///
27526ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///       primary-expression:
27536ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// [GNU]             binary-type-trait '(' type-id ',' type-id ')'
27546ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///
27556ad6f2848d7652ab2991286eb48be440d3493b28Francois PichetExprResult Parser::ParseBinaryTypeTrait() {
27566ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
27576ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  SourceLocation Loc = ConsumeToken();
27586ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
27594a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
27604a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
27616ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
27626ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
27636ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeResult LhsTy = ParseTypeName();
27646ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (LhsTy.isInvalid()) {
27658fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_paren, StopAtSemi);
27666ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
27676ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
27686ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
27696ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
27708fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_paren, StopAtSemi);
27716ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
27726ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
27736ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
27746ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeResult RhsTy = ParseTypeName();
27756ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (RhsTy.isInvalid()) {
27768fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_paren, StopAtSemi);
27776ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
27786ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
27796ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
27804a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
27816ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
27824a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
27834a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      T.getCloseLocation());
27846ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
27856ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
27864ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor/// \brief Parse the built-in type-trait pseudo-functions that allow
27874ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor/// implementation of the TR1/C++11 type traits templates.
27884ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///
27894ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///       primary-expression:
27904ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///          type-trait '(' type-id-seq ')'
27914ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///
27924ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///       type-id-seq:
27934ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///          type-id ...[opt] type-id-seq[opt]
27944ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor///
27954ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas GregorExprResult Parser::ParseTypeTrait() {
27964ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
27974ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  SourceLocation Loc = ConsumeToken();
27984ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
27994ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  BalancedDelimiterTracker Parens(*this, tok::l_paren);
28004ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  if (Parens.expectAndConsume(diag::err_expected_lparen))
28014ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return ExprError();
28024ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
2803cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<ParsedType, 2> Args;
28044ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  do {
28054ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // Parse the next type.
28064ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    TypeResult Ty = ParseTypeName();
28074ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Ty.isInvalid()) {
28084ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      Parens.skipToEnd();
28094ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      return ExprError();
28104ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
28114ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
28124ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // Parse the ellipsis, if present.
28134ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Tok.is(tok::ellipsis)) {
28144ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
28154ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      if (Ty.isInvalid()) {
28164ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        Parens.skipToEnd();
28174ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        return ExprError();
28184ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      }
28194ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
28204ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
28214ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // Add this type to the list of arguments.
28224ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    Args.push_back(Ty.get());
28234ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
28244ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Tok.is(tok::comma)) {
28254ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      ConsumeToken();
28264ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      continue;
28274ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
28284ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
28294ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    break;
28304ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  } while (true);
28314ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
28324ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  if (Parens.consumeClose())
28334ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return ExprError();
28344ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
28354ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
28364ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor}
28374ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
283821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// ParseArrayTypeTrait - Parse the built-in array type-trait
283921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// pseudo-functions.
284021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///
284121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///       primary-expression:
284221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// [Embarcadero]     '__array_rank' '(' type-id ')'
284321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// [Embarcadero]     '__array_extent' '(' type-id ',' expression ')'
284421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///
284521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John WiegleyExprResult Parser::ParseArrayTypeTrait() {
284621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
284721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  SourceLocation Loc = ConsumeToken();
284821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
28494a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
28504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
285121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return ExprError();
285221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
285321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  TypeResult Ty = ParseTypeName();
285421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  if (Ty.isInvalid()) {
28558fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::comma, StopAtSemi);
28568fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_paren, StopAtSemi);
285721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return ExprError();
285821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
285921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
286021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch (ATT) {
286121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayRank: {
28624a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
28634a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
28644a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
286521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
286621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayExtent: {
286721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
28688fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev      SkipUntil(tok::r_paren, StopAtSemi);
286921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      return ExprError();
287021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    }
287121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
287221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    ExprResult DimExpr = ParseExpression();
28734a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
287421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
28754a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
28764a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
287721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
287821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
28793026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid ArrayTypeTrait!");
288021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
288121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
2882552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// ParseExpressionTrait - Parse built-in expression-trait
2883552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// pseudo-functions like __is_lvalue_expr( xxx ).
2884552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///
2885552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///       primary-expression:
2886552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// [Embarcadero]     expression-trait '(' expression ')'
2887552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///
2888552622067dc45013d240f73952fece703f5e63bdJohn WiegleyExprResult Parser::ParseExpressionTrait() {
2889552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2890552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  SourceLocation Loc = ConsumeToken();
2891552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
28924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
28934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
2894552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    return ExprError();
2895552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2896552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  ExprResult Expr = ParseExpression();
2897552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
28984a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
2899552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
29004a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
29014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      T.getCloseLocation());
2902552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
2903552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2904552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2905f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2906f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2907f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens.
290860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2909f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
2910b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                         ParsedType &CastTy,
29114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                         BalancedDelimiterTracker &Tracker) {
29124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
2913f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2914f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(isTypeIdInParens() && "Not a type-id!");
2915f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
291660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(true);
2917b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  CastTy = ParsedType();
2918f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2919f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // We need to disambiguate a very ugly part of the C++ syntax:
2920f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2921f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())x;  - type-id
2922f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())*x; - type-id
2923f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())/x; - expression
2924f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T());   - expression
2925f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2926f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The bad news is that we cannot use the specialized tentative parser, since
2927f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // it can only verify that the thing inside the parens can be parsed as
2928f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // type-id, it is not useful for determining the context past the parens.
2929f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2930f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The good news is that the parser can disambiguate this part without
2931a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis  // making any unnecessary Action calls.
2932f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  //
2933f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // It uses a scheme similar to parsing inline methods. The parenthesized
2934f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // tokens are cached, the context that follows is determined (possibly by
2935f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parsing a cast-expression), and then we re-introduce the cached tokens
2936f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // into the token stream and parse them appropriately.
2937f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
29381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParenParseOption ParseAs;
2939f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  CachedTokens Toks;
2940f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2941f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Store the tokens of the parentheses. We will parse them after we determine
2942f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // the context that follows them.
294314b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis  if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
2944f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We didn't find the ')' we expected.
29454a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Tracker.consumeClose();
2946f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
2947f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
2948f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2949f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::l_brace)) {
2950f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = CompoundLiteral;
2951f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  } else {
2952f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    bool NotCastExpr;
2953b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2954b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2955b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      NotCastExpr = true;
2956b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    } else {
2957b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // Try parsing the cast-expression that may follow.
2958b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // If it is not a cast-expression, NotCastExpr will be true and no token
2959b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // will be consumed.
2960b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      Result = ParseCastExpression(false/*isUnaryExpression*/,
2961b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman                                   false/*isAddressofOperand*/,
2962b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   NotCastExpr,
29630a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis                                   // type-id has priority.
2964cd78e612d6fa8e214e6a6bb0e739a0c3e419df91Kaelyn Uhrain                                   IsTypeCast);
2965b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    }
2966f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2967f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // If we parsed a cast-expression, it's really a type-id, otherwise it's
2968f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // an expression.
2969f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
2970f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
2971f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
29721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The current token should go after the cached tokens.
2973f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Toks.push_back(Tok);
2974f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Re-enter the stored parenthesized tokens into the token stream, so we may
2975f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parse them now.
2976f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  PP.EnterTokenStream(Toks.data(), Toks.size(),
2977f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2978f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Drop the current token and bring the first cached one. It's the same token
2979f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // as when we entered this function.
2980f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  ConsumeAnyToken();
2981f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2982f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (ParseAs >= CompoundLiteral) {
29830a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    // Parse the type declarator.
29840a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    DeclSpec DS(AttrFactory);
29850a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    ParseSpecifierQualifierList(DS);
29860a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
29870a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    ParseDeclarator(DeclaratorInfo);
2988f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2989f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Match the ')'.
29904a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Tracker.consumeClose();
2991f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2992f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (ParseAs == CompoundLiteral) {
2993f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      ExprType = CompoundLiteral;
29940a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis      TypeResult Ty = ParseTypeName();
29954a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor       return ParseCompoundLiteralExpression(Ty.get(),
29964a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Tracker.getOpenLocation(),
29974a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Tracker.getCloseLocation());
2998f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    }
29991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3000f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
3001f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    assert(ParseAs == CastExpr);
3002f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
30030a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    if (DeclaratorInfo.isInvalidType())
3004f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ExprError();
3005f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
3006f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Result is what ParseCastExpression returned earlier.
3007f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    if (!Result.isInvalid())
30084a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
30094a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    DeclaratorInfo, CastTy,
30104a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    Tracker.getCloseLocation(), Result.take());
30113fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    return Result;
3012f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
30131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3014f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Not a compound literal, and not followed by a cast-expression.
3015f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  assert(ParseAs == SimpleExpr);
3016f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
3017f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  ExprType = SimpleExpr;
3018f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Result = ParseExpression();
3019f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (!Result.isInvalid() && Tok.is(tok::r_paren))
30204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
30214a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    Tok.getLocation(), Result.take());
3022f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
3023f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // Match the ')'.
3024f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Result.isInvalid()) {
30258fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev    SkipUntil(tok::r_paren, StopAtSemi);
3026f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
3027f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
30281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30294a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Tracker.consumeClose();
30303fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
3031f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis}
3032