ParseExprCXX.cpp revision 2aed8b88613863f3c439cdfb205bdf8b608fb205
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements the Expression parsing implementation for C++.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h"
16bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor#include "RAIIObjectsForParser.h"
17dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman#include "clang/Basic/PrettyStackTrace.h"
1819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
19ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor#include "clang/Sema/Scope.h"
2019510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h"
213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor#include "llvm/Support/ErrorHandling.h"
223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic int SelectDigraphErrorMessage(tok::TokenKind Kind) {
26ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  switch (Kind) {
27ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_template:         return 0;
28ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_const_cast:       return 1;
29ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_dynamic_cast:     return 2;
30ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_reinterpret_cast: return 3;
31ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    case tok::kw_static_cast:      return 4;
32ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    default:
33b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unknown type for digraph error message.");
34ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  }
35ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
36ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
37ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith// Are the two tokens adjacent in the same source file?
38ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
39ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceManager &SM = PP.getSourceManager();
40ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
41a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
42ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  return FirstEnd == SM.getSpellingLoc(Second.getLocation());
43ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
44ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
45ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith// Suggest fixit for "<::" after a cast.
46ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smithstatic void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
47ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith                       Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
48ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Pull '<:' and ':' off token stream.
49ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (!AtDigraph)
50ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    PP.Lex(DigraphToken);
51ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  PP.Lex(ColonToken);
52ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
53ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  SourceRange Range;
54ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Range.setBegin(DigraphToken.getLocation());
55ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Range.setEnd(ColonToken.getLocation());
56ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
57ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      << SelectDigraphErrorMessage(Kind)
58ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      << FixItHint::CreateReplacement(Range, "< ::");
59ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
60ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Update token information to reflect their change in token type.
61ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setKind(tok::coloncolon);
62a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
63ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  ColonToken.setLength(2);
64ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  DigraphToken.setKind(tok::less);
65ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  DigraphToken.setLength(1);
66ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
67ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Push new tokens back to token stream.
68ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  PP.EnterToken(ColonToken);
69ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (!AtDigraph)
70ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    PP.EnterToken(DigraphToken);
71ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith}
72ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
73950be71c745409e373ae8a834490f9026c8ac222Richard Trieu// Check for '<::' which should be '< ::' instead of '[:' when following
74950be71c745409e373ae8a834490f9026c8ac222Richard Trieu// a template name.
75950be71c745409e373ae8a834490f9026c8ac222Richard Trieuvoid Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
76950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                                        bool EnteringContext,
77950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                                        IdentifierInfo &II, CXXScopeSpec &SS) {
78c11030ea936f6952deb5a1423ce1648173cd417eRichard Trieu  if (!Next.is(tok::l_square) || Next.getLength() != 2)
79950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
80950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
81950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  Token SecondToken = GetLookAheadToken(2);
82950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  if (!SecondToken.is(tok::colon) || !AreTokensAdjacent(PP, Next, SecondToken))
83950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
84950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
85950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  TemplateTy Template;
86950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  UnqualifiedId TemplateName;
87950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  TemplateName.setIdentifier(&II, Tok.getLocation());
88950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  bool MemberOfUnknownSpecialization;
89950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
90950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                              TemplateName, ObjectType, EnteringContext,
91950be71c745409e373ae8a834490f9026c8ac222Richard Trieu                              Template, MemberOfUnknownSpecialization))
92950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    return;
93950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
94950be71c745409e373ae8a834490f9026c8ac222Richard Trieu  FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
95950be71c745409e373ae8a834490f9026c8ac222Richard Trieu             /*AtDigraph*/false);
96950be71c745409e373ae8a834490f9026c8ac222Richard Trieu}
97950be71c745409e373ae8a834490f9026c8ac222Richard Trieu
981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Parse global scope or nested-name-specifier if present.
992dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1002dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// may be preceded by '::'). Note that this routine will not parse ::new or
1022dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// ::delete; it will just leave them in the token stream.
103eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
104eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'[opt] nested-name-specifier
105eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       '::'
106eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
107eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       nested-name-specifier:
108eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         type-name '::'
109eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         namespace-name '::'
110eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         nested-name-specifier identifier '::'
1112dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///         nested-name-specifier 'template'[opt] simple-template-id '::'
1122dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1132dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param SS the scope specifier that will be set to the parsed
1152dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// nested-name-specifier (or empty)
1162dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
1171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ObjectType if this nested-name-specifier is being parsed following
1182dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the "." or "->" of a member access expression, this parameter provides the
1192dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// type of the object whose members are being accessed.
120eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
1212dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// \param EnteringContext whether we will be entering into the context of
1222dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor/// the nested-name-specifier after parsing it.
1232dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor///
124d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param MayBePseudoDestructor When non-NULL, points to a flag that
125d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// indicates whether this nested-name-specifier may be part of a
126d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// pseudo-destructor name. In this case, the flag will be set false
127d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we don't actually end up parsing a destructor name. Moreorover,
128d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// if we do end up determining that we are parsing a destructor name,
129d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// the last component of the nested-name-specifier is not parsed as
130d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// part of the scope specifier.
131d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
132b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor/// member access expression, e.g., the \p T:: in \p p->T::m.
133b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor///
1349ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// \returns true if there was an error parsing a scope specifier
135495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
136b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType ObjectType,
137b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            bool EnteringContext,
1384147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                            bool *MayBePseudoDestructor,
1394147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                            bool IsTypename) {
1404bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  assert(getLang().CPlusPlus &&
1417452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Call sites of this function should be guarded by checking for C++");
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
143eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::annot_cxxscope)) {
144c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor    Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
145c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 Tok.getAnnotationRange(),
146c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor                                                 SS);
147eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    ConsumeToken();
1489ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
149eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
150e607e808c2b90724a2a6fd841e850f07de1f5b30Chris Lattner
15139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  bool HasScopeSpecifier = false;
15239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
1535b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner  if (Tok.is(tok::coloncolon)) {
1545b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    // ::new and ::delete aren't nested-name-specifiers.
1555b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    tok::TokenKind NextKind = NextToken().getKind();
1565b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner    if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
1575b4547318bb179fc76f984f0eeaaf615927e795cChris Lattner      return false;
1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15955a7cefc846765ac7d142a63f773747a20518d71Chris Lattner    // '::' - Global scope qualifier.
1602e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor    if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
1612e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      return true;
1622e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
16339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    HasScopeSpecifier = true;
164eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  }
165eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
166d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  bool CheckForDestructor = false;
167d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (MayBePseudoDestructor && *MayBePseudoDestructor) {
168d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CheckForDestructor = true;
169d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = false;
170d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
171d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
17242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
17342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    DeclSpec DS(AttrFactory);
17442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation DeclLoc = Tok.getLocation();
17542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation EndLoc  = ParseDecltypeSpecifier(DS);
17642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    if (Tok.isNot(tok::coloncolon)) {
17742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
17842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      return false;
17942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    }
18042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
18142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    SourceLocation CCLoc = ConsumeToken();
18242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
18342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie      SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
18442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
18542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie    HasScopeSpecifier = true;
18642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie  }
18742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie
18839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  while (true) {
1892dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    if (HasScopeSpecifier) {
1902dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // C++ [basic.lookup.classref]p5:
1912dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   If the qualified-id has the form
1923b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
1932dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //       ::class-name-or-namespace-name::...
1943b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor      //
1952dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   the class-name-or-namespace-name is looked up in global scope as a
1962dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //   class-name or namespace-name.
1972dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      //
1982dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // To implement this, we clear out the object type as soon as we've
1992dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // seen a leading '::' or part of a nested-name-specifier.
200b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      ObjectType = ParsedType();
20181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
20281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      if (Tok.is(tok::code_completion)) {
20381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // Code completion for a nested-name-specifier, where the code
20481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor        // code completion token follows the '::'.
20523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
206b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // Include code completion token into the range of the scope otherwise
207b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // when we try to annotate the scope tokens the dangling code completion
208b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // token will cause assertion in
209b6b2b180c6857af75c6683f7107a7c8994f074edArgyrios Kyrtzidis        // Preprocessor::AnnotatePreviousCachedTokens.
2107d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        SS.setEndLoc(Tok.getLocation());
2117d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
2127d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return true;
21381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor      }
2142dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
2151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier:
21777cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    //   nested-name-specifier 'template'[opt] simple-template-id '::'
21877cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner
21977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // Parse the optional 'template' keyword, then make sure we have
22077cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    // 'identifier <' after it.
22177cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    if (Tok.is(tok::kw_template)) {
2222dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      // If we don't have a scope specifier or an object type, this isn't a
223eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // nested-name-specifier, since they aren't allowed to start with
224eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman      // 'template'.
2252dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor      if (!HasScopeSpecifier && !ObjectType)
226eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman        break;
227eab975dce83fcf6a7fa8fc9379944d4b1aaf1a00Eli Friedman
2287bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TentativeParsingAction TPA(*this);
22977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      SourceLocation TemplateKWLoc = ConsumeToken();
230ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
231ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      UnqualifiedId TemplateName;
232ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::identifier)) {
233ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
2347bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
235ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
236ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else if (Tok.is(tok::kw_operator)) {
237ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
2387bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor                                       TemplateName)) {
2397bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
240ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
2417bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        }
242ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
243e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt        if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
244e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt            TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
245ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          Diag(TemplateName.getSourceRange().getBegin(),
246ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor               diag::err_id_after_template_in_nested_name_spec)
247ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor            << TemplateName.getSourceRange();
2487bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor          TPA.Commit();
249ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          break;
250ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        }
251ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
2527bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
25377cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner        break;
25477cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      }
2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2567bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // If the next token is not '<', we have a qualified-id that refers
2577bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // to a template name, such as T::template apply, but is not a
2587bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // template-id.
2597bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      if (Tok.isNot(tok::less)) {
2607bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        TPA.Revert();
2617bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor        break;
2627bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      }
2637bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor
2647bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      // Commit to parsing the template-id.
2657bb87fca7d22a8a194d04188746b90f46512975fDouglas Gregor      TPA.Commit();
266d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      TemplateTy Template;
267e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      if (TemplateNameKind TNK
268e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara          = Actions.ActOnDependentTemplateName(getCurScope(),
269e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               SS, TemplateKWLoc, TemplateName,
270e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               ObjectType, EnteringContext,
271e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               Template)) {
272e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
273e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName, false))
274d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          return true;
275d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      } else
2769ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
2771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27877cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner      continue;
27977cf72a95baa18e82737bb45131e9658a00fee16Chris Lattner    }
2801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
2821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We have
28339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
28439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //   simple-template-id '::'
28539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      //
28639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // So we need to check whether the simple-template-id is of the
287c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // right kind (it should name a type or be dependent), and then
288c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor      // convert it into a type within the nested-name-specifier.
28925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
290d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
291d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
2929ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
293d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
294d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
2956cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      // Consume the template-id token.
2966cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      ConsumeToken();
2976cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
2986cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
2996cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      SourceLocation CCLoc = ConsumeToken();
3001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3016796fc1adcaf57c38d072a238b016b2834afbe0dDavid Blaikie      HasScopeSpecifier = true;
3026cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
3036cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      ASTTemplateArgsPtr TemplateArgsPtr(Actions,
3046cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                         TemplateId->getTemplateArgs(),
3056cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                         TemplateId->NumArgs);
3066cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor
3076cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
308e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                              SS,
309e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                              TemplateId->TemplateKWLoc,
3106cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->Template,
3116cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->TemplateNameLoc,
3126cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->LAngleLoc,
3136cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateArgsPtr,
3146cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              TemplateId->RAngleLoc,
3156cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              CCLoc,
3166cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                              EnteringContext)) {
3176cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        SourceLocation StartLoc
3186cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor          = SS.getBeginLoc().isValid()? SS.getBeginLoc()
3196cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor                                      : TemplateId->TemplateNameLoc;
3206cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor        SS.SetInvalid(SourceRange(StartLoc, CCLoc));
32167b9e831943300ce54e564e601971828ce4def15Chris Lattner      }
322eccce7e246a17e12a2afd6eabb9ac7c8d582db4eArgyrios Kyrtzidis
3236cd9d4aa13c2145c8b4398453974515b734bfe42Douglas Gregor      continue;
32439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
32539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
3265c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
3275c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // The rest of the nested-name-specifier possibilities start with
3285c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // tok::identifier.
3295c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Tok.isNot(tok::identifier))
3305c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      break;
3315c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
3325c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    IdentifierInfo &II = *Tok.getIdentifierInfo();
3335c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
3345c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
3355c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '::'
3365c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   namespace-name '::'
3375c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   nested-name-specifier identifier '::'
3385c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    Token Next = NextToken();
33946646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
34046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // If we get foo:bar, this is almost certainly a typo for foo::bar.  Recover
34146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    // and emit a fixit hint for it.
342b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor    if (Next.is(tok::colon) && !ColonIsSacred) {
3432e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
3442e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Tok.getLocation(),
3452e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                            Next.getLocation(), ObjectType,
346b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor                                            EnteringContext) &&
347b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // If the token after the colon isn't an identifier, it's still an
348b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // error, but they probably meant something else strange so don't
349b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          // recover like this.
350b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor          PP.LookAhead(1).is(tok::identifier)) {
351b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
352849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateReplacement(Next.getLocation(), "::");
353b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor
354b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        // Recover as if the user wrote '::'.
355b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor        Next.setKind(tok::coloncolon);
356b10cd04880672103660e5844e51ee91af7361a20Douglas Gregor      }
35746646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner    }
35846646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner
3595c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::coloncolon)) {
36077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
36123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
36277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                                                II, ObjectType)) {
363d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor        *MayBePseudoDestructor = true;
3649ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return false;
365d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor      }
366d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
3675c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // We have an identifier followed by a '::'. Lookup this name
3685c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      // as the name in a nested-name-specifier.
3695c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation IdLoc = ConsumeToken();
37046646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner      assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
37146646491834cd8faabb22482dfe93b24ce28a6c1Chris Lattner             "NextToken() not working properly!");
3725c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      SourceLocation CCLoc = ConsumeToken();
3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3742e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      HasScopeSpecifier = true;
3752e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
3762e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor                                              ObjectType, EnteringContext, SS))
3772e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor        SS.SetInvalid(SourceRange(IdLoc, CCLoc));
3782e4c34ac53d08633b9473df921db4c7e4c9cd577Douglas Gregor
3795c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      continue;
3805c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
3811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
382950be71c745409e373ae8a834490f9026c8ac222Richard Trieu    CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
383ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
3845c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    // nested-name-specifier:
3855c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    //   type-name '<'
3865c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    if (Next.is(tok::less)) {
3875c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      TemplateTy Template;
388014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
389014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(&II, Tok.getLocation());
3901fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
39123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
3927c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                              /*hasTemplateKeyword=*/false,
393014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor                                                        TemplateName,
3942dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor                                                        ObjectType,
395495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor                                                        EnteringContext,
3961fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                                        Template,
3971fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                              MemberOfUnknownSpecialization)) {
3986796fc1adcaf57c38d072a238b016b2834afbe0dDavid Blaikie        // We have found a template name, so annotate this token
3995c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // with a template-id annotation. We do not permit the
4005c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // template-id to be translated into a type annotation,
4015c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // because some clients (e.g., the parsing of class template
4025c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // specializations) still want to see the original template-id
4035c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        // token.
404ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
405e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
406e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName, false))
4079ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
4085c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner        continue;
409d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      }
410d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
411d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor      if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
4124147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet          (IsTypename || IsTemplateArgumentList(1))) {
413d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // We have something like t::getAs<T>, where getAs is a
414d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // member of an unknown specialization. However, this will only
415d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
416d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
4174147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        unsigned DiagID = diag::err_missing_dependent_template_keyword;
41862ec1f2fd7368542bb926c04797fb07023547694Francois Pichet        if (getLang().MicrosoftExt)
419cf320c6388c90f1938c264e87d77a0e43946e2c3Francois Pichet          DiagID = diag::warn_missing_dependent_template_keyword;
4204147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet
4214147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet        Diag(Tok.getLocation(), DiagID)
422d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << II.getName()
423d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
424d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor
425d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TemplateNameKind TNK
42623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor              = Actions.ActOnDependentTemplateName(getCurScope(),
427e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                   SS, SourceLocation(),
428d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   TemplateName, ObjectType,
429d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                                   EnteringContext, Template)) {
430d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          // Consume the identifier.
431d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor          ConsumeToken();
432e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara          if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
433e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      TemplateName, false))
434e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara            return true;
435d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        }
436d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        else
437d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor          return true;
438d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor
439d5ab9b0a0ae24f7d0f49f6f10fd1b247e64b3306Douglas Gregor        continue;
4405c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner      }
4415c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner    }
4425c7f786e3269ee2b512ecf13ccf556c47be83a76Chris Lattner
44339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // We don't have any tokens that form the beginning of a
44439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // nested-name-specifier, so we're done.
44539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    break;
44639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
448d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Even if we didn't see any pieces of a nested-name-specifier, we
449d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // still check whether there is a tilde in this position, which
450d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // indicates a potential pseudo-destructor.
451d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (CheckForDestructor && Tok.is(tok::tilde))
452d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    *MayBePseudoDestructor = true;
453d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
4549ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
455eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
456eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
457eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// ParseCXXIdExpression - Handle id-expression.
458eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
459eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       id-expression:
460eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         unqualified-id
461eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         qualified-id
462eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
463eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
464eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
465eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' identifier
466eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' operator-function-id
467edce4dd44732dfad69f28822dddcf2b8e92b4483Douglas Gregor///         '::' template-id
468eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
469eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// NOTE: The standard specifies that, for qualified-id, the parser does not
470eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// expect:
471eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
472eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' conversion-function-id
473eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   '::' '~' class-name
474eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
475eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This may cause a slight inconsistency on diagnostics:
476eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
477eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// class C {};
478eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// namespace A {}
479eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// void f() {
480eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: A :: ~ C(); // Some Sema error about using destructor with a
481eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///                  // namespace.
482eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///   :: ~ C(); // Some Parser error like 'unexpected ~'.
483eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// }
484eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
485eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// We simplify the parser a bit and make it work like:
486eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
487eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///       qualified-id:
488eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
489eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::' unqualified-id
490eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
491eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// That way Sema can handle and report similar errors for namespaces and the
492eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// global scope.
493eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///
494ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// The isAddressOfOperand parameter indicates that this id-expression is a
495ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// direct operand of the address-of operator. This is, besides member contexts,
496ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// the only place where a qualified-id naming a non-static class member may
497ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl/// appear.
498ebc07d57be9e0722b4b9c66625e1fca43dcc2ee0Sebastian Redl///
49960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
500eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  // qualified-id:
501eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
502eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //   '::' unqualified-id
503eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  //
504eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
505efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
506e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
507e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SourceLocation TemplateKWLoc;
50802a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor  UnqualifiedId Name;
509e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  if (ParseUnqualifiedId(SS,
510e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*EnteringContext=*/false,
511e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*AllowDestructorName=*/false,
512e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         /*AllowConstructorName=*/false,
513b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                         /*ObjectType=*/ ParsedType(),
514e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         TemplateKWLoc,
51502a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor                         Name))
51602a24ee67c0a91bdb0db8a651d5748595652e670Douglas Gregor    return ExprError();
517b681b61fea36618778b8030360e90e3f4641233bJohn McCall
518b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // This is only the direct operand of an & operator if it is not
519b681b61fea36618778b8030360e90e3f4641233bJohn McCall  // followed by a postfix-expression suffix.
5209c72c6088d591ace8503b842d39448c2040f3033John McCall  if (isAddressOfOperand && isPostfixExpressionSuffixStart())
5219c72c6088d591ace8503b842d39448c2040f3033John McCall    isAddressOfOperand = false;
522e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
523e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
524e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   Tok.is(tok::l_paren), isAddressOfOperand);
525eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
526eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
527ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// ParseLambdaExpression - Parse a C++0x lambda expression.
528ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
529ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-expression:
530ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         lambda-introducer lambda-declarator[opt] compound-statement
531ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
532ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-introducer:
533ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '[' lambda-capture[opt] ']'
534ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
535ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-capture:
536ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-default
537ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-list
538ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-default ',' capture-list
539ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
540ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture-default:
541ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '&'
542ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '='
543ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
544ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture-list:
545ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture
546ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         capture-list ',' capture
547ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
548ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       capture:
549ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         identifier
550ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '&' identifier
551ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         'this'
552ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
553ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///       lambda-declarator:
554ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///         '(' parameter-declaration-clause ')' attribute-specifier[opt]
555ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///           'mutable'[opt] exception-specification[opt]
556ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///           trailing-return-type[opt]
557ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
558ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::ParseLambdaExpression() {
559ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse lambda-introducer.
560ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  LambdaIntroducer Intro;
561ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
562ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
563ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (DiagID) {
564ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Diag(Tok, DiagID.getValue());
565ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SkipUntil(tok::r_square);
566dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    SkipUntil(tok::l_brace);
567dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    SkipUntil(tok::r_brace);
568dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprError();
569ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
570ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
571ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return ParseLambdaExpressionAfterIntroducer(Intro);
572ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
573ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
574ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// TryParseLambdaExpression - Use lookahead and potentially tentative
575ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// parsing to determine if we are looking at a C++0x lambda expression, and parse
576ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// it if we are.
577ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
578ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// If we are not looking at a lambda expression, returns ExprError().
579ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::TryParseLambdaExpression() {
580ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  assert(getLang().CPlusPlus0x
581ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor         && Tok.is(tok::l_square)
582ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor         && "Not at the start of a possible lambda expression.");
583ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
584ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  const Token Next = NextToken(), After = GetLookAheadToken(2);
585ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
586ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // If lookahead indicates this is a lambda...
587ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Next.is(tok::r_square) ||     // []
588ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Next.is(tok::equal) ||        // [=
589ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (Next.is(tok::amp) &&         // [&] or [&,
590ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor       (After.is(tok::r_square) ||
591ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        After.is(tok::comma))) ||
592ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (Next.is(tok::identifier) &&  // [identifier]
593ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor       After.is(tok::r_square))) {
594ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    return ParseLambdaExpression();
595ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
596ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
597dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // If lookahead indicates an ObjC message send...
598dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // [identifier identifier
599ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Next.is(tok::identifier) && After.is(tok::identifier)) {
600dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprEmpty();
601ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
602ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
603dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // Here, we're stuck: lambda introducers and Objective-C message sends are
604dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // unambiguous, but it requires arbitrary lookhead.  [a,b,c,d,e,f,g] is a
605dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send.  Instead of
606dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // writing two routines to parse a lambda introducer, just try to parse
607dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // a lambda introducer first, and fall back if that fails.
608dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  // (TryParseLambdaIntroducer never produces any diagnostic output.)
609ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  LambdaIntroducer Intro;
610ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (TryParseLambdaIntroducer(Intro))
611dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprEmpty();
612ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return ParseLambdaExpressionAfterIntroducer(Intro);
613ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
614ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
615ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// ParseLambdaExpression - Parse a lambda introducer.
616ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
617ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// Returns a DiagnosticID if it hit something unexpected.
61881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregorllvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro){
619ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  typedef llvm::Optional<unsigned> DiagResult;
620ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
621ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
6224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_square);
6234a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
6244a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
6254a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Intro.Range.setBegin(T.getOpenLocation());
626ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
627ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  bool first = true;
628ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
629ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse capture-default.
630ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Tok.is(tok::amp) &&
631ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
632ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Intro.Default = LCD_ByRef;
6333ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor    Intro.DefaultLoc = ConsumeToken();
634ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    first = false;
635ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  } else if (Tok.is(tok::equal)) {
636ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Intro.Default = LCD_ByCopy;
6373ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor    Intro.DefaultLoc = ConsumeToken();
638ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    first = false;
639ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
640ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
641ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  while (Tok.isNot(tok::r_square)) {
642ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (!first) {
64381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      if (Tok.isNot(tok::comma)) {
64481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        if (Tok.is(tok::code_completion)) {
64581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
64681f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                               /*AfterAmpersand=*/false);
64781f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          ConsumeCodeCompletionToken();
64881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          break;
64981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        }
65081f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
651ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_expected_comma_or_rsquare);
65281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      }
653ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      ConsumeToken();
654ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
655ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
65681f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    if (Tok.is(tok::code_completion)) {
65781f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      // If we're in Objective-C++ and we have a bare '[', then this is more
65881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      // likely to be a message receiver.
65981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      if (getLang().ObjC1 && first)
66081f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        Actions.CodeCompleteObjCMessageReceiver(getCurScope());
66181f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      else
66281f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
66381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                             /*AfterAmpersand=*/false);
66481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      ConsumeCodeCompletionToken();
66581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor      break;
66681f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    }
667ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
66881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor    first = false;
66981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
670ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse capture.
671ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    LambdaCaptureKind Kind = LCK_ByCopy;
672ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation Loc;
673ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    IdentifierInfo* Id = 0;
674a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor    SourceLocation EllipsisLoc;
675a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor
676ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::kw_this)) {
677ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Kind = LCK_This;
678ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      Loc = ConsumeToken();
679ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    } else {
680ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Tok.is(tok::amp)) {
681ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Kind = LCK_ByRef;
682ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        ConsumeToken();
68381f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor
68481f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        if (Tok.is(tok::code_completion)) {
68581f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
68681f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor                                               /*AfterAmpersand=*/true);
68781f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          ConsumeCodeCompletionToken();
68881f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor          break;
68981f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor        }
690ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      }
691ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
692ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Tok.is(tok::identifier)) {
693ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Id = Tok.getIdentifierInfo();
694ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        Loc = ConsumeToken();
695a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor
696a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor        if (Tok.is(tok::ellipsis))
697a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor          EllipsisLoc = ConsumeToken();
698ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      } else if (Tok.is(tok::kw_this)) {
699ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // FIXME: If we want to suggest a fixit here, will need to return more
700ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
701ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        // Clear()ed to prevent emission in case of tentative parsing?
702ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_this_captured_by_reference);
703ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      } else {
704ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        return DiagResult(diag::err_expected_capture);
705ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      }
706ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
707ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
708a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor    Intro.addCapture(Kind, Loc, Id, EllipsisLoc);
709ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
710ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
7114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
7124a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Intro.Range.setEnd(T.getCloseLocation());
713ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
714ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return DiagResult();
715ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
716ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
71781f3bff7c202d688c9298bc049fdb5b6f77057b1Douglas Gregor/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
718ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor///
719ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// Returns true if it hit something unexpected.
720ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregorbool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
721ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  TentativeParsingAction PA(*this);
722ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
723ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
724ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
725ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (DiagID) {
726ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    PA.Revert();
727ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    return true;
728ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
729ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
730ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  PA.Commit();
731ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  return false;
732ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
733ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
734ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
735ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor/// expression.
736ae7902c4293d9de8b9591759513f0d075f45022aDouglas GregorExprResult Parser::ParseLambdaExpressionAfterIntroducer(
737ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                     LambdaIntroducer &Intro) {
738dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
739dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
740dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
741dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
742dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman                                "lambda expression parsing");
743dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
744ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse lambda-declarator[opt].
745ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  DeclSpec DS(AttrFactory);
746f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman  Declarator D(DS, Declarator::LambdaExprContext);
747ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
748ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  if (Tok.is(tok::l_paren)) {
749ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ParseScope PrototypeScope(this,
750ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                              Scope::FunctionPrototypeScope |
751ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                              Scope::DeclScope);
752ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
7534a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    SourceLocation DeclLoc, DeclEndLoc;
7544a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
7554a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
7564a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    DeclLoc = T.getOpenLocation();
757ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
758ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse parameter-declaration-clause.
759ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ParsedAttributes Attr(AttrFactory);
760ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
761ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation EllipsisLoc;
762ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
763ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.isNot(tok::r_paren))
764ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
765ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
7664a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
7674a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    DeclEndLoc = T.getCloseLocation();
768ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
769ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse 'mutable'[opt].
770ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceLocation MutableLoc;
771ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::kw_mutable)) {
772ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      MutableLoc = ConsumeToken();
773ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      DeclEndLoc = MutableLoc;
774ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
775ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
776ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse exception-specification[opt].
777ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ExceptionSpecificationType ESpecType = EST_None;
778ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    SourceRange ESpecRange;
779ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    llvm::SmallVector<ParsedType, 2> DynamicExceptions;
780ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
781ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ExprResult NoexceptExpr;
782ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ESpecType = MaybeParseExceptionSpecification(ESpecRange,
783ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                                 DynamicExceptions,
784ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                                 DynamicExceptionRanges,
785ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                                 NoexceptExpr);
786ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
787ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (ESpecType != EST_None)
788ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      DeclEndLoc = ESpecRange.getEnd();
789ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
790ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse attribute-specifier[opt].
791ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
792ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
793ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    // Parse trailing-return-type[opt].
794ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    ParsedType TrailingReturnType;
795ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    if (Tok.is(tok::arrow)) {
796ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      SourceRange Range;
797ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      TrailingReturnType = ParseTrailingReturnType(Range).get();
798ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor      if (Range.getEnd().isValid())
799ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor        DeclEndLoc = Range.getEnd();
800ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    }
801ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
802ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    PrototypeScope.Exit();
803ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
804ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
805ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           /*isVariadic=*/EllipsisLoc.isValid(),
806ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           EllipsisLoc,
807ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           ParamInfo.data(), ParamInfo.size(),
808ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DS.getTypeQualifiers(),
809ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           /*RefQualifierIsLValueRef=*/true,
810ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           /*RefQualifierLoc=*/SourceLocation(),
81143f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                                         /*ConstQualifierLoc=*/SourceLocation(),
81243f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                                      /*VolatileQualifierLoc=*/SourceLocation(),
813ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           MutableLoc,
814ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           ESpecType, ESpecRange.getBegin(),
815ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptions.data(),
816ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptionRanges.data(),
817ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DynamicExceptions.size(),
818ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           NoexceptExpr.isUsable() ?
819ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                             NoexceptExpr.get() : 0,
820ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           DeclLoc, DeclEndLoc, D,
821ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                                           TrailingReturnType),
822ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor                  Attr, DeclEndLoc);
823ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
824ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
825906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
826906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  // it.
827906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
828906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                             Scope::BreakScope | Scope::ContinueScope |
829906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                             Scope::DeclScope);
830906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman
831ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman  Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
832ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman
833ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  // Parse compound-statement.
834dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  if (!Tok.is(tok::l_brace)) {
835ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    Diag(Tok, diag::err_expected_lambda_body);
836dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
837dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman    return ExprError();
838ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
839ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
840dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  StmtResult Stmt(ParseCompoundStatementBody());
841dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman  BodyScope.Exit();
842dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
843deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  if (!Stmt.isInvalid())
844deeab90783eb28d955add1062b616c030eb2b781Eli Friedman    return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(),
845deeab90783eb28d955add1062b616c030eb2b781Eli Friedman                                   getCurScope());
846dc3b723d35067e5d13474247b94a10c869cc7e58Eli Friedman
847deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
848deeab90783eb28d955add1062b616c030eb2b781Eli Friedman  return ExprError();
849ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor}
850ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXCasts - This handles the various ways to cast expressions to another
8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// type.
8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
8545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       postfix-expression: [C++ 5.2p1]
8555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'dynamic_cast' '<' type-name '>' '(' expression ')'
8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'static_cast' '<' type-name '>' '(' expression ')'
8575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
8585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'const_cast' '<' type-name '>' '(' expression ')'
8595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
86060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXCasts() {
8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CastName = 0;     // For error messages
8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Kind) {
865eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie  default: llvm_unreachable("Unknown C++ cast!");
8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_const_cast:       CastName = "const_cast";       break;
8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_static_cast:      CastName = "static_cast";      break;
8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation OpLoc = ConsumeToken();
8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation LAngleBracketLoc = Tok.getLocation();
8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
875ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // Check for "<::" which is parsed as "[:".  If found, fix token stream,
876ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  // diagnose error, suggest fix, and recover parsing.
877ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  Token Next = NextToken();
878ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith  if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
879ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith      AreTokensAdjacent(PP, Tok, Next))
880ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith    FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
881ea698b3f6cad84f7f583282dce3e03e24fe80e98Richard Smith
8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
88320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
88531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Parse the common declaration-specifiers piece.
88631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclSpec DS(AttrFactory);
88731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
88831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
88931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Parse the abstract-declarator, if present.
89031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
89131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
89231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
8935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation RAngleBracketLoc = Tok.getLocation();
8945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8951ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
89620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
8975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8984a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  SourceLocation LParenLoc, RParenLoc;
8994a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
9005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
90221e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis    return ExprError();
9035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
90460d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = ParseExpression();
9051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90621e7ad24099965acfa801e4abdd91e3d94106428Argyrios Kyrtzidis  // Match the ')'.
9074a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
90931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
91049badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
91131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                       LAngleBracketLoc, DeclaratorInfo,
912809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                       RAngleBracketLoc,
9134a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getOpenLocation(), Result.take(),
9144a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
91620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
919c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl/// ParseCXXTypeid - This handles the C++ typeid expression.
920c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
921c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///       postfix-expression: [C++ 5.2p1]
922c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' expression ')'
923c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///         'typeid' '(' type-id ')'
924c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl///
92560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXTypeid() {
926c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
927c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
928c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  SourceLocation OpLoc = ConsumeToken();
9294a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  SourceLocation LParenLoc, RParenLoc;
9304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
931c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
932c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  // typeid expressions are always parenthesized.
9334a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
93420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
9354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  LParenLoc = T.getOpenLocation();
936c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
93760d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result;
938c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
939c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  if (isTypeIdInParens()) {
940809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult Ty = ParseTypeName();
941c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
942c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
9434a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
9444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    RParenLoc = T.getCloseLocation();
9454eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    if (Ty.isInvalid() || RParenLoc.isInvalid())
94620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
947c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
948c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
949b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                    Ty.get().getAsOpaquePtr(), RParenLoc);
950c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  } else {
951e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    // C++0x [expr.typeid]p3:
9521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   When typeid is applied to an expression other than an lvalue of a
9531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   polymorphic class type [...] The expression is an unevaluated
954e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //   operand (Clause 5).
955e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor    //
9561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Note that we can't tell whether the expression is an lvalue of a
957ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    // polymorphic class type until after we've parsed the expression; we
958ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    // speculatively assume the subexpression is unevaluated, and fix it up
959ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    // later.
960ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
961c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    Result = ParseExpression();
962c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
963c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // Match the ')'.
9640e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Result.isInvalid())
965c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      SkipUntil(tok::r_paren);
966c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    else {
9674a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
9684a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      RParenLoc = T.getCloseLocation();
9694eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor      if (RParenLoc.isInvalid())
9704eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor        return ExprError();
971fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor
972c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
973effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl                                      Result.release(), RParenLoc);
974c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    }
975c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  }
976c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
97720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  return move(Result);
978c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
979c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
98001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
98101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
98201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' expression ')'
98301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///         '__uuidof' '(' type-id ')'
98401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet///
98501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetExprResult Parser::ParseCXXUuidof() {
98601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
98701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
98801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  SourceLocation OpLoc = ConsumeToken();
9894a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
99001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
99101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  // __uuidof expressions are always parenthesized.
9924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
99301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    return ExprError();
99401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
99501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  ExprResult Result;
99601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
99701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  if (isTypeIdInParens()) {
99801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    TypeResult Ty = ParseTypeName();
99901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
100001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
10014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
100201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
100301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Ty.isInvalid())
100401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      return ExprError();
100501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
10064a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
10074a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    Ty.get().getAsOpaquePtr(),
10084a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    T.getCloseLocation());
100901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  } else {
101001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
101101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    Result = ParseExpression();
101201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
101301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // Match the ')'.
101401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (Result.isInvalid())
101501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      SkipUntil(tok::r_paren);
101601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    else {
10174a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
101801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
10194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
10204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      /*isType=*/false,
10214a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      Result.release(), T.getCloseLocation());
102201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    }
102301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  }
102401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
102501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  return move(Result);
102601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet}
102701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
1028d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \brief Parse a C++ pseudo-destructor expression after the base,
1029d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// . or -> operator, and nested-name-specifier have already been
1030d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// parsed.
1031d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
1032d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       postfix-expression: [C++ 5.2]
1033d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression . pseudo-destructor-name
1034d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         postfix-expression -> pseudo-destructor-name
1035d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
1036d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///       pseudo-destructor-name:
1037d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1038d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier template simple-template-id ::
1039d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///                 ~type-name
1040d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///         ::[opt] nested-name-specifier[opt] ~type-name
1041d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
104260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
1043d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas GregorParser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1044d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 tok::TokenKind OpKind,
1045d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                 CXXScopeSpec &SS,
1046b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                 ParsedType ObjectType) {
1047d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // We're parsing either a pseudo-destructor-name or a dependent
1048d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // member access that has the same form as a
1049d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // pseudo-destructor-name. We parse both in the same way and let
1050d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // the action model sort them out.
1051d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  //
1052d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Note that the ::[opt] nested-name-specifier[opt] has already
1053d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been parsed, and if there was a simple-template-id, it has
1054d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // been coalesced into a template-id annotation token.
1055d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId FirstTypeName;
1056d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation CCLoc;
1057d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::identifier)) {
1058d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1059d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
1060d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1061d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
1062d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else if (Tok.is(tok::annot_template_id)) {
1063e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    // FIXME: retrieve TemplateKWLoc from template-id annotation and
1064e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    // store it in the pseudo-dtor node (to be used when instantiating it).
1065d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setTemplateId(
1066d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                              (TemplateIdAnnotation *)Tok.getAnnotationValue());
1067d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    ConsumeToken();
1068d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1069d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    CCLoc = ConsumeToken();
1070d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  } else {
1071d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    FirstTypeName.setIdentifier(0, SourceLocation());
1072d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
1073d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1074d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the tilde.
1075d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1076d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation TildeLoc = ConsumeToken();
107791ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
107891ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
107991ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    DeclSpec DS(AttrFactory);
108085c60db2131c6d210d4777c3d50bdaf0e69bb8bfBenjamin Kramer    ParseDecltypeSpecifier(DS);
108191ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    if (DS.getTypeSpecType() == TST_error)
108291ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie      return ExprError();
108391ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
108491ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                             OpKind, TildeLoc, DS,
108591ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                             Tok.is(tok::l_paren));
108691ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  }
108791ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
1088d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (!Tok.is(tok::identifier)) {
1089d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    Diag(Tok, diag::err_destructor_tilde_identifier);
1090d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
1091d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  }
1092d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1093d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // Parse the second type.
1094d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  UnqualifiedId SecondTypeName;
1095d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  IdentifierInfo *Name = Tok.getIdentifierInfo();
1096d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SourceLocation NameLoc = ConsumeToken();
1097d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  SecondTypeName.setIdentifier(Name, NameLoc);
1098d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
1099d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // If there is a '<', the second type name is a template-id. Parse
1100d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  // it as such.
1101d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  if (Tok.is(tok::less) &&
1102e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1103e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   Name, NameLoc,
1104e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   false, ObjectType, SecondTypeName,
1105e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   /*AssumeTemplateName=*/true))
1106d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    return ExprError();
1107d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
11089ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
11099ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                           OpLoc, OpKind,
1110d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           SS, FirstTypeName, CCLoc,
1111d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           TildeLoc, SecondTypeName,
1112d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                           Tok.is(tok::l_paren));
1113d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor}
1114d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       boolean-literal: [C++ 2.13.5]
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'true'
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'false'
112060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXBoolLiteral() {
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  tok::TokenKind Kind = Tok.getKind();
1122f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
112450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
112550dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ParseThrowExpression - This handles the C++ throw expression.
112650dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///
112750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///       throw-expression: [C++ 15]
112850dd289f45738ed22b7583d52ed2525b927042ffChris Lattner///         'throw' assignment-expression[opt]
112960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseThrowExpression() {
113050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  assert(Tok.is(tok::kw_throw) && "Not throw!");
113150dd289f45738ed22b7583d52ed2525b927042ffChris Lattner  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
113220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl
11332a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // If the current token isn't the start of an assignment-expression,
11342a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  // then the expression is not present.  This handles things like:
11352a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  //   "C ? throw : (void)42", which is crazy but legal.
11362a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
11372a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::semi:
11382a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_paren:
11392a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_square:
11402a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::r_brace:
11412a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::colon:
11422a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  case tok::comma:
1143bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
114450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
11452a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  default:
114660d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Expr(ParseAssignmentExpression());
114720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    if (Expr.isInvalid()) return move(Expr);
1148bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
11492a2819a0a8e114bfa5a3c4fc4c97fa173155bae9Chris Lattner  }
115050dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
11514cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis
11524cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// ParseCXXThis - This handles the C++ 'this' pointer.
11534cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis///
11544cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
11554cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// a non-lvalue expression whose value is the address of the object for which
11564cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis/// the function is called.
115760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseCXXThis() {
11584cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  assert(Tok.is(tok::kw_this) && "Not 'this'!");
11594cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis  SourceLocation ThisLoc = ConsumeToken();
1160f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXThis(ThisLoc);
11614cc18a4d5222e04bd568b1e3e4d86127dbbcdf3fArgyrios Kyrtzidis}
1162987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1163987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1164987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// Can be interpreted either as function-style casting ("int(x)")
1165987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or class type construction ("ClassType(x,y,z)")
1166987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// or creation of a value-initialized type ("int()").
1167dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// See [C++ 5.2.3].
1168987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1169987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       postfix-expression: [C++ 5.2p1]
1170dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl///         simple-type-specifier '(' expression-list[opt] ')'
1171dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] simple-type-specifier braced-init-list
1172dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl///         typename-specifier '(' expression-list[opt] ')'
1173dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x] typename-specifier braced-init-list
1174987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
117560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
117620df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian RedlParser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
1177987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1178b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
1179987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1180dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  assert((Tok.is(tok::l_paren) ||
1181dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl          (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1182dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl         && "Expected '(' or '{'!");
1183bc61bd8109d9accf8f966b59e3f16a1497e72adfDouglas Gregor
1184dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  if (Tok.is(tok::l_brace)) {
11856dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    ExprResult Init = ParseBraceInitializer();
11866dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    if (Init.isInvalid())
11876dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl      return Init;
11886dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    Expr *InitList = Init.take();
11896dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
11906dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl                                             MultiExprArg(&InitList, 1),
11916dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl                                             SourceLocation());
1192dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  } else {
1193dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1194dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
11954a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
11964a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
1197dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
1198dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    ExprVector Exprs(Actions);
1199dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    CommaLocsTy CommaLocs;
1200dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl
1201dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (Tok.isNot(tok::r_paren)) {
1202dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      if (ParseExpressionList(Exprs, CommaLocs)) {
1203dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl        SkipUntil(tok::r_paren);
1204dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl        return ExprError();
1205dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      }
1206987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    }
1207987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1208dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // Match the ')'.
12094a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
1210987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
1211dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    // TypeRep could be null, if it references an invalid typedef.
1212dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    if (!TypeRep)
1213dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl      return ExprError();
1214ef0cb8e62d090ad88a01ca9fa89e48d7416f0ac7Sebastian Redl
1215dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl    assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1216dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl           "Unexpected number of commas!");
12174a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
12184a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                             move_arg(Exprs),
12194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                             T.getCloseLocation());
1220dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl  }
1221987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
1222987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
122399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// ParseCXXCondition - if/switch/while condition expression.
122471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
122571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///       condition:
122671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         expression
122771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///         type-specifier-seq declarator '=' assignment-expression
122871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
122971b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///             '=' assignment-expression
123071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis///
123199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param ExprResult if the condition was parsed as an expression, the
123299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed expression.
123399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
123499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \param DeclResult if the condition was parsed as a declaration, the
123599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// parsed declaration.
123699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor///
1237586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param Loc The location of the start of the statement that requires this
1238586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// condition, e.g., the "for" in a for loop.
1239586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
1240586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// \param ConvertToBoolean Whether the condition expression should be
1241586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor/// converted to a boolean value.
1242586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor///
124399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor/// \returns true if there was a parsing, false otherwise.
124460d7b3a319d84d688752be3870615ac0f111fb16John McCallbool Parser::ParseCXXCondition(ExprResult &ExprOut,
124560d7b3a319d84d688752be3870615ac0f111fb16John McCall                               Decl *&DeclOut,
1246586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               SourceLocation Loc,
1247586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor                               bool ConvertToBoolean) {
124801dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  if (Tok.is(tok::code_completion)) {
1249f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
12507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
12517d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return true;
125201dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor  }
125301dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor
125499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!isCXXConditionDeclaration()) {
1255586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // Parse the expression.
125660d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprOut = ParseExpression(); // expression
125760d7b3a319d84d688752be3870615ac0f111fb16John McCall    DeclOut = 0;
125860d7b3a319d84d688752be3870615ac0f111fb16John McCall    if (ExprOut.isInvalid())
1259586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return true;
1260586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
1261586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    // If required, convert to a boolean value.
1262586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    if (ConvertToBoolean)
126360d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprOut
126460d7b3a319d84d688752be3870615ac0f111fb16John McCall        = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
126560d7b3a319d84d688752be3870615ac0f111fb16John McCall    return ExprOut.isInvalid();
126699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
126771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
126871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // type-specifier-seq
12690b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
127071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseSpecifierQualifierList(DS);
127171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
127271b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // declarator
127371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
127471b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  ParseDeclarator(DeclaratorInfo);
127571b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
127671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // simple-asm-expr[opt]
127771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  if (Tok.is(tok::kw_asm)) {
1278ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SourceLocation Loc;
127960d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult AsmLabel(ParseSimpleAsm(&Loc));
12800e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (AsmLabel.isInvalid()) {
128171b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis      SkipUntil(tok::semi);
128299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return true;
128371b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis    }
1284effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    DeclaratorInfo.setAsmLabel(AsmLabel.release());
1285ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    DeclaratorInfo.SetRangeEnd(Loc);
128671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  }
128771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
128871b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // If attributes are present, parse them.
12897f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseGNUAttributes(DeclaratorInfo);
129071b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
129199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  // Type-check the declaration itself.
129260d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
12937f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                        DeclaratorInfo);
129460d7b3a319d84d688752be3870615ac0f111fb16John McCall  DeclOut = Dcl.get();
129560d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprOut = ExprError();
1296a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
129771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis  // '=' assignment-expression
1298d6c7c67313634b317a0d63c32be0511a121bb33dRichard Trieu  // If a '==' or '+=' is found, suggest a fixit to '='.
1299fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  if (isTokenEqualOrEqualTypo()) {
1300dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeToken();
130160d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult AssignExpr(ParseAssignmentExpression());
130299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (!AssignExpr.isInvalid())
130334b41d939a1328f484511c6002ba2456db879a29Richard Smith      Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
130434b41d939a1328f484511c6002ba2456db879a29Richard Smith                                   DS.getTypeSpecType() == DeclSpec::TST_auto);
130599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  } else {
130699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // FIXME: C++0x allows a braced-init-list
130799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    Diag(Tok, diag::err_expected_equal_after_declarator);
130899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
130999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
1310586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // FIXME: Build a reference to this declaration? Convert it to bool?
1311586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  // (This is currently handled by Sema).
1312483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
1313483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Actions.FinalizeDeclaration(DeclOut);
1314586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
131599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return false;
131671b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis}
131771b914b999d9c4b6df11068fc5a3291ac4770492Argyrios Kyrtzidis
13186aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brief Determine whether the current token starts a C++
13196aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// simple-type-specifier.
13206aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::isCXXSimpleTypeSpecifier() const {
13216aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  switch (Tok.getKind()) {
13226aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::annot_typename:
13236aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_short:
13246aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_long:
1325338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet  case tok::kw___int64:
13266aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_signed:
13276aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_unsigned:
13286aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_void:
13296aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char:
13306aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_int:
1331aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case tok::kw_half:
13326aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_float:
13336aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_double:
13346aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_wchar_t:
13356aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char16_t:
13366aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_char32_t:
13376aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_bool:
1338d9d75e57dfa22366c0379c92beac1db82db34e9aDouglas Gregor  case tok::kw_decltype:
13396aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  case tok::kw_typeof:
1340db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt  case tok::kw___underlying_type:
13416aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
13426aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
13436aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  default:
13446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    break;
13456aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
13466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
13476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
13486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
13496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
1350987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1351987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// This should only be called when the current token is known to be part of
1352987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// simple-type-specifier.
1353987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1354987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       simple-type-specifier:
1355eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis///         '::'[opt] nested-name-specifier[opt] type-name
1356987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1357987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         char
1358987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         wchar_t
1359987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         bool
1360987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         short
1361987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         int
1362987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         long
1363987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         signed
1364987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         unsigned
1365987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         float
1366987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         double
1367987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         void
1368987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [GNU]   typeof-specifier
1369987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis/// [C++0x] auto               [TODO]
1370987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1371987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///       type-name:
1372987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         class-name
1373987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         enum-name
1374987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///         typedef-name
1375987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis///
1376987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidisvoid Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1377987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  DS.SetRangeStart(Tok.getLocation());
1378987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  const char *PrevSpec;
1379fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
1380987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  SourceLocation Loc = Tok.getLocation();
13811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1382987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  switch (Tok.getKind()) {
138355a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::identifier:   // foo::bar
138455a7cefc846765ac7d142a63f773747a20518d71Chris Lattner  case tok::coloncolon:   // ::foo::bar
1385b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Annotation token should already be formed!");
13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
1387b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Not a simple-type-specifier token!");
138855a7cefc846765ac7d142a63f773747a20518d71Chris Lattner
1389987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // type-name
1390b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  case tok::annot_typename: {
13916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (getTypeAnnotation(Tok))
13926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
13936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                         getTypeAnnotation(Tok));
13946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    else
13956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DS.SetTypeSpecError();
13969bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
13979bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
13989bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    ConsumeToken();
13999bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
14009bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
14019bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
14029bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // Objective-C interface.  If we don't have Objective-C or a '<', this is
14039bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    // just a normal reference to a typedef name.
14049bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    if (Tok.is(tok::less) && getLang().ObjC1)
14059bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor      ParseObjCProtocolQualifiers(DS);
14069bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
14079bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.Finish(Diags, PP);
14089bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    return;
1409987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
14101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1411987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // builtin types
1412987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_short:
1413fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
1414987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1415987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_long:
1416fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
1417338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet    break;
1418338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet  case tok::kw___int64:
1419338d7f7362d18fa9c39c6bb5282b4e20574a9309Francois Pichet    DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1420987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1421987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_signed:
1422fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
1423987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1424987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_unsigned:
1425fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
1426987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1427987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_void:
1428fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
1429987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1430987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_char:
1431fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
1432987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1433987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_int:
1434fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
1435987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1436aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case tok::kw_half:
1437aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1438aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    break;
1439987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_float:
1440fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
1441987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1442987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_double:
1443fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
1444987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1445987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_wchar_t:
1446fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
1447987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
1448f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char16_t:
1449fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
1450f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1451f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case tok::kw_char32_t:
1452fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
1453f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1454987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_bool:
1455fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
1456987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    break;
14575e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie  case tok::annot_decltype:
14585e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie  case tok::kw_decltype:
14595e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie    DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
14605e089fe1affb63d670ea02010b104bd9fa3477a1David Blaikie    return DS.Finish(Diags, PP);
14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1462987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // GNU typeof support.
1463987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case tok::kw_typeof:
1464987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    ParseTypeofSpecifier(DS);
14659b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor    DS.Finish(Diags, PP);
1466987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis    return;
1467987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
1468b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner  if (Tok.is(tok::annot_typename))
1469eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1470eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  else
1471eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    DS.SetRangeEnd(Tok.getLocation());
1472987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  ConsumeToken();
14739b3064b55f3c858923734e8b1c9831777fc22554Douglas Gregor  DS.Finish(Diags, PP);
1474987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
14751cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
14762f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
14772f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// [dcl.name]), which is a non-empty sequence of type-specifiers,
14782f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// e.g., "const short int". Note that the DeclSpec is *not* finished
14792f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// by parsing the type-specifier-seq, because these sequences are
14802f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// typically followed by some form of declarator. Returns true and
14812f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// emits diagnostics if this is not a type-specifier-seq, false
14822f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor/// otherwise.
14832f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
14842f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///   type-specifier-seq: [C++ 8.1]
14852f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///     type-specifier type-specifier-seq[opt]
14862f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor///
14872f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregorbool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
14882f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  DS.SetRangeStart(Tok.getLocation());
14892f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  const char *PrevSpec = 0;
1490fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  unsigned DiagID;
1491fec54013fcd0eb72642741584ca04c1bc292bef8John McCall  bool isInvalid = 0;
14922f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
14932f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  // Parse one or more of the type specifiers.
1494d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1495d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl      ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
14969fa8e569407e02148888136609431a3fe083096dNick Lewycky    Diag(Tok, diag::err_expected_type);
14972f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor    return true;
14982f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  }
14991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1500d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1501d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl         ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1502d9bafa76f8d6eb9e4f4974ed322217f8df6bb82eSebastian Redl  {}
15032f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
1504396a9f235e160093b5f803f7a6a18fad7b68bdbeDouglas Gregor  DS.Finish(Diags, PP);
15052f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor  return false;
15062f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor}
15072f1bc5285ccd40f411af5f5993f013e27e74ab78Douglas Gregor
15083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \brief Finish parsing a C++ unqualified-id that is a template-id of
15093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// some form.
15103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
15113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// This routine is invoked when a '<' is encountered after an identifier or
15123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
15133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// whether the unqualified-id is actually a template-id. This routine will
15143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// then parse the template arguments and form the appropriate template-id to
15153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// return to the caller.
15163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
15173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param SS the nested-name-specifier that precedes this template-id, if
15183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// we're actually parsing a qualified-id.
15193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
15203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Name for constructor and destructor names, this is the actual
15213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// identifier that may be a template-name.
15223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
15233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param NameLoc the location of the class-name in a constructor or
15243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// destructor.
15253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
15263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we're entering the scope of the
15273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
15283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
152946df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
153046df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
153146df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
15323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Id as input, describes the template-name or operator-function-id
15333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// that precedes the '<'. If template arguments were parsed successfully,
15343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// will be updated with the template-id.
15353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1536d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// \param AssumeTemplateId When true, this routine will assume that the name
1537d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor/// refers to a template without performing name lookup to verify.
1538d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor///
15393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if a parse error occurred, false otherwise.
15403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1541e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          SourceLocation TemplateKWLoc,
15423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          IdentifierInfo *Name,
15433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          SourceLocation NameLoc,
15443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                          bool EnteringContext,
1545b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          ParsedType ObjectType,
1546d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                          UnqualifiedId &Id,
1547e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          bool AssumeTemplateId) {
15480278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  assert((AssumeTemplateId || Tok.is(tok::less)) &&
15490278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor         "Expected '<' to finish parsing a template-id");
15503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
15513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateTy Template;
15523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateNameKind TNK = TNK_Non_template;
15533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (Id.getKind()) {
15543f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
1555014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
1556e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
1557d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    if (AssumeTemplateId) {
1558e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
1559d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Id, ObjectType, EnteringContext,
1560d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor                                               Template);
1561d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
1562d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        return true;
15631fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    } else {
15641fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
15657c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS,
15667c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateKWLoc.isValid(), Id,
15677c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   ObjectType, EnteringContext, Template,
15681fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
15691fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor
15701fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
15711fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          ObjectType && IsTemplateArgumentList()) {
15721fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // We have something like t->getAs<T>(), where getAs is a
15731fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // member of an unknown specialization. However, this will only
15741fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // parse correctly as a template, so suggest the keyword 'template'
15751fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        // before 'getAs' and treat this as a dependent template name.
15761fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        std::string Name;
15771fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        if (Id.getKind() == UnqualifiedId::IK_Identifier)
15781fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = Id.Identifier->getName();
15791fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        else {
15801fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          Name = "operator ";
15811fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
15821fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
15831fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          else
15841fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor            Name += Id.Identifier->getName();
15851fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        }
15861fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor        Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
15871fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << Name
15881fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          << FixItHint::CreateInsertion(Id.StartLocation, "template ");
1589e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1590e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 SS, TemplateKWLoc, Id,
1591e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 ObjectType, EnteringContext,
1592e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                 Template);
1593d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor        if (TNK == TNK_Non_template)
15941fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor          return true;
15951fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      }
15961fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    }
15973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
15983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1599014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_ConstructorName: {
1600014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
16011fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1602014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
16037c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara    TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
16047c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                 TemplateName, ObjectType,
16051fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 EnteringContext, Template,
16061fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                 MemberOfUnknownSpecialization);
16073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1608014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
16093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1610014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  case UnqualifiedId::IK_DestructorName: {
1611014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    UnqualifiedId TemplateName;
16121fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor    bool MemberOfUnknownSpecialization;
1613014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor    TemplateName.setIdentifier(Name, NameLoc);
16142d1c21414199a7452f122598189363a3922605b1Douglas Gregor    if (ObjectType) {
1615e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1616e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               SS, TemplateKWLoc, TemplateName,
1617e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               ObjectType, EnteringContext,
1618e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               Template);
1619d6ab232bb3ec9847de5af06249afb63078b5f2eeDouglas Gregor      if (TNK == TNK_Non_template)
16202d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
16212d1c21414199a7452f122598189363a3922605b1Douglas Gregor    } else {
16227c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara      TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
16237c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   TemplateName, ObjectType,
16241fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   EnteringContext, Template,
16251fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor                                   MemberOfUnknownSpecialization);
16262d1c21414199a7452f122598189363a3922605b1Douglas Gregor
1627b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
1628124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        Diag(NameLoc, diag::err_destructor_template_id)
1629124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          << Name << SS.getRange();
16302d1c21414199a7452f122598189363a3922605b1Douglas Gregor        return true;
16312d1c21414199a7452f122598189363a3922605b1Douglas Gregor      }
16322d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
16333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    break;
1634014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor  }
16353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  default:
16373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
16383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
16393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (TNK == TNK_Non_template)
16413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
16423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Parse the enclosed template argument list.
16443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
16453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  TemplateArgList TemplateArgs;
16460278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (Tok.is(tok::less) &&
16470278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
1648059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                       SS, true, LAngleLoc,
16493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       TemplateArgs,
16503f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                       RAngleLoc))
16513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
16523f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
1654e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1655e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt      Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
16563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Form a parsed representation of the template-id to be stored in the
16573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // UnqualifiedId.
16583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateIdAnnotation *TemplateId
16593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      = TemplateIdAnnotation::Allocate(TemplateArgs.size());
16603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
16623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->Name = Id.Identifier;
1663014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = OO_None;
16643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
16653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
1666014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Name = 0;
1667014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->Operator = Id.OperatorFunctionId.Operator;
1668014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateId->TemplateNameLoc = Id.StartLocation;
16693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
16703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1671059101f922de6eb765601459925f4c8914420b23Douglas Gregor    TemplateId->SS = SS;
16722b5289b6fd7e3d9899868410a498c081c9595662John McCall    TemplateId->Template = Template;
16733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->Kind = TNK;
16743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->LAngleLoc = LAngleLoc;
16753f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    TemplateId->RAngleLoc = RAngleLoc;
1676314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
16773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
1678314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor         Arg != ArgEnd; ++Arg)
16793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Args[Arg] = TemplateArgs[Arg];
16803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setTemplateId(TemplateId);
16823f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
16833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
16843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Bundle the template arguments together.
16863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
16873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                     TemplateArgs.size());
1688fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara
16893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // Constructor and destructor names.
1690f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Type
169155d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara    = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
169255d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara                                  Template, NameLoc,
1693fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                  LAngleLoc, TemplateArgsPtr, RAngleLoc,
1694fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                  /*IsCtorOrDtorName=*/true);
16953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Type.isInvalid())
16963f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return true;
16973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
16983f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
16993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
17003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  else
17013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
17023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
17033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return false;
17043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
17053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1706ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse an operator-function-id or conversion-function-id as part
1707ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// of a C++ unqualified-id.
17083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1709ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// This routine is responsible only for parsing the operator-function-id or
1710ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// conversion-function-id; it does not handle template arguments in any way.
17113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1712ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
17133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       operator-function-id: [C++ 13.5]
17143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         'operator' operator
17153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1716ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       operator: one of
17173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            new   delete  new[]   delete[]
17183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            +     -    *  /    %  ^    &   |   ~
17193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            !     =    <  >    += -=   *=  /=  %=
17203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ^=    &=   |= <<   >> >>= <<=  ==  !=
17213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            <=    >=   && ||   ++ --   ,   ->* ->
17223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///            ()    []
17233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
17243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-function-id: [C++ 12.3.2]
17253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         operator conversion-type-id
17263f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
17273f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-type-id:
17283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         type-specifier-seq conversion-declarator[opt]
17293f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
17303f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///       conversion-declarator:
17313f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///         ptr-operator conversion-declarator[opt]
17323f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \endcode
17333f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
17343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
17353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
17363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
17373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param EnteringContext whether we are entering the scope of the
17383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// nested-name-specifier.
17393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
1740ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
1741ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// expression, the type of the base object whose member is being accessed.
1742ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1743ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
1744ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1745ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \returns true if parsing fails, false otherwise.
1746ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregorbool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1747b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType ObjectType,
1748ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                        UnqualifiedId &Result) {
1749ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1750ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1751ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Consume the 'operator' keyword.
1752ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation KeywordLoc = ConsumeToken();
1753ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1754ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Determine what kind of operator name we have.
1755ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  unsigned SymbolIdx = 0;
1756ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  SourceLocation SymbolLocations[3];
1757ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  OverloadedOperatorKind Op = OO_None;
1758ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  switch (Tok.getKind()) {
1759ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_new:
1760ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::kw_delete: {
1761ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      bool isNew = Tok.getKind() == tok::kw_new;
1762ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Consume the 'new' or 'delete'.
1763ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();
1764ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      if (Tok.is(tok::l_square)) {
17654a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        // Consume the '[' and ']'.
17664a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        BalancedDelimiterTracker T(*this, tok::l_square);
17674a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeOpen();
17684a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeClose();
17694a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        if (T.getCloseLocation().isInvalid())
1770ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor          return true;
1771ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
17724a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        SymbolLocations[SymbolIdx++] = T.getOpenLocation();
17734a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        SymbolLocations[SymbolIdx++] = T.getCloseLocation();
1774ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_Array_New : OO_Array_Delete;
1775ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      } else {
1776ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        Op = isNew? OO_New : OO_Delete;
1777ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
1778ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1779ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1780ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1781ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1782ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::Token:                                                     \
1783ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
1784ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_##Name;                                                    \
1785ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1786ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1787ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor#include "clang/Basic/OperatorKinds.def"
1788ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1789ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_paren: {
17904a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      // Consume the '(' and ')'.
17914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      BalancedDelimiterTracker T(*this, tok::l_paren);
17924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeOpen();
17934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
17944a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      if (T.getCloseLocation().isInvalid())
1795ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1796ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
17974a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getOpenLocation();
17984a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getCloseLocation();
1799ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Call;
1800ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1801ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1802ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1803ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::l_square: {
18044a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      // Consume the '[' and ']'.
18054a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      BalancedDelimiterTracker T(*this, tok::l_square);
18064a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeOpen();
18074a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
18084a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      if (T.getCloseLocation().isInvalid())
1809ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        return true;
1810ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
18114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getOpenLocation();
18124a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      SymbolLocations[SymbolIdx++] = T.getCloseLocation();
1813ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      Op = OO_Subscript;
1814ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1815ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1816ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1817ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    case tok::code_completion: {
1818ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Code completion for the operator name.
181923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOperatorName(getCurScope());
18207d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
1821ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      // Don't try to parse any further.
1822ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      return true;
1823ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    }
1824ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1825ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    default:
1826ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      break;
1827ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
1828ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1829ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Op != OO_None) {
1830ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    // We have parsed an operator-function-id.
1831ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1832ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return false;
1833ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
18340486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
18350486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  // Parse a literal-operator-id.
18360486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //
18370486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //   literal-operator-id: [C++0x 13.5.8]
18380486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  //     operator "" identifier
18390486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
18400486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
18417fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith    Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
18420486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    if (Tok.getLength() != 2)
18430486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
18440486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    ConsumeStringToken();
18450486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
18460486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    if (Tok.isNot(tok::identifier)) {
18470486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      Diag(Tok.getLocation(), diag::err_expected_ident);
18480486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt      return true;
18490486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    }
18500486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt
18510486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    IdentifierInfo *II = Tok.getIdentifierInfo();
18520486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt    Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
18533e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    return false;
18540486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  }
1855ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1856ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse a conversion-function-id.
1857ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1858ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-function-id: [C++ 12.3.2]
1859ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     operator conversion-type-id
1860ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1861ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-type-id:
1862ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     type-specifier-seq conversion-declarator[opt]
1863ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //
1864ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //   conversion-declarator:
1865ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  //     ptr-operator conversion-declarator[opt]
1866ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1867ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the type-specifier-seq.
18680b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
1869f6e6fc801c700c7b8ac202ddbe550d9843a816fcDouglas Gregor  if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
1870ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1871ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1872ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Parse the conversion-declarator, which is merely a sequence of
1873ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // ptr-operators.
1874ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Declarator D(DS, Declarator::TypeNameContext);
1875ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1876ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1877ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Finish up the type.
1878f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
1879ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (Ty.isInvalid())
1880ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return true;
1881ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1882ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  // Note that this is a conversion-function-id.
1883ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1884ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                 D.getSourceRange().getEnd());
1885ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return false;
1886ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
1887ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
1888ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1889ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// name of an entity.
1890ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1891ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \code
1892ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///       unqualified-id: [C++ expr.prim.general]
1893ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         identifier
1894ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         operator-function-id
1895ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         conversion-function-id
1896ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// [C++0x] literal-operator-id [TODO]
1897ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         ~ class-name
1898ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///         template-id
1899ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1900ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \endcode
1901ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1902ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param The nested-name-specifier that preceded this unqualified-id. If
1903ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1904ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
1905ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \param EnteringContext whether we are entering the scope of the
1906ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// nested-name-specifier.
1907ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor///
19083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowDestructorName whether we allow parsing of a destructor name.
19093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
19103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param AllowConstructorName whether we allow parsing a constructor name.
19113f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
191246df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// \param ObjectType if this unqualified-id occurs within a member access
191346df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor/// expression, the type of the base object whose member is being accessed.
191446df8cc349096f141c841dbffaa72641ff1dd94bDouglas Gregor///
19153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \param Result on a successful parse, contains the parsed unqualified-id.
19163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor///
19173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor/// \returns true if parsing fails, false otherwise.
19183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregorbool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
19193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowDestructorName,
19203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                bool AllowConstructorName,
1921b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                ParsedType ObjectType,
1922e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                SourceLocation& TemplateKWLoc,
19233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor                                UnqualifiedId &Result) {
19240278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
19250278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // Handle 'A::template B'. This is for template-ids which have not
19260278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  // already been annotated by ParseOptionalCXXScopeSpecifier().
19270278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  bool TemplateSpecified = false;
19280278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
19290278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor      (ObjectType || SS.isSet())) {
19300278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateSpecified = true;
19310278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    TemplateKWLoc = ConsumeToken();
19320278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor  }
19330278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor
19343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
19353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   identifier
19363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (when it hasn't already been annotated)
19373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::identifier)) {
19383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Consume the identifier.
19393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *Id = Tok.getIdentifierInfo();
19403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation IdLoc = ConsumeToken();
19413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1942b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    if (!getLang().CPlusPlus) {
1943b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // If we're not in C++, only identifiers matter. Record the
1944b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      // identifier and return.
1945b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      Result.setIdentifier(Id, IdLoc);
1946b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      return false;
1947b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor    }
1948b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor
19493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (AllowConstructorName &&
195023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
19513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed a constructor name.
1952fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara      ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
1953fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          &SS, false, false,
1954fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          ParsedType(),
1955fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          /*IsCtorOrDtorName=*/true,
1956fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                          /*NonTrivialTypeSourceInfo=*/true);
1957fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara      Result.setConstructorName(Ty, IdLoc, IdLoc);
19583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    } else {
19593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      // We have parsed an identifier.
19603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      Result.setIdentifier(Id, IdLoc);
19613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
19623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // If the next token is a '<', we may have a template.
19640278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less))
1965e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
1966e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
1967e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
19683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
19703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
19713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
19723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
19733f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   template-id (already parsed and annotated)
19743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::annot_template_id)) {
197525a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
19760efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
19770efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    // If the template-name names the current class, then this is a constructor
19780efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    if (AllowConstructorName && TemplateId->Name &&
197923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
19800efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      if (SS.isSet()) {
19810efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // C++ [class.qual]p2 specifies that a qualified template-name
19820efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // is taken as the constructor name where a constructor can be
19830efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // declared. Thus, the template arguments are extraneous, so
19840efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        // complain about them and remove them entirely.
19850efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        Diag(TemplateId->TemplateNameLoc,
19860efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor             diag::err_out_of_line_constructor_template_id)
19870efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor          << TemplateId->Name
1988849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateRemoval(
19890efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                    SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1990fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara        ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
1991fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            TemplateId->TemplateNameLoc,
1992fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            getCurScope(),
1993fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            &SS, false, false,
1994fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            ParsedType(),
1995fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*IsCtorOrDtorName=*/true,
1996fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*NontrivialTypeSourceInfo=*/true);
1997fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara        Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
19980efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor                                  TemplateId->RAngleLoc);
19990efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        ConsumeToken();
20000efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor        return false;
20010efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      }
20020efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
20030efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      Result.setConstructorTemplateId(TemplateId);
20040efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      ConsumeToken();
20050efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor      return false;
20060efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    }
20070efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor
20083f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // We have already parsed a template-id; consume the annotation token as
20093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // our unqualified-id.
20100efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor    Result.setTemplateId(TemplateId);
2011e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    TemplateKWLoc = TemplateId->TemplateKWLoc;
20123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    ConsumeToken();
20133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
20143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
20153f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  // unqualified-id:
20173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   operator-function-id
20183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  //   conversion-function-id
20193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (Tok.is(tok::kw_operator)) {
2020ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
20213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
20223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
2023e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // If we have an operator-function-id or a literal-operator-id and the next
2024e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    // token is a '<', we may have a
2025ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //
2026ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //   template-id:
2027ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    //     operator-function-id < template-argument-list[opt] >
2028e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt    if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2029e6252d1587f98dbac704178e7b2ce53116048310Sean Hunt         Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
20300278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor        (TemplateSpecified || Tok.is(tok::less)))
2031e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2032e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          0, SourceLocation(),
2033e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2034e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
20353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
20373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
20383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
2039b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor  if (getLang().CPlusPlus &&
2040b862b8f93424a583fc912ab37bbbac1c231e852eDouglas Gregor      (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
20413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // C++ [expr.unary.op]p10:
20423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   There is an ambiguity in the unary-expression ~X(), where X is a
20433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //   class-name. The ambiguity is resolved in favor of treating ~ as a
20443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    //    unary complement rather than treating ~X as referring to a destructor.
20453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the '~'.
20473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation TildeLoc = ConsumeToken();
204853a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie
204953a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
205053a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      DeclSpec DS(AttrFactory);
205153a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
205253a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
205353a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie        Result.setDestructorName(TildeLoc, Type, EndLoc);
205453a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie        return false;
205553a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      }
205653a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      return true;
205753a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    }
20583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name.
20603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    if (Tok.isNot(tok::identifier)) {
2061124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      Diag(Tok, diag::err_destructor_tilde_identifier);
20623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
20633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    }
20643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20653f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Parse the class-name (or template-name in a simple-template-id).
20663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
20673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    SourceLocation ClassNameLoc = ConsumeToken();
20683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20690278e123b4606ea15dbfa717e9c5a76a5ef2bc7dDouglas Gregor    if (TemplateSpecified || Tok.is(tok::less)) {
2070b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
2071e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara      return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2072e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          ClassName, ClassNameLoc,
2073e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          EnteringContext, ObjectType,
2074e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          Result, TemplateSpecified);
20752d1c21414199a7452f122598189363a3922605b1Douglas Gregor    }
20762d1c21414199a7452f122598189363a3922605b1Douglas Gregor
20773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    // Note that this is a destructor name.
2078b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2079b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              ClassNameLoc, getCurScope(),
2080b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              SS, ObjectType,
2081b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                              EnteringContext);
2082124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (!Ty)
20833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor      return true;
2084124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
20853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
20863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return false;
20873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
20883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20892d1c21414199a7452f122598189363a3922605b1Douglas Gregor  Diag(Tok, diag::err_expected_unqualified_id)
20902d1c21414199a7452f122598189363a3922605b1Douglas Gregor    << getLang().CPlusPlus;
20913f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  return true;
20923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor}
20933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
20944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
20954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// memory in a typesafe manner and call constructors.
20961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
209759232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the new expression after the optional :: has
209859232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
209959232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// is its location.  Otherwise, "Start" is the location of the 'new' token.
21004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
21014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
21024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] new-type-id
21034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
21044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
21054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
21064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
21074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
21084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
21094c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
2110cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-type-id:
2111cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   type-specifier-seq new-declarator[opt]
2112893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor/// [GNU]             attributes type-specifier-seq new-declarator[opt]
2113cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
2114cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///        new-declarator:
2115cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   ptr-operator new-declarator[opt]
2116cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///                   direct-new-declarator
2117cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl///
21184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-initializer:
21194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list[opt] ')'
2120dbef1bb8a8118b7b73e184e08fccfe0eaf914ddaSebastian Redl/// [C++0x]           braced-init-list
21214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
212260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
212359232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
212459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_new) && "expected 'new' token");
212559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken();   // Consume 'new'
21264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
21274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
21284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // second form of new-expression. It can't be a new-type-id.
21294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2130a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector PlacementArgs(Actions);
21314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  SourceLocation PlacementLParen, PlacementRParen;
21324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
21334bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor  SourceRange TypeIdParens;
21340b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
21350b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis  Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
21364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
21374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // If it turns out to be a placement, we change the type location.
21384a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
21394a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
21404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    PlacementLParen = T.getOpenLocation();
2141cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2142cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
214320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2144cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
21454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
21464a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
21474a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    PlacementRParen = T.getCloseLocation();
2148cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementRParen.isInvalid()) {
2149cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
215020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2151cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
21524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2153cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (PlacementArgs.empty()) {
21544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Reset the placement locations. There was no placement.
21554a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      TypeIdParens = T.getRange();
21564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      PlacementLParen = PlacementRParen = SourceLocation();
21574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    } else {
21584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // We still need the type.
21594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      if (Tok.is(tok::l_paren)) {
21604a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        BalancedDelimiterTracker T(*this, tok::l_paren);
21614a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeOpen();
2162893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor        MaybeParseGNUAttributes(DeclaratorInfo);
2163cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseSpecifierQualifierList(DS);
2164ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2165cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        ParseDeclarator(DeclaratorInfo);
21664a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        T.consumeClose();
21674a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor        TypeIdParens = T.getRange();
21684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      } else {
2169893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor        MaybeParseGNUAttributes(DeclaratorInfo);
2170cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        if (ParseCXXTypeSpecifierSeq(DS))
2171cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          DeclaratorInfo.setInvalidType(true);
2172ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        else {
2173ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2174cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl          ParseDeclaratorInternal(DeclaratorInfo,
2175cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                                  &Parser::ParseDirectNewDeclarator);
2176ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl        }
21774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      }
21784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
21794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  } else {
2180cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // A new-type-id is a simplified type-id, where essentially the
2181cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    // direct-declarator is replaced by a direct-new-declarator.
2182893e1cc13ab17e96ada5019df6978af1668fee26Douglas Gregor    MaybeParseGNUAttributes(DeclaratorInfo);
2183cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ParseCXXTypeSpecifierSeq(DS))
2184cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      DeclaratorInfo.setInvalidType(true);
2185ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    else {
2186ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2187cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      ParseDeclaratorInternal(DeclaratorInfo,
2188cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                              &Parser::ParseDirectNewDeclarator);
2189ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    }
21904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
2191eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (DeclaratorInfo.isInvalidType()) {
2192cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
219320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return ExprError();
2194cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
21954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
21962aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  ExprResult Initializer;
21974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
21984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_paren)) {
21992aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    SourceLocation ConstructorLParen, ConstructorRParen;
22002aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    ExprVector ConstructorArgs(Actions);
22014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
22024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
22034a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    ConstructorLParen = T.getOpenLocation();
22044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    if (Tok.isNot(tok::r_paren)) {
22054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      CommaLocsTy CommaLocs;
2206cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2207cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl        SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
220820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl        return ExprError();
2209cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      }
22104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
22114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
22124a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    ConstructorRParen = T.getCloseLocation();
2213cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (ConstructorRParen.isInvalid()) {
2214cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
221520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
2216cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
22172aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
22182aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                                             ConstructorRParen,
22192aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                                             move_arg(ConstructorArgs));
222029e3a31b7cbd9f9cdf2cc857a3a805871b6f3f62Richard Smith  } else if (Tok.is(tok::l_brace) && getLang().CPlusPlus0x) {
22217fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith    Diag(Tok.getLocation(),
22227fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith         diag::warn_cxx98_compat_generalized_initializer_lists);
22232aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    Initializer = ParseBraceInitializer();
22244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
22252aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  if (Initializer.isInvalid())
22262aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    return Initializer;
22274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2228f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2229f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                             move_arg(PlacementArgs), PlacementRParen,
22302aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                             TypeIdParens, DeclaratorInfo, Initializer.take());
22314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
22324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
22344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// passed to ParseDeclaratorInternal.
22354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
22364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        direct-new-declarator:
22374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '[' expression ']'
22384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   direct-new-declarator '[' constant-expression ']'
22394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
224059232d35f5820e334b6c8b007ae8006f4390055dChris Lattnervoid Parser::ParseDirectNewDeclarator(Declarator &D) {
22414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Parse the array dimensions.
22424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool first = true;
22434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  while (Tok.is(tok::l_square)) {
22444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_square);
22454a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
22464a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
224760d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Size(first ? ParseExpression()
22482f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl                                : ParseConstantExpression());
22490e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Size.isInvalid()) {
22504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      // Recover
22514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      SkipUntil(tok::r_square);
22524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
22534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    }
22544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    first = false;
22554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22564a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
22570b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
22580b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes attrs(AttrFactory);
22590b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    D.AddTypeInfo(DeclaratorChunk::getArray(0,
22607f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                            /*static=*/false, /*star=*/false,
22614a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Size.release(),
22624a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            T.getOpenLocation(),
22634a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            T.getCloseLocation()),
22644a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                  attrs, T.getCloseLocation());
22654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22664a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
22674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl      return;
22684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
22694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
22704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
22724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// This ambiguity appears in the syntax of the C++ new operator.
22734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
22744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-expression:
22754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
22764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                                     new-initializer[opt]
22774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
22784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        new-placement:
22794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '(' expression-list ')'
22804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
2281ca0408fb49c1370430672acf2d770b7151cf71deJohn McCallbool Parser::ParseExpressionListOrTypeId(
22825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   SmallVectorImpl<Expr*> &PlacementArgs,
228359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner                                         Declarator &D) {
22844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // The '(' was already consumed.
22854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (isTypeIdInParens()) {
2286cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseSpecifierQualifierList(D.getMutableDeclSpec());
2287ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetSourceRange(D.getDeclSpec().getSourceRange());
2288cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ParseDeclarator(D);
2289eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    return D.isInvalidType();
22904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
22914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // It's not a type, it has to be an expression list.
22934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Discard the comma locations - ActOnCXXNew has enough parameters.
22944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  CommaLocsTy CommaLocs;
22954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return ParseExpressionList(PlacementArgs, CommaLocs);
22964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
22974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
22994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// to free memory allocated by new.
23004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///
230159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// This method is called to parse the 'delete' expression after the optional
230259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
230359232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// and "Start" is its location.  Otherwise, "Start" is the location of the
230459232d35f5820e334b6c8b007ae8006f4390055dChris Lattner/// 'delete' token.
230559232d35f5820e334b6c8b007ae8006f4390055dChris Lattner///
23064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///        delete-expression:
23074c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' cast-expression
23084c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl///                   '::'[opt] 'delete' '[' ']' cast-expression
230960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
231059232d35f5820e334b6c8b007ae8006f4390055dChris LattnerParser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
231159232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
231259232d35f5820e334b6c8b007ae8006f4390055dChris Lattner  ConsumeToken(); // Consume 'delete'
23134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
23144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Array delete?
23154c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  bool ArrayDelete = false;
23164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  if (Tok.is(tok::l_square)) {
23174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    ArrayDelete = true;
23184a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_square);
23194a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
23204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
23214a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
23224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
232320df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl      return ExprError();
23244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
23254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
232660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Operand(ParseCastExpression(false));
23270e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Operand.isInvalid())
232820df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl    return move(Operand);
23294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
23309ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
23314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
233264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
23331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
233464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch(kind) {
2335b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a known unary type trait.");
233664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign;
233764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
233820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___has_nothrow_copy:           return UTT_HasNothrowCopy;
233964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign;
2340023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  case tok::kw___has_trivial_constructor:
2341023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                    return UTT_HasTrivialDefaultConstructor;
234220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___has_trivial_copy:           return UTT_HasTrivialCopy;
234364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor;
234464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor;
234564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_abstract:             return UTT_IsAbstract;
234620c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_arithmetic:              return UTT_IsArithmetic;
234720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_array:                   return UTT_IsArray;
234864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_class:                return UTT_IsClass;
234920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_complete_type:           return UTT_IsCompleteType;
235020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_compound:                return UTT_IsCompound;
235120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_const:                   return UTT_IsConst;
235264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_empty:                return UTT_IsEmpty;
235364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_enum:                 return UTT_IsEnum;
23545e9392ba18f5925e26cc5714d1412eda0d219826Douglas Gregor  case tok::kw___is_final:                 return UTT_IsFinal;
235520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_floating_point:          return UTT_IsFloatingPoint;
235620c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_function:                return UTT_IsFunction;
235720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_fundamental:             return UTT_IsFundamental;
235820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_integral:                return UTT_IsIntegral;
235920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_lvalue_reference:        return UTT_IsLvalueReference;
236020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
236120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_object_pointer:   return UTT_IsMemberObjectPointer;
236220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_member_pointer:          return UTT_IsMemberPointer;
236320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_object:                  return UTT_IsObject;
23644e61ddd644e9c6293697a966d98d7c1905cf63a8Chandler Carruth  case tok::kw___is_literal:              return UTT_IsLiteral;
23653840281126e7d10552c55f6fd8b1ec9483898906Chandler Carruth  case tok::kw___is_literal_type:         return UTT_IsLiteral;
236664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_pod:                  return UTT_IsPOD;
236720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_pointer:                 return UTT_IsPointer;
236864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_polymorphic:          return UTT_IsPolymorphic;
236920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_reference:               return UTT_IsReference;
237020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_rvalue_reference:        return UTT_IsRvalueReference;
237120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_scalar:                  return UTT_IsScalar;
237220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_signed:                  return UTT_IsSigned;
237320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_standard_layout:         return UTT_IsStandardLayout;
237420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_trivial:                 return UTT_IsTrivial;
2375feb375d31b7e9108b04a9f55b721d5e0c793a558Sean Hunt  case tok::kw___is_trivially_copyable:      return UTT_IsTriviallyCopyable;
237664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case tok::kw___is_union:                return UTT_IsUnion;
237720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_unsigned:                return UTT_IsUnsigned;
237820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_void:                    return UTT_IsVoid;
237920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_volatile:                return UTT_IsVolatile;
238064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
23816ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
23826ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
23836ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichetstatic BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
23846ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  switch(kind) {
238538c2b730a8553fa1cf369d0c5567f8b5d0a3dda8Francois Pichet  default: llvm_unreachable("Not a known binary type trait");
2386f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case tok::kw___is_base_of:                 return BTT_IsBaseOf;
238720c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_convertible:             return BTT_IsConvertible;
238820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case tok::kw___is_same:                    return BTT_IsSame;
2389f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
23909f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor  case tok::kw___is_convertible_to:          return BTT_IsConvertibleTo;
23916ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
239264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
239364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
239421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegleystatic ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
239521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch(kind) {
239621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  default: llvm_unreachable("Not a known binary type trait");
239721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case tok::kw___array_rank:                 return ATT_ArrayRank;
239821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case tok::kw___array_extent:               return ATT_ArrayExtent;
239921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
240021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
240121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
2402552622067dc45013d240f73952fece703f5e63bdJohn Wiegleystatic ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2403552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  switch(kind) {
2404b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a known unary expression trait.");
2405552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case tok::kw___is_lvalue_expr:             return ET_IsLValueExpr;
2406552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case tok::kw___is_rvalue_expr:             return ET_IsRValueExpr;
2407552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  }
2408552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
2409552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
241064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
241164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// pseudo-functions that allow implementation of the TR1/C++0x type traits
241264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// templates.
241364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
241464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///       primary-expression:
241564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl/// [GNU]             unary-type-trait '(' type-id ')'
241664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl///
241760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseUnaryTypeTrait() {
241864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
241964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  SourceLocation Loc = ConsumeToken();
242064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
24214a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
24224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
242364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return ExprError();
242464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
242564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
242664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // there will be cryptic errors about mismatched parentheses and missing
242764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // specifiers.
2428809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
242964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
24304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
243164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
2432809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2433809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2434809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
24354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
243664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
2437f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
24386ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
24396ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// pseudo-functions that allow implementation of the TR1/C++0x type traits
24406ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// templates.
24416ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///
24426ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///       primary-expression:
24436ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet/// [GNU]             binary-type-trait '(' type-id ',' type-id ')'
24446ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet///
24456ad6f2848d7652ab2991286eb48be440d3493b28Francois PichetExprResult Parser::ParseBinaryTypeTrait() {
24466ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
24476ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  SourceLocation Loc = ConsumeToken();
24486ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
24494a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
24504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
24516ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
24526ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
24536ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeResult LhsTy = ParseTypeName();
24546ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (LhsTy.isInvalid()) {
24556ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
24566ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
24576ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
24586ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
24596ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
24606ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
24616ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
24626ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
24636ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
24646ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeResult RhsTy = ParseTypeName();
24656ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (RhsTy.isInvalid()) {
24666ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    SkipUntil(tok::r_paren);
24676ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return ExprError();
24686ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
24696ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
24704a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
24716ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
24724a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
24734a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      T.getCloseLocation());
24746ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
24756ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
247621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// ParseArrayTypeTrait - Parse the built-in array type-trait
247721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// pseudo-functions.
247821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///
247921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///       primary-expression:
248021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// [Embarcadero]     '__array_rank' '(' type-id ')'
248121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley/// [Embarcadero]     '__array_extent' '(' type-id ',' expression ')'
248221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley///
248321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John WiegleyExprResult Parser::ParseArrayTypeTrait() {
248421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
248521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  SourceLocation Loc = ConsumeToken();
248621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
24874a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
24884a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
248921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return ExprError();
249021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
249121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  TypeResult Ty = ParseTypeName();
249221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  if (Ty.isInvalid()) {
249321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    SkipUntil(tok::comma);
249421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    SkipUntil(tok::r_paren);
249521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return ExprError();
249621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
249721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
249821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch (ATT) {
249921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayRank: {
25004a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
25014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
25024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
250321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
250421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayExtent: {
250521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
250621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      SkipUntil(tok::r_paren);
250721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      return ExprError();
250821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    }
250921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
251021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    ExprResult DimExpr = ParseExpression();
25114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
251221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
25134a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
25144a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                       T.getCloseLocation());
251521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
251621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
25173026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid ArrayTypeTrait!");
251821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
251921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
2520552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// ParseExpressionTrait - Parse built-in expression-trait
2521552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// pseudo-functions like __is_lvalue_expr( xxx ).
2522552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///
2523552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///       primary-expression:
2524552622067dc45013d240f73952fece703f5e63bdJohn Wiegley/// [Embarcadero]     expression-trait '(' expression ')'
2525552622067dc45013d240f73952fece703f5e63bdJohn Wiegley///
2526552622067dc45013d240f73952fece703f5e63bdJohn WiegleyExprResult Parser::ParseExpressionTrait() {
2527552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2528552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  SourceLocation Loc = ConsumeToken();
2529552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
25304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
25314a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.expectAndConsume(diag::err_expected_lparen))
2532552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    return ExprError();
2533552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2534552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  ExprResult Expr = ParseExpression();
2535552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
25364a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
2537552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
25384a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
25394a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                      T.getCloseLocation());
2540552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
2541552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2542552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
2543f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2544f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2545f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis/// based on the context past the parens.
254660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2547f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios KyrtzidisParser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
2548b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                         ParsedType &CastTy,
25494a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                         BalancedDelimiterTracker &Tracker) {
2550f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(getLang().CPlusPlus && "Should only be called for C++!");
2551f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2552f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  assert(isTypeIdInParens() && "Not a type-id!");
2553f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
255460d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(true);
2555b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  CastTy = ParsedType();
2556f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2557f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // We need to disambiguate a very ugly part of the C++ syntax:
2558f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2559f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())x;  - type-id
2560f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())*x; - type-id
2561f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T())/x; - expression
2562f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // (T());   - expression
2563f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2564f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The bad news is that we cannot use the specialized tentative parser, since
2565f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // it can only verify that the thing inside the parens can be parsed as
2566f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // type-id, it is not useful for determining the context past the parens.
2567f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  //
2568f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // The good news is that the parser can disambiguate this part without
2569a558a897cbe83a21914058348ffbdcf827530ad4Argyrios Kyrtzidis  // making any unnecessary Action calls.
2570f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  //
2571f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // It uses a scheme similar to parsing inline methods. The parenthesized
2572f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // tokens are cached, the context that follows is determined (possibly by
2573f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parsing a cast-expression), and then we re-introduce the cached tokens
2574f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // into the token stream and parse them appropriately.
2575f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
25761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParenParseOption ParseAs;
2577f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  CachedTokens Toks;
2578f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2579f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Store the tokens of the parentheses. We will parse them after we determine
2580f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // the context that follows them.
258114b91628961ab50cc6e724bbcd408fdee100662dArgyrios Kyrtzidis  if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
2582f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We didn't find the ')' we expected.
25834a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Tracker.consumeClose();
2584f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
2585f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
2586f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2587f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Tok.is(tok::l_brace)) {
2588f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = CompoundLiteral;
2589f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  } else {
2590f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    bool NotCastExpr;
2591b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2592b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2593b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      NotCastExpr = true;
2594b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    } else {
2595b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // Try parsing the cast-expression that may follow.
2596b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // If it is not a cast-expression, NotCastExpr will be true and no token
2597b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      // will be consumed.
2598b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman      Result = ParseCastExpression(false/*isUnaryExpression*/,
2599b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman                                   false/*isAddressofOperand*/,
2600b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   NotCastExpr,
26010a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis                                   // type-id has priority.
2602cd78e612d6fa8e214e6a6bb0e739a0c3e419df91Kaelyn Uhrain                                   IsTypeCast);
2603b53f08ac87f1e1f4bc2fbfa4560c2183a82020eeEli Friedman    }
2604f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2605f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // If we parsed a cast-expression, it's really a type-id, otherwise it's
2606f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // an expression.
2607f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
2608f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
2609f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
26101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The current token should go after the cached tokens.
2611f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Toks.push_back(Tok);
2612f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Re-enter the stored parenthesized tokens into the token stream, so we may
2613f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // parse them now.
2614f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  PP.EnterTokenStream(Toks.data(), Toks.size(),
2615f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2616f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Drop the current token and bring the first cached one. It's the same token
2617f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // as when we entered this function.
2618f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  ConsumeAnyToken();
2619f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2620f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  if (ParseAs >= CompoundLiteral) {
26210a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    // Parse the type declarator.
26220a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    DeclSpec DS(AttrFactory);
26230a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    ParseSpecifierQualifierList(DS);
26240a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
26250a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    ParseDeclarator(DeclaratorInfo);
2626f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2627f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Match the ')'.
26284a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Tracker.consumeClose();
2629f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
2630f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    if (ParseAs == CompoundLiteral) {
2631f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      ExprType = CompoundLiteral;
26320a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis      TypeResult Ty = ParseTypeName();
26334a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor       return ParseCompoundLiteralExpression(Ty.get(),
26344a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Tracker.getOpenLocation(),
26354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            Tracker.getCloseLocation());
2636f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    }
26371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2638f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2639f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    assert(ParseAs == CastExpr);
2640f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis
26410a85183be6930571f3af8e5a976d24c3f95e5b25Argyrios Kyrtzidis    if (DeclaratorInfo.isInvalidType())
2642f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis      return ExprError();
2643f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2644f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis    // Result is what ParseCastExpression returned earlier.
2645f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    if (!Result.isInvalid())
26464a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
26474a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    DeclaratorInfo, CastTy,
26484a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    Tracker.getCloseLocation(), Result.take());
2649f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return move(Result);
2650f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
26511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2652f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  // Not a compound literal, and not followed by a cast-expression.
2653f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  assert(ParseAs == SimpleExpr);
2654f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2655f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  ExprType = SimpleExpr;
2656f40882a38baf258fa10e362003f6939a590074bbArgyrios Kyrtzidis  Result = ParseExpression();
2657f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (!Result.isInvalid() && Tok.is(tok::r_paren))
26584a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
26594a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                    Tok.getLocation(), Result.take());
2660f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis
2661f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  // Match the ')'.
2662f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  if (Result.isInvalid()) {
2663f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    SkipUntil(tok::r_paren);
2664f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis    return ExprError();
2665f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  }
26661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26674a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Tracker.consumeClose();
2668f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis  return move(Result);
2669f58f45e6d76792df8c643ce1c6d364dce5db4826Argyrios Kyrtzidis}
2670