ParseExprCXX.cpp revision 950be71c745409e373ae8a834490f9026c8ac222
1//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/ParseDiagnostic.h"
15#include "clang/Parse/Parser.h"
16#include "RAIIObjectsForParser.h"
17#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
20#include "llvm/Support/ErrorHandling.h"
21
22using namespace clang;
23
24static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
25  switch (Kind) {
26    case tok::kw_template:         return 0;
27    case tok::kw_const_cast:       return 1;
28    case tok::kw_dynamic_cast:     return 2;
29    case tok::kw_reinterpret_cast: return 3;
30    case tok::kw_static_cast:      return 4;
31    default:
32      assert(0 && "Unknown type for digraph error message.");
33      return -1;
34  }
35}
36
37// Are the two tokens adjacent in the same source file?
38static bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
39  SourceManager &SM = PP.getSourceManager();
40  SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
41  SourceLocation FirstEnd = FirstLoc.getFileLocWithOffset(First.getLength());
42  return FirstEnd == SM.getSpellingLoc(Second.getLocation());
43}
44
45// Suggest fixit for "<::" after a cast.
46static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
47                       Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
48  // Pull '<:' and ':' off token stream.
49  if (!AtDigraph)
50    PP.Lex(DigraphToken);
51  PP.Lex(ColonToken);
52
53  SourceRange Range;
54  Range.setBegin(DigraphToken.getLocation());
55  Range.setEnd(ColonToken.getLocation());
56  P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
57      << SelectDigraphErrorMessage(Kind)
58      << FixItHint::CreateReplacement(Range, "< ::");
59
60  // Update token information to reflect their change in token type.
61  ColonToken.setKind(tok::coloncolon);
62  ColonToken.setLocation(ColonToken.getLocation().getFileLocWithOffset(-1));
63  ColonToken.setLength(2);
64  DigraphToken.setKind(tok::less);
65  DigraphToken.setLength(1);
66
67  // Push new tokens back to token stream.
68  PP.EnterToken(ColonToken);
69  if (!AtDigraph)
70    PP.EnterToken(DigraphToken);
71}
72
73// Check for '<::' which should be '< ::' instead of '[:' when following
74// a template name.
75void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
76                                        bool EnteringContext,
77                                        IdentifierInfo &II, CXXScopeSpec &SS) {
78  if (!Next.is(tok::l_square) || !Next.getLength() == 2)
79    return;
80
81  Token SecondToken = GetLookAheadToken(2);
82  if (!SecondToken.is(tok::colon) || !AreTokensAdjacent(PP, Next, SecondToken))
83    return;
84
85  TemplateTy Template;
86  UnqualifiedId TemplateName;
87  TemplateName.setIdentifier(&II, Tok.getLocation());
88  bool MemberOfUnknownSpecialization;
89  if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
90                              TemplateName, ObjectType, EnteringContext,
91                              Template, MemberOfUnknownSpecialization))
92    return;
93
94  FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
95             /*AtDigraph*/false);
96}
97
98/// \brief Parse global scope or nested-name-specifier if present.
99///
100/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
101/// may be preceded by '::'). Note that this routine will not parse ::new or
102/// ::delete; it will just leave them in the token stream.
103///
104///       '::'[opt] nested-name-specifier
105///       '::'
106///
107///       nested-name-specifier:
108///         type-name '::'
109///         namespace-name '::'
110///         nested-name-specifier identifier '::'
111///         nested-name-specifier 'template'[opt] simple-template-id '::'
112///
113///
114/// \param SS the scope specifier that will be set to the parsed
115/// nested-name-specifier (or empty)
116///
117/// \param ObjectType if this nested-name-specifier is being parsed following
118/// the "." or "->" of a member access expression, this parameter provides the
119/// type of the object whose members are being accessed.
120///
121/// \param EnteringContext whether we will be entering into the context of
122/// the nested-name-specifier after parsing it.
123///
124/// \param MayBePseudoDestructor When non-NULL, points to a flag that
125/// indicates whether this nested-name-specifier may be part of a
126/// pseudo-destructor name. In this case, the flag will be set false
127/// if we don't actually end up parsing a destructor name. Moreorover,
128/// if we do end up determining that we are parsing a destructor name,
129/// the last component of the nested-name-specifier is not parsed as
130/// part of the scope specifier.
131
132/// member access expression, e.g., the \p T:: in \p p->T::m.
133///
134/// \returns true if there was an error parsing a scope specifier
135bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
136                                            ParsedType ObjectType,
137                                            bool EnteringContext,
138                                            bool *MayBePseudoDestructor,
139                                            bool IsTypename) {
140  assert(getLang().CPlusPlus &&
141         "Call sites of this function should be guarded by checking for C++");
142
143  if (Tok.is(tok::annot_cxxscope)) {
144    Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
145                                                 Tok.getAnnotationRange(),
146                                                 SS);
147    ConsumeToken();
148    return false;
149  }
150
151  bool HasScopeSpecifier = false;
152
153  if (Tok.is(tok::coloncolon)) {
154    // ::new and ::delete aren't nested-name-specifiers.
155    tok::TokenKind NextKind = NextToken().getKind();
156    if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
157      return false;
158
159    // '::' - Global scope qualifier.
160    if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
161      return true;
162
163    HasScopeSpecifier = true;
164  }
165
166  bool CheckForDestructor = false;
167  if (MayBePseudoDestructor && *MayBePseudoDestructor) {
168    CheckForDestructor = true;
169    *MayBePseudoDestructor = false;
170  }
171
172  while (true) {
173    if (HasScopeSpecifier) {
174      // C++ [basic.lookup.classref]p5:
175      //   If the qualified-id has the form
176      //
177      //       ::class-name-or-namespace-name::...
178      //
179      //   the class-name-or-namespace-name is looked up in global scope as a
180      //   class-name or namespace-name.
181      //
182      // To implement this, we clear out the object type as soon as we've
183      // seen a leading '::' or part of a nested-name-specifier.
184      ObjectType = ParsedType();
185
186      if (Tok.is(tok::code_completion)) {
187        // Code completion for a nested-name-specifier, where the code
188        // code completion token follows the '::'.
189        Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
190        // Include code completion token into the range of the scope otherwise
191        // when we try to annotate the scope tokens the dangling code completion
192        // token will cause assertion in
193        // Preprocessor::AnnotatePreviousCachedTokens.
194        SS.setEndLoc(Tok.getLocation());
195        cutOffParsing();
196        return true;
197      }
198    }
199
200    // nested-name-specifier:
201    //   nested-name-specifier 'template'[opt] simple-template-id '::'
202
203    // Parse the optional 'template' keyword, then make sure we have
204    // 'identifier <' after it.
205    if (Tok.is(tok::kw_template)) {
206      // If we don't have a scope specifier or an object type, this isn't a
207      // nested-name-specifier, since they aren't allowed to start with
208      // 'template'.
209      if (!HasScopeSpecifier && !ObjectType)
210        break;
211
212      TentativeParsingAction TPA(*this);
213      SourceLocation TemplateKWLoc = ConsumeToken();
214
215      UnqualifiedId TemplateName;
216      if (Tok.is(tok::identifier)) {
217        // Consume the identifier.
218        TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
219        ConsumeToken();
220      } else if (Tok.is(tok::kw_operator)) {
221        if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
222                                       TemplateName)) {
223          TPA.Commit();
224          break;
225        }
226
227        if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
228            TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
229          Diag(TemplateName.getSourceRange().getBegin(),
230               diag::err_id_after_template_in_nested_name_spec)
231            << TemplateName.getSourceRange();
232          TPA.Commit();
233          break;
234        }
235      } else {
236        TPA.Revert();
237        break;
238      }
239
240      // If the next token is not '<', we have a qualified-id that refers
241      // to a template name, such as T::template apply, but is not a
242      // template-id.
243      if (Tok.isNot(tok::less)) {
244        TPA.Revert();
245        break;
246      }
247
248      // Commit to parsing the template-id.
249      TPA.Commit();
250      TemplateTy Template;
251      if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
252                                                                TemplateKWLoc,
253                                                                    SS,
254                                                                  TemplateName,
255                                                                    ObjectType,
256                                                                EnteringContext,
257                                                                    Template)) {
258        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
259                                    TemplateKWLoc, false))
260          return true;
261      } else
262        return true;
263
264      continue;
265    }
266
267    if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
268      // We have
269      //
270      //   simple-template-id '::'
271      //
272      // So we need to check whether the simple-template-id is of the
273      // right kind (it should name a type or be dependent), and then
274      // convert it into a type within the nested-name-specifier.
275      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
276      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
277        *MayBePseudoDestructor = true;
278        return false;
279      }
280
281      // Consume the template-id token.
282      ConsumeToken();
283
284      assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
285      SourceLocation CCLoc = ConsumeToken();
286
287      if (!HasScopeSpecifier)
288        HasScopeSpecifier = true;
289
290      ASTTemplateArgsPtr TemplateArgsPtr(Actions,
291                                         TemplateId->getTemplateArgs(),
292                                         TemplateId->NumArgs);
293
294      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
295                                              /*FIXME:*/SourceLocation(),
296                                              SS,
297                                              TemplateId->Template,
298                                              TemplateId->TemplateNameLoc,
299                                              TemplateId->LAngleLoc,
300                                              TemplateArgsPtr,
301                                              TemplateId->RAngleLoc,
302                                              CCLoc,
303                                              EnteringContext)) {
304        SourceLocation StartLoc
305          = SS.getBeginLoc().isValid()? SS.getBeginLoc()
306                                      : TemplateId->TemplateNameLoc;
307        SS.SetInvalid(SourceRange(StartLoc, CCLoc));
308      }
309
310      continue;
311    }
312
313
314    // The rest of the nested-name-specifier possibilities start with
315    // tok::identifier.
316    if (Tok.isNot(tok::identifier))
317      break;
318
319    IdentifierInfo &II = *Tok.getIdentifierInfo();
320
321    // nested-name-specifier:
322    //   type-name '::'
323    //   namespace-name '::'
324    //   nested-name-specifier identifier '::'
325    Token Next = NextToken();
326
327    // If we get foo:bar, this is almost certainly a typo for foo::bar.  Recover
328    // and emit a fixit hint for it.
329    if (Next.is(tok::colon) && !ColonIsSacred) {
330      if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
331                                            Tok.getLocation(),
332                                            Next.getLocation(), ObjectType,
333                                            EnteringContext) &&
334          // If the token after the colon isn't an identifier, it's still an
335          // error, but they probably meant something else strange so don't
336          // recover like this.
337          PP.LookAhead(1).is(tok::identifier)) {
338        Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
339          << FixItHint::CreateReplacement(Next.getLocation(), "::");
340
341        // Recover as if the user wrote '::'.
342        Next.setKind(tok::coloncolon);
343      }
344    }
345
346    if (Next.is(tok::coloncolon)) {
347      if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
348          !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
349                                                II, ObjectType)) {
350        *MayBePseudoDestructor = true;
351        return false;
352      }
353
354      // We have an identifier followed by a '::'. Lookup this name
355      // as the name in a nested-name-specifier.
356      SourceLocation IdLoc = ConsumeToken();
357      assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
358             "NextToken() not working properly!");
359      SourceLocation CCLoc = ConsumeToken();
360
361      HasScopeSpecifier = true;
362      if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
363                                              ObjectType, EnteringContext, SS))
364        SS.SetInvalid(SourceRange(IdLoc, CCLoc));
365
366      continue;
367    }
368
369    CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
370
371    // nested-name-specifier:
372    //   type-name '<'
373    if (Next.is(tok::less)) {
374      TemplateTy Template;
375      UnqualifiedId TemplateName;
376      TemplateName.setIdentifier(&II, Tok.getLocation());
377      bool MemberOfUnknownSpecialization;
378      if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
379                                              /*hasTemplateKeyword=*/false,
380                                                        TemplateName,
381                                                        ObjectType,
382                                                        EnteringContext,
383                                                        Template,
384                                              MemberOfUnknownSpecialization)) {
385        // We have found a template name, so annotate this this token
386        // with a template-id annotation. We do not permit the
387        // template-id to be translated into a type annotation,
388        // because some clients (e.g., the parsing of class template
389        // specializations) still want to see the original template-id
390        // token.
391        ConsumeToken();
392        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
393                                    SourceLocation(), false))
394          return true;
395        continue;
396      }
397
398      if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
399          (IsTypename || IsTemplateArgumentList(1))) {
400        // We have something like t::getAs<T>, where getAs is a
401        // member of an unknown specialization. However, this will only
402        // parse correctly as a template, so suggest the keyword 'template'
403        // before 'getAs' and treat this as a dependent template name.
404        unsigned DiagID = diag::err_missing_dependent_template_keyword;
405        if (getLang().MicrosoftExt)
406          DiagID = diag::warn_missing_dependent_template_keyword;
407
408        Diag(Tok.getLocation(), DiagID)
409          << II.getName()
410          << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
411
412        if (TemplateNameKind TNK
413              = Actions.ActOnDependentTemplateName(getCurScope(),
414                                                   Tok.getLocation(), SS,
415                                                   TemplateName, ObjectType,
416                                                   EnteringContext, Template)) {
417          // Consume the identifier.
418          ConsumeToken();
419          if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
420                                      SourceLocation(), false))
421            return true;
422        }
423        else
424          return true;
425
426        continue;
427      }
428    }
429
430    // We don't have any tokens that form the beginning of a
431    // nested-name-specifier, so we're done.
432    break;
433  }
434
435  // Even if we didn't see any pieces of a nested-name-specifier, we
436  // still check whether there is a tilde in this position, which
437  // indicates a potential pseudo-destructor.
438  if (CheckForDestructor && Tok.is(tok::tilde))
439    *MayBePseudoDestructor = true;
440
441  return false;
442}
443
444/// ParseCXXIdExpression - Handle id-expression.
445///
446///       id-expression:
447///         unqualified-id
448///         qualified-id
449///
450///       qualified-id:
451///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
452///         '::' identifier
453///         '::' operator-function-id
454///         '::' template-id
455///
456/// NOTE: The standard specifies that, for qualified-id, the parser does not
457/// expect:
458///
459///   '::' conversion-function-id
460///   '::' '~' class-name
461///
462/// This may cause a slight inconsistency on diagnostics:
463///
464/// class C {};
465/// namespace A {}
466/// void f() {
467///   :: A :: ~ C(); // Some Sema error about using destructor with a
468///                  // namespace.
469///   :: ~ C(); // Some Parser error like 'unexpected ~'.
470/// }
471///
472/// We simplify the parser a bit and make it work like:
473///
474///       qualified-id:
475///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
476///         '::' unqualified-id
477///
478/// That way Sema can handle and report similar errors for namespaces and the
479/// global scope.
480///
481/// The isAddressOfOperand parameter indicates that this id-expression is a
482/// direct operand of the address-of operator. This is, besides member contexts,
483/// the only place where a qualified-id naming a non-static class member may
484/// appear.
485///
486ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
487  // qualified-id:
488  //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
489  //   '::' unqualified-id
490  //
491  CXXScopeSpec SS;
492  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
493
494  UnqualifiedId Name;
495  if (ParseUnqualifiedId(SS,
496                         /*EnteringContext=*/false,
497                         /*AllowDestructorName=*/false,
498                         /*AllowConstructorName=*/false,
499                         /*ObjectType=*/ ParsedType(),
500                         Name))
501    return ExprError();
502
503  // This is only the direct operand of an & operator if it is not
504  // followed by a postfix-expression suffix.
505  if (isAddressOfOperand && isPostfixExpressionSuffixStart())
506    isAddressOfOperand = false;
507
508  return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
509                                   isAddressOfOperand);
510
511}
512
513/// ParseLambdaExpression - Parse a C++0x lambda expression.
514///
515///       lambda-expression:
516///         lambda-introducer lambda-declarator[opt] compound-statement
517///
518///       lambda-introducer:
519///         '[' lambda-capture[opt] ']'
520///
521///       lambda-capture:
522///         capture-default
523///         capture-list
524///         capture-default ',' capture-list
525///
526///       capture-default:
527///         '&'
528///         '='
529///
530///       capture-list:
531///         capture
532///         capture-list ',' capture
533///
534///       capture:
535///         identifier
536///         '&' identifier
537///         'this'
538///
539///       lambda-declarator:
540///         '(' parameter-declaration-clause ')' attribute-specifier[opt]
541///           'mutable'[opt] exception-specification[opt]
542///           trailing-return-type[opt]
543///
544ExprResult Parser::ParseLambdaExpression() {
545  // Parse lambda-introducer.
546  LambdaIntroducer Intro;
547
548  llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
549  if (DiagID) {
550    Diag(Tok, DiagID.getValue());
551    SkipUntil(tok::r_square);
552  }
553
554  return ParseLambdaExpressionAfterIntroducer(Intro);
555}
556
557/// TryParseLambdaExpression - Use lookahead and potentially tentative
558/// parsing to determine if we are looking at a C++0x lambda expression, and parse
559/// it if we are.
560///
561/// If we are not looking at a lambda expression, returns ExprError().
562ExprResult Parser::TryParseLambdaExpression() {
563  assert(getLang().CPlusPlus0x
564         && Tok.is(tok::l_square)
565         && "Not at the start of a possible lambda expression.");
566
567  const Token Next = NextToken(), After = GetLookAheadToken(2);
568
569  // If lookahead indicates this is a lambda...
570  if (Next.is(tok::r_square) ||     // []
571      Next.is(tok::equal) ||        // [=
572      (Next.is(tok::amp) &&         // [&] or [&,
573       (After.is(tok::r_square) ||
574        After.is(tok::comma))) ||
575      (Next.is(tok::identifier) &&  // [identifier]
576       After.is(tok::r_square))) {
577    return ParseLambdaExpression();
578  }
579
580  // If lookahead indicates this is an Objective-C message...
581  if (Next.is(tok::identifier) && After.is(tok::identifier)) {
582    return ExprError();
583  }
584
585  LambdaIntroducer Intro;
586  if (TryParseLambdaIntroducer(Intro))
587    return ExprError();
588  return ParseLambdaExpressionAfterIntroducer(Intro);
589}
590
591/// ParseLambdaExpression - Parse a lambda introducer.
592///
593/// Returns a DiagnosticID if it hit something unexpected.
594llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
595  typedef llvm::Optional<unsigned> DiagResult;
596
597  assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
598  Intro.Range.setBegin(ConsumeBracket());
599
600  bool first = true;
601
602  // Parse capture-default.
603  if (Tok.is(tok::amp) &&
604      (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
605    Intro.Default = LCD_ByRef;
606    ConsumeToken();
607    first = false;
608  } else if (Tok.is(tok::equal)) {
609    Intro.Default = LCD_ByCopy;
610    ConsumeToken();
611    first = false;
612  }
613
614  while (Tok.isNot(tok::r_square)) {
615    if (!first) {
616      if (Tok.isNot(tok::comma))
617        return DiagResult(diag::err_expected_comma_or_rsquare);
618      ConsumeToken();
619    }
620
621    first = false;
622
623    // Parse capture.
624    LambdaCaptureKind Kind = LCK_ByCopy;
625    SourceLocation Loc;
626    IdentifierInfo* Id = 0;
627
628    if (Tok.is(tok::kw_this)) {
629      Kind = LCK_This;
630      Loc = ConsumeToken();
631    } else {
632      if (Tok.is(tok::amp)) {
633        Kind = LCK_ByRef;
634        ConsumeToken();
635      }
636
637      if (Tok.is(tok::identifier)) {
638        Id = Tok.getIdentifierInfo();
639        Loc = ConsumeToken();
640      } else if (Tok.is(tok::kw_this)) {
641        // FIXME: If we want to suggest a fixit here, will need to return more
642        // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
643        // Clear()ed to prevent emission in case of tentative parsing?
644        return DiagResult(diag::err_this_captured_by_reference);
645      } else {
646        return DiagResult(diag::err_expected_capture);
647      }
648    }
649
650    Intro.addCapture(Kind, Loc, Id);
651  }
652
653  Intro.Range.setEnd(MatchRHSPunctuation(tok::r_square,
654                                         Intro.Range.getBegin()));
655
656  return DiagResult();
657}
658
659/// TryParseLambdaExpression - Tentatively parse a lambda introducer.
660///
661/// Returns true if it hit something unexpected.
662bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
663  TentativeParsingAction PA(*this);
664
665  llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
666
667  if (DiagID) {
668    PA.Revert();
669    return true;
670  }
671
672  PA.Commit();
673  return false;
674}
675
676/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
677/// expression.
678ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
679                     LambdaIntroducer &Intro) {
680  // Parse lambda-declarator[opt].
681  DeclSpec DS(AttrFactory);
682  Declarator D(DS, Declarator::PrototypeContext);
683
684  if (Tok.is(tok::l_paren)) {
685    ParseScope PrototypeScope(this,
686                              Scope::FunctionPrototypeScope |
687                              Scope::DeclScope);
688
689    SourceLocation DeclLoc = ConsumeParen(), DeclEndLoc;
690
691    // Parse parameter-declaration-clause.
692    ParsedAttributes Attr(AttrFactory);
693    llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
694    SourceLocation EllipsisLoc;
695
696    if (Tok.isNot(tok::r_paren))
697      ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
698
699    DeclEndLoc = MatchRHSPunctuation(tok::r_paren, DeclLoc);
700
701    // Parse 'mutable'[opt].
702    SourceLocation MutableLoc;
703    if (Tok.is(tok::kw_mutable)) {
704      MutableLoc = ConsumeToken();
705      DeclEndLoc = MutableLoc;
706    }
707
708    // Parse exception-specification[opt].
709    ExceptionSpecificationType ESpecType = EST_None;
710    SourceRange ESpecRange;
711    llvm::SmallVector<ParsedType, 2> DynamicExceptions;
712    llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
713    ExprResult NoexceptExpr;
714    ESpecType = MaybeParseExceptionSpecification(ESpecRange,
715                                                 DynamicExceptions,
716                                                 DynamicExceptionRanges,
717                                                 NoexceptExpr);
718
719    if (ESpecType != EST_None)
720      DeclEndLoc = ESpecRange.getEnd();
721
722    // Parse attribute-specifier[opt].
723    MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
724
725    // Parse trailing-return-type[opt].
726    ParsedType TrailingReturnType;
727    if (Tok.is(tok::arrow)) {
728      SourceRange Range;
729      TrailingReturnType = ParseTrailingReturnType(Range).get();
730      if (Range.getEnd().isValid())
731        DeclEndLoc = Range.getEnd();
732    }
733
734    PrototypeScope.Exit();
735
736    D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
737                                           /*isVariadic=*/EllipsisLoc.isValid(),
738                                           EllipsisLoc,
739                                           ParamInfo.data(), ParamInfo.size(),
740                                           DS.getTypeQualifiers(),
741                                           /*RefQualifierIsLValueRef=*/true,
742                                           /*RefQualifierLoc=*/SourceLocation(),
743                                           MutableLoc,
744                                           ESpecType, ESpecRange.getBegin(),
745                                           DynamicExceptions.data(),
746                                           DynamicExceptionRanges.data(),
747                                           DynamicExceptions.size(),
748                                           NoexceptExpr.isUsable() ?
749                                             NoexceptExpr.get() : 0,
750                                           DeclLoc, DeclEndLoc, D,
751                                           TrailingReturnType),
752                  Attr, DeclEndLoc);
753  }
754
755  // Parse compound-statement.
756  if (Tok.is(tok::l_brace)) {
757    // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
758    // it.
759    ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
760                               Scope::BreakScope | Scope::ContinueScope |
761                               Scope::DeclScope);
762
763    StmtResult Stmt(ParseCompoundStatementBody());
764
765    BodyScope.Exit();
766  } else {
767    Diag(Tok, diag::err_expected_lambda_body);
768  }
769
770  return ExprEmpty();
771}
772
773/// ParseCXXCasts - This handles the various ways to cast expressions to another
774/// type.
775///
776///       postfix-expression: [C++ 5.2p1]
777///         'dynamic_cast' '<' type-name '>' '(' expression ')'
778///         'static_cast' '<' type-name '>' '(' expression ')'
779///         'reinterpret_cast' '<' type-name '>' '(' expression ')'
780///         'const_cast' '<' type-name '>' '(' expression ')'
781///
782ExprResult Parser::ParseCXXCasts() {
783  tok::TokenKind Kind = Tok.getKind();
784  const char *CastName = 0;     // For error messages
785
786  switch (Kind) {
787  default: assert(0 && "Unknown C++ cast!"); abort();
788  case tok::kw_const_cast:       CastName = "const_cast";       break;
789  case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break;
790  case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
791  case tok::kw_static_cast:      CastName = "static_cast";      break;
792  }
793
794  SourceLocation OpLoc = ConsumeToken();
795  SourceLocation LAngleBracketLoc = Tok.getLocation();
796
797  // Check for "<::" which is parsed as "[:".  If found, fix token stream,
798  // diagnose error, suggest fix, and recover parsing.
799  Token Next = NextToken();
800  if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
801      AreTokensAdjacent(PP, Tok, Next))
802    FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
803
804  if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
805    return ExprError();
806
807  // Parse the common declaration-specifiers piece.
808  DeclSpec DS(AttrFactory);
809  ParseSpecifierQualifierList(DS);
810
811  // Parse the abstract-declarator, if present.
812  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
813  ParseDeclarator(DeclaratorInfo);
814
815  SourceLocation RAngleBracketLoc = Tok.getLocation();
816
817  if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
818    return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
819
820  SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
821
822  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
823    return ExprError();
824
825  ExprResult Result = ParseExpression();
826
827  // Match the ')'.
828  RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
829
830  if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
831    Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
832                                       LAngleBracketLoc, DeclaratorInfo,
833                                       RAngleBracketLoc,
834                                       LParenLoc, Result.take(), RParenLoc);
835
836  return move(Result);
837}
838
839/// ParseCXXTypeid - This handles the C++ typeid expression.
840///
841///       postfix-expression: [C++ 5.2p1]
842///         'typeid' '(' expression ')'
843///         'typeid' '(' type-id ')'
844///
845ExprResult Parser::ParseCXXTypeid() {
846  assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
847
848  SourceLocation OpLoc = ConsumeToken();
849  SourceLocation LParenLoc = Tok.getLocation();
850  SourceLocation RParenLoc;
851
852  // typeid expressions are always parenthesized.
853  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
854      "typeid"))
855    return ExprError();
856
857  ExprResult Result;
858
859  if (isTypeIdInParens()) {
860    TypeResult Ty = ParseTypeName();
861
862    // Match the ')'.
863    RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
864
865    if (Ty.isInvalid() || RParenLoc.isInvalid())
866      return ExprError();
867
868    Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
869                                    Ty.get().getAsOpaquePtr(), RParenLoc);
870  } else {
871    // C++0x [expr.typeid]p3:
872    //   When typeid is applied to an expression other than an lvalue of a
873    //   polymorphic class type [...] The expression is an unevaluated
874    //   operand (Clause 5).
875    //
876    // Note that we can't tell whether the expression is an lvalue of a
877    // polymorphic class type until after we've parsed the expression, so
878    // we the expression is potentially potentially evaluated.
879    EnterExpressionEvaluationContext Unevaluated(Actions,
880                                       Sema::PotentiallyPotentiallyEvaluated);
881    Result = ParseExpression();
882
883    // Match the ')'.
884    if (Result.isInvalid())
885      SkipUntil(tok::r_paren);
886    else {
887      RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
888      if (RParenLoc.isInvalid())
889        return ExprError();
890
891      // If we are a foo<int> that identifies a single function, resolve it now...
892      Expr* e = Result.get();
893      if (e->getType() == Actions.Context.OverloadTy) {
894        ExprResult er =
895              Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
896        if (er.isUsable())
897          Result = er.release();
898      }
899      Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
900                                      Result.release(), RParenLoc);
901    }
902  }
903
904  return move(Result);
905}
906
907/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
908///
909///         '__uuidof' '(' expression ')'
910///         '__uuidof' '(' type-id ')'
911///
912ExprResult Parser::ParseCXXUuidof() {
913  assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
914
915  SourceLocation OpLoc = ConsumeToken();
916  SourceLocation LParenLoc = Tok.getLocation();
917  SourceLocation RParenLoc;
918
919  // __uuidof expressions are always parenthesized.
920  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
921      "__uuidof"))
922    return ExprError();
923
924  ExprResult Result;
925
926  if (isTypeIdInParens()) {
927    TypeResult Ty = ParseTypeName();
928
929    // Match the ')'.
930    RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
931
932    if (Ty.isInvalid())
933      return ExprError();
934
935    Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
936                                    Ty.get().getAsOpaquePtr(), RParenLoc);
937  } else {
938    EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
939    Result = ParseExpression();
940
941    // Match the ')'.
942    if (Result.isInvalid())
943      SkipUntil(tok::r_paren);
944    else {
945      RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
946
947      Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
948                                      Result.release(), RParenLoc);
949    }
950  }
951
952  return move(Result);
953}
954
955/// \brief Parse a C++ pseudo-destructor expression after the base,
956/// . or -> operator, and nested-name-specifier have already been
957/// parsed.
958///
959///       postfix-expression: [C++ 5.2]
960///         postfix-expression . pseudo-destructor-name
961///         postfix-expression -> pseudo-destructor-name
962///
963///       pseudo-destructor-name:
964///         ::[opt] nested-name-specifier[opt] type-name :: ~type-name
965///         ::[opt] nested-name-specifier template simple-template-id ::
966///                 ~type-name
967///         ::[opt] nested-name-specifier[opt] ~type-name
968///
969ExprResult
970Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
971                                 tok::TokenKind OpKind,
972                                 CXXScopeSpec &SS,
973                                 ParsedType ObjectType) {
974  // We're parsing either a pseudo-destructor-name or a dependent
975  // member access that has the same form as a
976  // pseudo-destructor-name. We parse both in the same way and let
977  // the action model sort them out.
978  //
979  // Note that the ::[opt] nested-name-specifier[opt] has already
980  // been parsed, and if there was a simple-template-id, it has
981  // been coalesced into a template-id annotation token.
982  UnqualifiedId FirstTypeName;
983  SourceLocation CCLoc;
984  if (Tok.is(tok::identifier)) {
985    FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
986    ConsumeToken();
987    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
988    CCLoc = ConsumeToken();
989  } else if (Tok.is(tok::annot_template_id)) {
990    FirstTypeName.setTemplateId(
991                              (TemplateIdAnnotation *)Tok.getAnnotationValue());
992    ConsumeToken();
993    assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
994    CCLoc = ConsumeToken();
995  } else {
996    FirstTypeName.setIdentifier(0, SourceLocation());
997  }
998
999  // Parse the tilde.
1000  assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1001  SourceLocation TildeLoc = ConsumeToken();
1002  if (!Tok.is(tok::identifier)) {
1003    Diag(Tok, diag::err_destructor_tilde_identifier);
1004    return ExprError();
1005  }
1006
1007  // Parse the second type.
1008  UnqualifiedId SecondTypeName;
1009  IdentifierInfo *Name = Tok.getIdentifierInfo();
1010  SourceLocation NameLoc = ConsumeToken();
1011  SecondTypeName.setIdentifier(Name, NameLoc);
1012
1013  // If there is a '<', the second type name is a template-id. Parse
1014  // it as such.
1015  if (Tok.is(tok::less) &&
1016      ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
1017                                   SecondTypeName, /*AssumeTemplateName=*/true,
1018                                   /*TemplateKWLoc*/SourceLocation()))
1019    return ExprError();
1020
1021  return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1022                                           OpLoc, OpKind,
1023                                           SS, FirstTypeName, CCLoc,
1024                                           TildeLoc, SecondTypeName,
1025                                           Tok.is(tok::l_paren));
1026}
1027
1028/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1029///
1030///       boolean-literal: [C++ 2.13.5]
1031///         'true'
1032///         'false'
1033ExprResult Parser::ParseCXXBoolLiteral() {
1034  tok::TokenKind Kind = Tok.getKind();
1035  return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
1036}
1037
1038/// ParseThrowExpression - This handles the C++ throw expression.
1039///
1040///       throw-expression: [C++ 15]
1041///         'throw' assignment-expression[opt]
1042ExprResult Parser::ParseThrowExpression() {
1043  assert(Tok.is(tok::kw_throw) && "Not throw!");
1044  SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token.
1045
1046  // If the current token isn't the start of an assignment-expression,
1047  // then the expression is not present.  This handles things like:
1048  //   "C ? throw : (void)42", which is crazy but legal.
1049  switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common.
1050  case tok::semi:
1051  case tok::r_paren:
1052  case tok::r_square:
1053  case tok::r_brace:
1054  case tok::colon:
1055  case tok::comma:
1056    return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
1057
1058  default:
1059    ExprResult Expr(ParseAssignmentExpression());
1060    if (Expr.isInvalid()) return move(Expr);
1061    return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
1062  }
1063}
1064
1065/// ParseCXXThis - This handles the C++ 'this' pointer.
1066///
1067/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1068/// a non-lvalue expression whose value is the address of the object for which
1069/// the function is called.
1070ExprResult Parser::ParseCXXThis() {
1071  assert(Tok.is(tok::kw_this) && "Not 'this'!");
1072  SourceLocation ThisLoc = ConsumeToken();
1073  return Actions.ActOnCXXThis(ThisLoc);
1074}
1075
1076/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1077/// Can be interpreted either as function-style casting ("int(x)")
1078/// or class type construction ("ClassType(x,y,z)")
1079/// or creation of a value-initialized type ("int()").
1080/// See [C++ 5.2.3].
1081///
1082///       postfix-expression: [C++ 5.2p1]
1083///         simple-type-specifier '(' expression-list[opt] ')'
1084/// [C++0x] simple-type-specifier braced-init-list
1085///         typename-specifier '(' expression-list[opt] ')'
1086/// [C++0x] typename-specifier braced-init-list
1087///
1088ExprResult
1089Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
1090  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1091  ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
1092
1093  assert((Tok.is(tok::l_paren) ||
1094          (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1095         && "Expected '(' or '{'!");
1096
1097  if (Tok.is(tok::l_brace)) {
1098
1099    // FIXME: Convert to a proper type construct expression.
1100    return ParseBraceInitializer();
1101
1102  } else {
1103    GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1104
1105    SourceLocation LParenLoc = ConsumeParen();
1106
1107    ExprVector Exprs(Actions);
1108    CommaLocsTy CommaLocs;
1109
1110    if (Tok.isNot(tok::r_paren)) {
1111      if (ParseExpressionList(Exprs, CommaLocs)) {
1112        SkipUntil(tok::r_paren);
1113        return ExprError();
1114      }
1115    }
1116
1117    // Match the ')'.
1118    SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1119
1120    // TypeRep could be null, if it references an invalid typedef.
1121    if (!TypeRep)
1122      return ExprError();
1123
1124    assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1125           "Unexpected number of commas!");
1126    return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
1127                                             RParenLoc);
1128  }
1129}
1130
1131/// ParseCXXCondition - if/switch/while condition expression.
1132///
1133///       condition:
1134///         expression
1135///         type-specifier-seq declarator '=' assignment-expression
1136/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1137///             '=' assignment-expression
1138///
1139/// \param ExprResult if the condition was parsed as an expression, the
1140/// parsed expression.
1141///
1142/// \param DeclResult if the condition was parsed as a declaration, the
1143/// parsed declaration.
1144///
1145/// \param Loc The location of the start of the statement that requires this
1146/// condition, e.g., the "for" in a for loop.
1147///
1148/// \param ConvertToBoolean Whether the condition expression should be
1149/// converted to a boolean value.
1150///
1151/// \returns true if there was a parsing, false otherwise.
1152bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1153                               Decl *&DeclOut,
1154                               SourceLocation Loc,
1155                               bool ConvertToBoolean) {
1156  if (Tok.is(tok::code_completion)) {
1157    Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
1158    cutOffParsing();
1159    return true;
1160  }
1161
1162  if (!isCXXConditionDeclaration()) {
1163    // Parse the expression.
1164    ExprOut = ParseExpression(); // expression
1165    DeclOut = 0;
1166    if (ExprOut.isInvalid())
1167      return true;
1168
1169    // If required, convert to a boolean value.
1170    if (ConvertToBoolean)
1171      ExprOut
1172        = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1173    return ExprOut.isInvalid();
1174  }
1175
1176  // type-specifier-seq
1177  DeclSpec DS(AttrFactory);
1178  ParseSpecifierQualifierList(DS);
1179
1180  // declarator
1181  Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1182  ParseDeclarator(DeclaratorInfo);
1183
1184  // simple-asm-expr[opt]
1185  if (Tok.is(tok::kw_asm)) {
1186    SourceLocation Loc;
1187    ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1188    if (AsmLabel.isInvalid()) {
1189      SkipUntil(tok::semi);
1190      return true;
1191    }
1192    DeclaratorInfo.setAsmLabel(AsmLabel.release());
1193    DeclaratorInfo.SetRangeEnd(Loc);
1194  }
1195
1196  // If attributes are present, parse them.
1197  MaybeParseGNUAttributes(DeclaratorInfo);
1198
1199  // Type-check the declaration itself.
1200  DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
1201                                                        DeclaratorInfo);
1202  DeclOut = Dcl.get();
1203  ExprOut = ExprError();
1204
1205  // '=' assignment-expression
1206  if (isTokenEqualOrMistypedEqualEqual(
1207                               diag::err_invalid_equalequal_after_declarator)) {
1208    ConsumeToken();
1209    ExprResult AssignExpr(ParseAssignmentExpression());
1210    if (!AssignExpr.isInvalid())
1211      Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1212                                   DS.getTypeSpecType() == DeclSpec::TST_auto);
1213  } else {
1214    // FIXME: C++0x allows a braced-init-list
1215    Diag(Tok, diag::err_expected_equal_after_declarator);
1216  }
1217
1218  // FIXME: Build a reference to this declaration? Convert it to bool?
1219  // (This is currently handled by Sema).
1220
1221  Actions.FinalizeDeclaration(DeclOut);
1222
1223  return false;
1224}
1225
1226/// \brief Determine whether the current token starts a C++
1227/// simple-type-specifier.
1228bool Parser::isCXXSimpleTypeSpecifier() const {
1229  switch (Tok.getKind()) {
1230  case tok::annot_typename:
1231  case tok::kw_short:
1232  case tok::kw_long:
1233  case tok::kw___int64:
1234  case tok::kw_signed:
1235  case tok::kw_unsigned:
1236  case tok::kw_void:
1237  case tok::kw_char:
1238  case tok::kw_int:
1239  case tok::kw_float:
1240  case tok::kw_double:
1241  case tok::kw_wchar_t:
1242  case tok::kw_char16_t:
1243  case tok::kw_char32_t:
1244  case tok::kw_bool:
1245  case tok::kw_decltype:
1246  case tok::kw_typeof:
1247  case tok::kw___underlying_type:
1248    return true;
1249
1250  default:
1251    break;
1252  }
1253
1254  return false;
1255}
1256
1257/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1258/// This should only be called when the current token is known to be part of
1259/// simple-type-specifier.
1260///
1261///       simple-type-specifier:
1262///         '::'[opt] nested-name-specifier[opt] type-name
1263///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1264///         char
1265///         wchar_t
1266///         bool
1267///         short
1268///         int
1269///         long
1270///         signed
1271///         unsigned
1272///         float
1273///         double
1274///         void
1275/// [GNU]   typeof-specifier
1276/// [C++0x] auto               [TODO]
1277///
1278///       type-name:
1279///         class-name
1280///         enum-name
1281///         typedef-name
1282///
1283void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1284  DS.SetRangeStart(Tok.getLocation());
1285  const char *PrevSpec;
1286  unsigned DiagID;
1287  SourceLocation Loc = Tok.getLocation();
1288
1289  switch (Tok.getKind()) {
1290  case tok::identifier:   // foo::bar
1291  case tok::coloncolon:   // ::foo::bar
1292    assert(0 && "Annotation token should already be formed!");
1293  default:
1294    assert(0 && "Not a simple-type-specifier token!");
1295    abort();
1296
1297  // type-name
1298  case tok::annot_typename: {
1299    if (getTypeAnnotation(Tok))
1300      DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1301                         getTypeAnnotation(Tok));
1302    else
1303      DS.SetTypeSpecError();
1304
1305    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1306    ConsumeToken();
1307
1308    // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1309    // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1310    // Objective-C interface.  If we don't have Objective-C or a '<', this is
1311    // just a normal reference to a typedef name.
1312    if (Tok.is(tok::less) && getLang().ObjC1)
1313      ParseObjCProtocolQualifiers(DS);
1314
1315    DS.Finish(Diags, PP);
1316    return;
1317  }
1318
1319  // builtin types
1320  case tok::kw_short:
1321    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
1322    break;
1323  case tok::kw_long:
1324    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
1325    break;
1326  case tok::kw___int64:
1327    DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1328    break;
1329  case tok::kw_signed:
1330    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
1331    break;
1332  case tok::kw_unsigned:
1333    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
1334    break;
1335  case tok::kw_void:
1336    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
1337    break;
1338  case tok::kw_char:
1339    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
1340    break;
1341  case tok::kw_int:
1342    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
1343    break;
1344  case tok::kw_float:
1345    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
1346    break;
1347  case tok::kw_double:
1348    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
1349    break;
1350  case tok::kw_wchar_t:
1351    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
1352    break;
1353  case tok::kw_char16_t:
1354    DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
1355    break;
1356  case tok::kw_char32_t:
1357    DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
1358    break;
1359  case tok::kw_bool:
1360    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
1361    break;
1362
1363    // FIXME: C++0x decltype support.
1364  // GNU typeof support.
1365  case tok::kw_typeof:
1366    ParseTypeofSpecifier(DS);
1367    DS.Finish(Diags, PP);
1368    return;
1369  }
1370  if (Tok.is(tok::annot_typename))
1371    DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1372  else
1373    DS.SetRangeEnd(Tok.getLocation());
1374  ConsumeToken();
1375  DS.Finish(Diags, PP);
1376}
1377
1378/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1379/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1380/// e.g., "const short int". Note that the DeclSpec is *not* finished
1381/// by parsing the type-specifier-seq, because these sequences are
1382/// typically followed by some form of declarator. Returns true and
1383/// emits diagnostics if this is not a type-specifier-seq, false
1384/// otherwise.
1385///
1386///   type-specifier-seq: [C++ 8.1]
1387///     type-specifier type-specifier-seq[opt]
1388///
1389bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1390  DS.SetRangeStart(Tok.getLocation());
1391  const char *PrevSpec = 0;
1392  unsigned DiagID;
1393  bool isInvalid = 0;
1394
1395  // Parse one or more of the type specifiers.
1396  if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1397      ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
1398    Diag(Tok, diag::err_expected_type);
1399    return true;
1400  }
1401
1402  while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1403         ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1404  {}
1405
1406  DS.Finish(Diags, PP);
1407  return false;
1408}
1409
1410/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1411/// some form.
1412///
1413/// This routine is invoked when a '<' is encountered after an identifier or
1414/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1415/// whether the unqualified-id is actually a template-id. This routine will
1416/// then parse the template arguments and form the appropriate template-id to
1417/// return to the caller.
1418///
1419/// \param SS the nested-name-specifier that precedes this template-id, if
1420/// we're actually parsing a qualified-id.
1421///
1422/// \param Name for constructor and destructor names, this is the actual
1423/// identifier that may be a template-name.
1424///
1425/// \param NameLoc the location of the class-name in a constructor or
1426/// destructor.
1427///
1428/// \param EnteringContext whether we're entering the scope of the
1429/// nested-name-specifier.
1430///
1431/// \param ObjectType if this unqualified-id occurs within a member access
1432/// expression, the type of the base object whose member is being accessed.
1433///
1434/// \param Id as input, describes the template-name or operator-function-id
1435/// that precedes the '<'. If template arguments were parsed successfully,
1436/// will be updated with the template-id.
1437///
1438/// \param AssumeTemplateId When true, this routine will assume that the name
1439/// refers to a template without performing name lookup to verify.
1440///
1441/// \returns true if a parse error occurred, false otherwise.
1442bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1443                                          IdentifierInfo *Name,
1444                                          SourceLocation NameLoc,
1445                                          bool EnteringContext,
1446                                          ParsedType ObjectType,
1447                                          UnqualifiedId &Id,
1448                                          bool AssumeTemplateId,
1449                                          SourceLocation TemplateKWLoc) {
1450  assert((AssumeTemplateId || Tok.is(tok::less)) &&
1451         "Expected '<' to finish parsing a template-id");
1452
1453  TemplateTy Template;
1454  TemplateNameKind TNK = TNK_Non_template;
1455  switch (Id.getKind()) {
1456  case UnqualifiedId::IK_Identifier:
1457  case UnqualifiedId::IK_OperatorFunctionId:
1458  case UnqualifiedId::IK_LiteralOperatorId:
1459    if (AssumeTemplateId) {
1460      TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
1461                                               Id, ObjectType, EnteringContext,
1462                                               Template);
1463      if (TNK == TNK_Non_template)
1464        return true;
1465    } else {
1466      bool MemberOfUnknownSpecialization;
1467      TNK = Actions.isTemplateName(getCurScope(), SS,
1468                                   TemplateKWLoc.isValid(), Id,
1469                                   ObjectType, EnteringContext, Template,
1470                                   MemberOfUnknownSpecialization);
1471
1472      if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1473          ObjectType && IsTemplateArgumentList()) {
1474        // We have something like t->getAs<T>(), where getAs is a
1475        // member of an unknown specialization. However, this will only
1476        // parse correctly as a template, so suggest the keyword 'template'
1477        // before 'getAs' and treat this as a dependent template name.
1478        std::string Name;
1479        if (Id.getKind() == UnqualifiedId::IK_Identifier)
1480          Name = Id.Identifier->getName();
1481        else {
1482          Name = "operator ";
1483          if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1484            Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1485          else
1486            Name += Id.Identifier->getName();
1487        }
1488        Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1489          << Name
1490          << FixItHint::CreateInsertion(Id.StartLocation, "template ");
1491        TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
1492                                                 SS, Id, ObjectType,
1493                                                 EnteringContext, Template);
1494        if (TNK == TNK_Non_template)
1495          return true;
1496      }
1497    }
1498    break;
1499
1500  case UnqualifiedId::IK_ConstructorName: {
1501    UnqualifiedId TemplateName;
1502    bool MemberOfUnknownSpecialization;
1503    TemplateName.setIdentifier(Name, NameLoc);
1504    TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1505                                 TemplateName, ObjectType,
1506                                 EnteringContext, Template,
1507                                 MemberOfUnknownSpecialization);
1508    break;
1509  }
1510
1511  case UnqualifiedId::IK_DestructorName: {
1512    UnqualifiedId TemplateName;
1513    bool MemberOfUnknownSpecialization;
1514    TemplateName.setIdentifier(Name, NameLoc);
1515    if (ObjectType) {
1516      TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
1517                                               TemplateName, ObjectType,
1518                                               EnteringContext, Template);
1519      if (TNK == TNK_Non_template)
1520        return true;
1521    } else {
1522      TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1523                                   TemplateName, ObjectType,
1524                                   EnteringContext, Template,
1525                                   MemberOfUnknownSpecialization);
1526
1527      if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
1528        Diag(NameLoc, diag::err_destructor_template_id)
1529          << Name << SS.getRange();
1530        return true;
1531      }
1532    }
1533    break;
1534  }
1535
1536  default:
1537    return false;
1538  }
1539
1540  if (TNK == TNK_Non_template)
1541    return false;
1542
1543  // Parse the enclosed template argument list.
1544  SourceLocation LAngleLoc, RAngleLoc;
1545  TemplateArgList TemplateArgs;
1546  if (Tok.is(tok::less) &&
1547      ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
1548                                       SS, true, LAngleLoc,
1549                                       TemplateArgs,
1550                                       RAngleLoc))
1551    return true;
1552
1553  if (Id.getKind() == UnqualifiedId::IK_Identifier ||
1554      Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1555      Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
1556    // Form a parsed representation of the template-id to be stored in the
1557    // UnqualifiedId.
1558    TemplateIdAnnotation *TemplateId
1559      = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1560
1561    if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1562      TemplateId->Name = Id.Identifier;
1563      TemplateId->Operator = OO_None;
1564      TemplateId->TemplateNameLoc = Id.StartLocation;
1565    } else {
1566      TemplateId->Name = 0;
1567      TemplateId->Operator = Id.OperatorFunctionId.Operator;
1568      TemplateId->TemplateNameLoc = Id.StartLocation;
1569    }
1570
1571    TemplateId->SS = SS;
1572    TemplateId->Template = Template;
1573    TemplateId->Kind = TNK;
1574    TemplateId->LAngleLoc = LAngleLoc;
1575    TemplateId->RAngleLoc = RAngleLoc;
1576    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
1577    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
1578         Arg != ArgEnd; ++Arg)
1579      Args[Arg] = TemplateArgs[Arg];
1580
1581    Id.setTemplateId(TemplateId);
1582    return false;
1583  }
1584
1585  // Bundle the template arguments together.
1586  ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
1587                                     TemplateArgs.size());
1588
1589  // Constructor and destructor names.
1590  TypeResult Type
1591    = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
1592                                  LAngleLoc, TemplateArgsPtr,
1593                                  RAngleLoc);
1594  if (Type.isInvalid())
1595    return true;
1596
1597  if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1598    Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1599  else
1600    Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1601
1602  return false;
1603}
1604
1605/// \brief Parse an operator-function-id or conversion-function-id as part
1606/// of a C++ unqualified-id.
1607///
1608/// This routine is responsible only for parsing the operator-function-id or
1609/// conversion-function-id; it does not handle template arguments in any way.
1610///
1611/// \code
1612///       operator-function-id: [C++ 13.5]
1613///         'operator' operator
1614///
1615///       operator: one of
1616///            new   delete  new[]   delete[]
1617///            +     -    *  /    %  ^    &   |   ~
1618///            !     =    <  >    += -=   *=  /=  %=
1619///            ^=    &=   |= <<   >> >>= <<=  ==  !=
1620///            <=    >=   && ||   ++ --   ,   ->* ->
1621///            ()    []
1622///
1623///       conversion-function-id: [C++ 12.3.2]
1624///         operator conversion-type-id
1625///
1626///       conversion-type-id:
1627///         type-specifier-seq conversion-declarator[opt]
1628///
1629///       conversion-declarator:
1630///         ptr-operator conversion-declarator[opt]
1631/// \endcode
1632///
1633/// \param The nested-name-specifier that preceded this unqualified-id. If
1634/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1635///
1636/// \param EnteringContext whether we are entering the scope of the
1637/// nested-name-specifier.
1638///
1639/// \param ObjectType if this unqualified-id occurs within a member access
1640/// expression, the type of the base object whose member is being accessed.
1641///
1642/// \param Result on a successful parse, contains the parsed unqualified-id.
1643///
1644/// \returns true if parsing fails, false otherwise.
1645bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1646                                        ParsedType ObjectType,
1647                                        UnqualifiedId &Result) {
1648  assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1649
1650  // Consume the 'operator' keyword.
1651  SourceLocation KeywordLoc = ConsumeToken();
1652
1653  // Determine what kind of operator name we have.
1654  unsigned SymbolIdx = 0;
1655  SourceLocation SymbolLocations[3];
1656  OverloadedOperatorKind Op = OO_None;
1657  switch (Tok.getKind()) {
1658    case tok::kw_new:
1659    case tok::kw_delete: {
1660      bool isNew = Tok.getKind() == tok::kw_new;
1661      // Consume the 'new' or 'delete'.
1662      SymbolLocations[SymbolIdx++] = ConsumeToken();
1663      if (Tok.is(tok::l_square)) {
1664        // Consume the '['.
1665        SourceLocation LBracketLoc = ConsumeBracket();
1666        // Consume the ']'.
1667        SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1668                                                         LBracketLoc);
1669        if (RBracketLoc.isInvalid())
1670          return true;
1671
1672        SymbolLocations[SymbolIdx++] = LBracketLoc;
1673        SymbolLocations[SymbolIdx++] = RBracketLoc;
1674        Op = isNew? OO_Array_New : OO_Array_Delete;
1675      } else {
1676        Op = isNew? OO_New : OO_Delete;
1677      }
1678      break;
1679    }
1680
1681#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1682    case tok::Token:                                                     \
1683      SymbolLocations[SymbolIdx++] = ConsumeToken();                     \
1684      Op = OO_##Name;                                                    \
1685      break;
1686#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1687#include "clang/Basic/OperatorKinds.def"
1688
1689    case tok::l_paren: {
1690      // Consume the '('.
1691      SourceLocation LParenLoc = ConsumeParen();
1692      // Consume the ')'.
1693      SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1694                                                     LParenLoc);
1695      if (RParenLoc.isInvalid())
1696        return true;
1697
1698      SymbolLocations[SymbolIdx++] = LParenLoc;
1699      SymbolLocations[SymbolIdx++] = RParenLoc;
1700      Op = OO_Call;
1701      break;
1702    }
1703
1704    case tok::l_square: {
1705      // Consume the '['.
1706      SourceLocation LBracketLoc = ConsumeBracket();
1707      // Consume the ']'.
1708      SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1709                                                       LBracketLoc);
1710      if (RBracketLoc.isInvalid())
1711        return true;
1712
1713      SymbolLocations[SymbolIdx++] = LBracketLoc;
1714      SymbolLocations[SymbolIdx++] = RBracketLoc;
1715      Op = OO_Subscript;
1716      break;
1717    }
1718
1719    case tok::code_completion: {
1720      // Code completion for the operator name.
1721      Actions.CodeCompleteOperatorName(getCurScope());
1722      cutOffParsing();
1723      // Don't try to parse any further.
1724      return true;
1725    }
1726
1727    default:
1728      break;
1729  }
1730
1731  if (Op != OO_None) {
1732    // We have parsed an operator-function-id.
1733    Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1734    return false;
1735  }
1736
1737  // Parse a literal-operator-id.
1738  //
1739  //   literal-operator-id: [C++0x 13.5.8]
1740  //     operator "" identifier
1741
1742  if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1743    if (Tok.getLength() != 2)
1744      Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1745    ConsumeStringToken();
1746
1747    if (Tok.isNot(tok::identifier)) {
1748      Diag(Tok.getLocation(), diag::err_expected_ident);
1749      return true;
1750    }
1751
1752    IdentifierInfo *II = Tok.getIdentifierInfo();
1753    Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
1754    return false;
1755  }
1756
1757  // Parse a conversion-function-id.
1758  //
1759  //   conversion-function-id: [C++ 12.3.2]
1760  //     operator conversion-type-id
1761  //
1762  //   conversion-type-id:
1763  //     type-specifier-seq conversion-declarator[opt]
1764  //
1765  //   conversion-declarator:
1766  //     ptr-operator conversion-declarator[opt]
1767
1768  // Parse the type-specifier-seq.
1769  DeclSpec DS(AttrFactory);
1770  if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
1771    return true;
1772
1773  // Parse the conversion-declarator, which is merely a sequence of
1774  // ptr-operators.
1775  Declarator D(DS, Declarator::TypeNameContext);
1776  ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1777
1778  // Finish up the type.
1779  TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
1780  if (Ty.isInvalid())
1781    return true;
1782
1783  // Note that this is a conversion-function-id.
1784  Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1785                                 D.getSourceRange().getEnd());
1786  return false;
1787}
1788
1789/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1790/// name of an entity.
1791///
1792/// \code
1793///       unqualified-id: [C++ expr.prim.general]
1794///         identifier
1795///         operator-function-id
1796///         conversion-function-id
1797/// [C++0x] literal-operator-id [TODO]
1798///         ~ class-name
1799///         template-id
1800///
1801/// \endcode
1802///
1803/// \param The nested-name-specifier that preceded this unqualified-id. If
1804/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1805///
1806/// \param EnteringContext whether we are entering the scope of the
1807/// nested-name-specifier.
1808///
1809/// \param AllowDestructorName whether we allow parsing of a destructor name.
1810///
1811/// \param AllowConstructorName whether we allow parsing a constructor name.
1812///
1813/// \param ObjectType if this unqualified-id occurs within a member access
1814/// expression, the type of the base object whose member is being accessed.
1815///
1816/// \param Result on a successful parse, contains the parsed unqualified-id.
1817///
1818/// \returns true if parsing fails, false otherwise.
1819bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1820                                bool AllowDestructorName,
1821                                bool AllowConstructorName,
1822                                ParsedType ObjectType,
1823                                UnqualifiedId &Result) {
1824
1825  // Handle 'A::template B'. This is for template-ids which have not
1826  // already been annotated by ParseOptionalCXXScopeSpecifier().
1827  bool TemplateSpecified = false;
1828  SourceLocation TemplateKWLoc;
1829  if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1830      (ObjectType || SS.isSet())) {
1831    TemplateSpecified = true;
1832    TemplateKWLoc = ConsumeToken();
1833  }
1834
1835  // unqualified-id:
1836  //   identifier
1837  //   template-id (when it hasn't already been annotated)
1838  if (Tok.is(tok::identifier)) {
1839    // Consume the identifier.
1840    IdentifierInfo *Id = Tok.getIdentifierInfo();
1841    SourceLocation IdLoc = ConsumeToken();
1842
1843    if (!getLang().CPlusPlus) {
1844      // If we're not in C++, only identifiers matter. Record the
1845      // identifier and return.
1846      Result.setIdentifier(Id, IdLoc);
1847      return false;
1848    }
1849
1850    if (AllowConstructorName &&
1851        Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
1852      // We have parsed a constructor name.
1853      Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
1854                                                    &SS, false, false,
1855                                                    ParsedType(),
1856                                            /*NonTrivialTypeSourceInfo=*/true),
1857                                IdLoc, IdLoc);
1858    } else {
1859      // We have parsed an identifier.
1860      Result.setIdentifier(Id, IdLoc);
1861    }
1862
1863    // If the next token is a '<', we may have a template.
1864    if (TemplateSpecified || Tok.is(tok::less))
1865      return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
1866                                          ObjectType, Result,
1867                                          TemplateSpecified, TemplateKWLoc);
1868
1869    return false;
1870  }
1871
1872  // unqualified-id:
1873  //   template-id (already parsed and annotated)
1874  if (Tok.is(tok::annot_template_id)) {
1875    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1876
1877    // If the template-name names the current class, then this is a constructor
1878    if (AllowConstructorName && TemplateId->Name &&
1879        Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
1880      if (SS.isSet()) {
1881        // C++ [class.qual]p2 specifies that a qualified template-name
1882        // is taken as the constructor name where a constructor can be
1883        // declared. Thus, the template arguments are extraneous, so
1884        // complain about them and remove them entirely.
1885        Diag(TemplateId->TemplateNameLoc,
1886             diag::err_out_of_line_constructor_template_id)
1887          << TemplateId->Name
1888          << FixItHint::CreateRemoval(
1889                    SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1890        Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1891                                                  TemplateId->TemplateNameLoc,
1892                                                      getCurScope(),
1893                                                      &SS, false, false,
1894                                                      ParsedType(),
1895                                            /*NontrivialTypeSourceInfo=*/true),
1896                                  TemplateId->TemplateNameLoc,
1897                                  TemplateId->RAngleLoc);
1898        ConsumeToken();
1899        return false;
1900      }
1901
1902      Result.setConstructorTemplateId(TemplateId);
1903      ConsumeToken();
1904      return false;
1905    }
1906
1907    // We have already parsed a template-id; consume the annotation token as
1908    // our unqualified-id.
1909    Result.setTemplateId(TemplateId);
1910    ConsumeToken();
1911    return false;
1912  }
1913
1914  // unqualified-id:
1915  //   operator-function-id
1916  //   conversion-function-id
1917  if (Tok.is(tok::kw_operator)) {
1918    if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
1919      return true;
1920
1921    // If we have an operator-function-id or a literal-operator-id and the next
1922    // token is a '<', we may have a
1923    //
1924    //   template-id:
1925    //     operator-function-id < template-argument-list[opt] >
1926    if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1927         Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
1928        (TemplateSpecified || Tok.is(tok::less)))
1929      return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1930                                          EnteringContext, ObjectType,
1931                                          Result,
1932                                          TemplateSpecified, TemplateKWLoc);
1933
1934    return false;
1935  }
1936
1937  if (getLang().CPlusPlus &&
1938      (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1939    // C++ [expr.unary.op]p10:
1940    //   There is an ambiguity in the unary-expression ~X(), where X is a
1941    //   class-name. The ambiguity is resolved in favor of treating ~ as a
1942    //    unary complement rather than treating ~X as referring to a destructor.
1943
1944    // Parse the '~'.
1945    SourceLocation TildeLoc = ConsumeToken();
1946
1947    // Parse the class-name.
1948    if (Tok.isNot(tok::identifier)) {
1949      Diag(Tok, diag::err_destructor_tilde_identifier);
1950      return true;
1951    }
1952
1953    // Parse the class-name (or template-name in a simple-template-id).
1954    IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1955    SourceLocation ClassNameLoc = ConsumeToken();
1956
1957    if (TemplateSpecified || Tok.is(tok::less)) {
1958      Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
1959      return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1960                                          EnteringContext, ObjectType, Result,
1961                                          TemplateSpecified, TemplateKWLoc);
1962    }
1963
1964    // Note that this is a destructor name.
1965    ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1966                                              ClassNameLoc, getCurScope(),
1967                                              SS, ObjectType,
1968                                              EnteringContext);
1969    if (!Ty)
1970      return true;
1971
1972    Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
1973    return false;
1974  }
1975
1976  Diag(Tok, diag::err_expected_unqualified_id)
1977    << getLang().CPlusPlus;
1978  return true;
1979}
1980
1981/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1982/// memory in a typesafe manner and call constructors.
1983///
1984/// This method is called to parse the new expression after the optional :: has
1985/// been already parsed.  If the :: was present, "UseGlobal" is true and "Start"
1986/// is its location.  Otherwise, "Start" is the location of the 'new' token.
1987///
1988///        new-expression:
1989///                   '::'[opt] 'new' new-placement[opt] new-type-id
1990///                                     new-initializer[opt]
1991///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1992///                                     new-initializer[opt]
1993///
1994///        new-placement:
1995///                   '(' expression-list ')'
1996///
1997///        new-type-id:
1998///                   type-specifier-seq new-declarator[opt]
1999/// [GNU]             attributes type-specifier-seq new-declarator[opt]
2000///
2001///        new-declarator:
2002///                   ptr-operator new-declarator[opt]
2003///                   direct-new-declarator
2004///
2005///        new-initializer:
2006///                   '(' expression-list[opt] ')'
2007/// [C++0x]           braced-init-list
2008///
2009ExprResult
2010Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2011  assert(Tok.is(tok::kw_new) && "expected 'new' token");
2012  ConsumeToken();   // Consume 'new'
2013
2014  // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2015  // second form of new-expression. It can't be a new-type-id.
2016
2017  ExprVector PlacementArgs(Actions);
2018  SourceLocation PlacementLParen, PlacementRParen;
2019
2020  SourceRange TypeIdParens;
2021  DeclSpec DS(AttrFactory);
2022  Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
2023  if (Tok.is(tok::l_paren)) {
2024    // If it turns out to be a placement, we change the type location.
2025    PlacementLParen = ConsumeParen();
2026    if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2027      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
2028      return ExprError();
2029    }
2030
2031    PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
2032    if (PlacementRParen.isInvalid()) {
2033      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
2034      return ExprError();
2035    }
2036
2037    if (PlacementArgs.empty()) {
2038      // Reset the placement locations. There was no placement.
2039      TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
2040      PlacementLParen = PlacementRParen = SourceLocation();
2041    } else {
2042      // We still need the type.
2043      if (Tok.is(tok::l_paren)) {
2044        TypeIdParens.setBegin(ConsumeParen());
2045        MaybeParseGNUAttributes(DeclaratorInfo);
2046        ParseSpecifierQualifierList(DS);
2047        DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2048        ParseDeclarator(DeclaratorInfo);
2049        TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
2050                                                TypeIdParens.getBegin()));
2051      } else {
2052        MaybeParseGNUAttributes(DeclaratorInfo);
2053        if (ParseCXXTypeSpecifierSeq(DS))
2054          DeclaratorInfo.setInvalidType(true);
2055        else {
2056          DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2057          ParseDeclaratorInternal(DeclaratorInfo,
2058                                  &Parser::ParseDirectNewDeclarator);
2059        }
2060      }
2061    }
2062  } else {
2063    // A new-type-id is a simplified type-id, where essentially the
2064    // direct-declarator is replaced by a direct-new-declarator.
2065    MaybeParseGNUAttributes(DeclaratorInfo);
2066    if (ParseCXXTypeSpecifierSeq(DS))
2067      DeclaratorInfo.setInvalidType(true);
2068    else {
2069      DeclaratorInfo.SetSourceRange(DS.getSourceRange());
2070      ParseDeclaratorInternal(DeclaratorInfo,
2071                              &Parser::ParseDirectNewDeclarator);
2072    }
2073  }
2074  if (DeclaratorInfo.isInvalidType()) {
2075    SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
2076    return ExprError();
2077  }
2078
2079  ExprVector ConstructorArgs(Actions);
2080  SourceLocation ConstructorLParen, ConstructorRParen;
2081
2082  if (Tok.is(tok::l_paren)) {
2083    ConstructorLParen = ConsumeParen();
2084    if (Tok.isNot(tok::r_paren)) {
2085      CommaLocsTy CommaLocs;
2086      if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2087        SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
2088        return ExprError();
2089      }
2090    }
2091    ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
2092    if (ConstructorRParen.isInvalid()) {
2093      SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
2094      return ExprError();
2095    }
2096  } else if (Tok.is(tok::l_brace)) {
2097    // FIXME: Have to communicate the init-list to ActOnCXXNew.
2098    ParseBraceInitializer();
2099  }
2100
2101  return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2102                             move_arg(PlacementArgs), PlacementRParen,
2103                             TypeIdParens, DeclaratorInfo, ConstructorLParen,
2104                             move_arg(ConstructorArgs), ConstructorRParen);
2105}
2106
2107/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2108/// passed to ParseDeclaratorInternal.
2109///
2110///        direct-new-declarator:
2111///                   '[' expression ']'
2112///                   direct-new-declarator '[' constant-expression ']'
2113///
2114void Parser::ParseDirectNewDeclarator(Declarator &D) {
2115  // Parse the array dimensions.
2116  bool first = true;
2117  while (Tok.is(tok::l_square)) {
2118    SourceLocation LLoc = ConsumeBracket();
2119    ExprResult Size(first ? ParseExpression()
2120                                : ParseConstantExpression());
2121    if (Size.isInvalid()) {
2122      // Recover
2123      SkipUntil(tok::r_square);
2124      return;
2125    }
2126    first = false;
2127
2128    SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
2129
2130    ParsedAttributes attrs(AttrFactory);
2131    D.AddTypeInfo(DeclaratorChunk::getArray(0,
2132                                            /*static=*/false, /*star=*/false,
2133                                            Size.release(), LLoc, RLoc),
2134                  attrs, RLoc);
2135
2136    if (RLoc.isInvalid())
2137      return;
2138  }
2139}
2140
2141/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2142/// This ambiguity appears in the syntax of the C++ new operator.
2143///
2144///        new-expression:
2145///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2146///                                     new-initializer[opt]
2147///
2148///        new-placement:
2149///                   '(' expression-list ')'
2150///
2151bool Parser::ParseExpressionListOrTypeId(
2152                                   SmallVectorImpl<Expr*> &PlacementArgs,
2153                                         Declarator &D) {
2154  // The '(' was already consumed.
2155  if (isTypeIdInParens()) {
2156    ParseSpecifierQualifierList(D.getMutableDeclSpec());
2157    D.SetSourceRange(D.getDeclSpec().getSourceRange());
2158    ParseDeclarator(D);
2159    return D.isInvalidType();
2160  }
2161
2162  // It's not a type, it has to be an expression list.
2163  // Discard the comma locations - ActOnCXXNew has enough parameters.
2164  CommaLocsTy CommaLocs;
2165  return ParseExpressionList(PlacementArgs, CommaLocs);
2166}
2167
2168/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2169/// to free memory allocated by new.
2170///
2171/// This method is called to parse the 'delete' expression after the optional
2172/// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true
2173/// and "Start" is its location.  Otherwise, "Start" is the location of the
2174/// 'delete' token.
2175///
2176///        delete-expression:
2177///                   '::'[opt] 'delete' cast-expression
2178///                   '::'[opt] 'delete' '[' ']' cast-expression
2179ExprResult
2180Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2181  assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2182  ConsumeToken(); // Consume 'delete'
2183
2184  // Array delete?
2185  bool ArrayDelete = false;
2186  if (Tok.is(tok::l_square)) {
2187    ArrayDelete = true;
2188    SourceLocation LHS = ConsumeBracket();
2189    SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
2190    if (RHS.isInvalid())
2191      return ExprError();
2192  }
2193
2194  ExprResult Operand(ParseCastExpression(false));
2195  if (Operand.isInvalid())
2196    return move(Operand);
2197
2198  return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
2199}
2200
2201static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
2202  switch(kind) {
2203  default: assert(false && "Not a known unary type trait.");
2204  case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign;
2205  case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
2206  case tok::kw___has_nothrow_copy:           return UTT_HasNothrowCopy;
2207  case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign;
2208  case tok::kw___has_trivial_constructor:
2209                                    return UTT_HasTrivialDefaultConstructor;
2210  case tok::kw___has_trivial_copy:           return UTT_HasTrivialCopy;
2211  case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor;
2212  case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor;
2213  case tok::kw___is_abstract:             return UTT_IsAbstract;
2214  case tok::kw___is_arithmetic:              return UTT_IsArithmetic;
2215  case tok::kw___is_array:                   return UTT_IsArray;
2216  case tok::kw___is_class:                return UTT_IsClass;
2217  case tok::kw___is_complete_type:           return UTT_IsCompleteType;
2218  case tok::kw___is_compound:                return UTT_IsCompound;
2219  case tok::kw___is_const:                   return UTT_IsConst;
2220  case tok::kw___is_empty:                return UTT_IsEmpty;
2221  case tok::kw___is_enum:                 return UTT_IsEnum;
2222  case tok::kw___is_floating_point:          return UTT_IsFloatingPoint;
2223  case tok::kw___is_function:                return UTT_IsFunction;
2224  case tok::kw___is_fundamental:             return UTT_IsFundamental;
2225  case tok::kw___is_integral:                return UTT_IsIntegral;
2226  case tok::kw___is_lvalue_reference:        return UTT_IsLvalueReference;
2227  case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2228  case tok::kw___is_member_object_pointer:   return UTT_IsMemberObjectPointer;
2229  case tok::kw___is_member_pointer:          return UTT_IsMemberPointer;
2230  case tok::kw___is_object:                  return UTT_IsObject;
2231  case tok::kw___is_literal:              return UTT_IsLiteral;
2232  case tok::kw___is_literal_type:         return UTT_IsLiteral;
2233  case tok::kw___is_pod:                  return UTT_IsPOD;
2234  case tok::kw___is_pointer:                 return UTT_IsPointer;
2235  case tok::kw___is_polymorphic:          return UTT_IsPolymorphic;
2236  case tok::kw___is_reference:               return UTT_IsReference;
2237  case tok::kw___is_rvalue_reference:        return UTT_IsRvalueReference;
2238  case tok::kw___is_scalar:                  return UTT_IsScalar;
2239  case tok::kw___is_signed:                  return UTT_IsSigned;
2240  case tok::kw___is_standard_layout:         return UTT_IsStandardLayout;
2241  case tok::kw___is_trivial:                 return UTT_IsTrivial;
2242  case tok::kw___is_trivially_copyable:      return UTT_IsTriviallyCopyable;
2243  case tok::kw___is_union:                return UTT_IsUnion;
2244  case tok::kw___is_unsigned:                return UTT_IsUnsigned;
2245  case tok::kw___is_void:                    return UTT_IsVoid;
2246  case tok::kw___is_volatile:                return UTT_IsVolatile;
2247  }
2248}
2249
2250static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2251  switch(kind) {
2252  default: llvm_unreachable("Not a known binary type trait");
2253  case tok::kw___is_base_of:                 return BTT_IsBaseOf;
2254  case tok::kw___is_convertible:             return BTT_IsConvertible;
2255  case tok::kw___is_same:                    return BTT_IsSame;
2256  case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
2257  case tok::kw___is_convertible_to:          return BTT_IsConvertibleTo;
2258  }
2259}
2260
2261static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2262  switch(kind) {
2263  default: llvm_unreachable("Not a known binary type trait");
2264  case tok::kw___array_rank:                 return ATT_ArrayRank;
2265  case tok::kw___array_extent:               return ATT_ArrayExtent;
2266  }
2267}
2268
2269static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2270  switch(kind) {
2271  default: assert(false && "Not a known unary expression trait.");
2272  case tok::kw___is_lvalue_expr:             return ET_IsLValueExpr;
2273  case tok::kw___is_rvalue_expr:             return ET_IsRValueExpr;
2274  }
2275}
2276
2277/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2278/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2279/// templates.
2280///
2281///       primary-expression:
2282/// [GNU]             unary-type-trait '(' type-id ')'
2283///
2284ExprResult Parser::ParseUnaryTypeTrait() {
2285  UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2286  SourceLocation Loc = ConsumeToken();
2287
2288  SourceLocation LParen = Tok.getLocation();
2289  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2290    return ExprError();
2291
2292  // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2293  // there will be cryptic errors about mismatched parentheses and missing
2294  // specifiers.
2295  TypeResult Ty = ParseTypeName();
2296
2297  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2298
2299  if (Ty.isInvalid())
2300    return ExprError();
2301
2302  return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
2303}
2304
2305/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2306/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2307/// templates.
2308///
2309///       primary-expression:
2310/// [GNU]             binary-type-trait '(' type-id ',' type-id ')'
2311///
2312ExprResult Parser::ParseBinaryTypeTrait() {
2313  BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2314  SourceLocation Loc = ConsumeToken();
2315
2316  SourceLocation LParen = Tok.getLocation();
2317  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2318    return ExprError();
2319
2320  TypeResult LhsTy = ParseTypeName();
2321  if (LhsTy.isInvalid()) {
2322    SkipUntil(tok::r_paren);
2323    return ExprError();
2324  }
2325
2326  if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2327    SkipUntil(tok::r_paren);
2328    return ExprError();
2329  }
2330
2331  TypeResult RhsTy = ParseTypeName();
2332  if (RhsTy.isInvalid()) {
2333    SkipUntil(tok::r_paren);
2334    return ExprError();
2335  }
2336
2337  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2338
2339  return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
2340}
2341
2342/// ParseArrayTypeTrait - Parse the built-in array type-trait
2343/// pseudo-functions.
2344///
2345///       primary-expression:
2346/// [Embarcadero]     '__array_rank' '(' type-id ')'
2347/// [Embarcadero]     '__array_extent' '(' type-id ',' expression ')'
2348///
2349ExprResult Parser::ParseArrayTypeTrait() {
2350  ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2351  SourceLocation Loc = ConsumeToken();
2352
2353  SourceLocation LParen = Tok.getLocation();
2354  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2355    return ExprError();
2356
2357  TypeResult Ty = ParseTypeName();
2358  if (Ty.isInvalid()) {
2359    SkipUntil(tok::comma);
2360    SkipUntil(tok::r_paren);
2361    return ExprError();
2362  }
2363
2364  switch (ATT) {
2365  case ATT_ArrayRank: {
2366    SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2367    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, RParen);
2368  }
2369  case ATT_ArrayExtent: {
2370    if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2371      SkipUntil(tok::r_paren);
2372      return ExprError();
2373    }
2374
2375    ExprResult DimExpr = ParseExpression();
2376    SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2377
2378    return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), RParen);
2379  }
2380  default:
2381    break;
2382  }
2383  return ExprError();
2384}
2385
2386/// ParseExpressionTrait - Parse built-in expression-trait
2387/// pseudo-functions like __is_lvalue_expr( xxx ).
2388///
2389///       primary-expression:
2390/// [Embarcadero]     expression-trait '(' expression ')'
2391///
2392ExprResult Parser::ParseExpressionTrait() {
2393  ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2394  SourceLocation Loc = ConsumeToken();
2395
2396  SourceLocation LParen = Tok.getLocation();
2397  if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2398    return ExprError();
2399
2400  ExprResult Expr = ParseExpression();
2401
2402  SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2403
2404  return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), RParen);
2405}
2406
2407
2408/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2409/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2410/// based on the context past the parens.
2411ExprResult
2412Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
2413                                         ParsedType &CastTy,
2414                                         SourceLocation LParenLoc,
2415                                         SourceLocation &RParenLoc) {
2416  assert(getLang().CPlusPlus && "Should only be called for C++!");
2417  assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2418  assert(isTypeIdInParens() && "Not a type-id!");
2419
2420  ExprResult Result(true);
2421  CastTy = ParsedType();
2422
2423  // We need to disambiguate a very ugly part of the C++ syntax:
2424  //
2425  // (T())x;  - type-id
2426  // (T())*x; - type-id
2427  // (T())/x; - expression
2428  // (T());   - expression
2429  //
2430  // The bad news is that we cannot use the specialized tentative parser, since
2431  // it can only verify that the thing inside the parens can be parsed as
2432  // type-id, it is not useful for determining the context past the parens.
2433  //
2434  // The good news is that the parser can disambiguate this part without
2435  // making any unnecessary Action calls.
2436  //
2437  // It uses a scheme similar to parsing inline methods. The parenthesized
2438  // tokens are cached, the context that follows is determined (possibly by
2439  // parsing a cast-expression), and then we re-introduce the cached tokens
2440  // into the token stream and parse them appropriately.
2441
2442  ParenParseOption ParseAs;
2443  CachedTokens Toks;
2444
2445  // Store the tokens of the parentheses. We will parse them after we determine
2446  // the context that follows them.
2447  if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
2448    // We didn't find the ')' we expected.
2449    MatchRHSPunctuation(tok::r_paren, LParenLoc);
2450    return ExprError();
2451  }
2452
2453  if (Tok.is(tok::l_brace)) {
2454    ParseAs = CompoundLiteral;
2455  } else {
2456    bool NotCastExpr;
2457    // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2458    if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2459      NotCastExpr = true;
2460    } else {
2461      // Try parsing the cast-expression that may follow.
2462      // If it is not a cast-expression, NotCastExpr will be true and no token
2463      // will be consumed.
2464      Result = ParseCastExpression(false/*isUnaryExpression*/,
2465                                   false/*isAddressofOperand*/,
2466                                   NotCastExpr,
2467                                   // type-id has priority.
2468                                   true/*isTypeCast*/);
2469    }
2470
2471    // If we parsed a cast-expression, it's really a type-id, otherwise it's
2472    // an expression.
2473    ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
2474  }
2475
2476  // The current token should go after the cached tokens.
2477  Toks.push_back(Tok);
2478  // Re-enter the stored parenthesized tokens into the token stream, so we may
2479  // parse them now.
2480  PP.EnterTokenStream(Toks.data(), Toks.size(),
2481                      true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2482  // Drop the current token and bring the first cached one. It's the same token
2483  // as when we entered this function.
2484  ConsumeAnyToken();
2485
2486  if (ParseAs >= CompoundLiteral) {
2487    // Parse the type declarator.
2488    DeclSpec DS(AttrFactory);
2489    ParseSpecifierQualifierList(DS);
2490    Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2491    ParseDeclarator(DeclaratorInfo);
2492
2493    // Match the ')'.
2494    if (Tok.is(tok::r_paren))
2495      RParenLoc = ConsumeParen();
2496    else
2497      MatchRHSPunctuation(tok::r_paren, LParenLoc);
2498
2499    if (ParseAs == CompoundLiteral) {
2500      ExprType = CompoundLiteral;
2501      TypeResult Ty = ParseTypeName();
2502      return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2503    }
2504
2505    // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2506    assert(ParseAs == CastExpr);
2507
2508    if (DeclaratorInfo.isInvalidType())
2509      return ExprError();
2510
2511    // Result is what ParseCastExpression returned earlier.
2512    if (!Result.isInvalid())
2513      Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc,
2514                                     DeclaratorInfo, CastTy,
2515                                     RParenLoc, Result.take());
2516    return move(Result);
2517  }
2518
2519  // Not a compound literal, and not followed by a cast-expression.
2520  assert(ParseAs == SimpleExpr);
2521
2522  ExprType = SimpleExpr;
2523  Result = ParseExpression();
2524  if (!Result.isInvalid() && Tok.is(tok::r_paren))
2525    Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
2526
2527  // Match the ')'.
2528  if (Result.isInvalid()) {
2529    SkipUntil(tok::r_paren);
2530    return ExprError();
2531  }
2532
2533  if (Tok.is(tok::r_paren))
2534    RParenLoc = ConsumeParen();
2535  else
2536    MatchRHSPunctuation(tok::r_paren, LParenLoc);
2537
2538  return move(Result);
2539}
2540