Parser.cpp revision 68de140b5d1df4c21cbf5ef1be2abcbfbc835cb5
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Parser.cpp - C Language Family Parser ----------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the Parser interfaces.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h"
15500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
1619510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/Scope.h"
1819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h"
190102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner#include "llvm/Support/raw_ostream.h"
20d167ca0d26e43292b8b9e8d5300d92784ae0e27dChris Lattner#include "RAIIObjectsForParser.h"
21fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar#include "ParsePragma.h"
228387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet#include "clang/AST/DeclTemplate.h"
23f986038beed360c031de8654cfba43a5d3184605Francois Pichet#include "clang/AST/ASTConsumer.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
2768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha Snamespace clang {
2868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
2968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S/// \brief An instance of this interface is defined to initiate OpenMP parsing,
3068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S/// and to help Parser during subsequent parsing of OpenMP constrcuts.
3168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha Sclass OpenMP {
3268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // Private Member Objects.
3368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha Sprivate:
3468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // \brief The member object reference to the Preprocessor.
3568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  Preprocessor &PP;
3668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
3768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // \brief Handler objects which handle different OpenMP pragma directive
3868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // statements.
3968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> ParallelHandler;
4068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> ForHandler;
4168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> SectionsHandler;
4268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> SectionHandler;
4368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> SingleHandler;
4468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> TaskHandler;
4568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> MasterHandler;
4668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> CriticalHandler;
4768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> BarrierHandler;
4868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> TaskwaitHandler;
4968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> TaskyieldHandler;
5068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> AtomicHandler;
5168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> FlushHandler;
5268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> OrderedHandler;
5368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OwningPtr<PragmaHandler> ThreadPrivateHandler;
5468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
5568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // \brief When an OpenMP pragma is ignored, we emit a warning message
5668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // saying so, but only once per translation unit irrespective of the
5768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // number of OpenMP pragmas appeared in the translation unit. This flag
5868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // keeps track of whether the unkown pragma warning message is emitted
5968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // or not for the current translation unit.
6068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  bool OmpUnknownWarned;
6168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
6268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // Private Member Methods.
6368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha Sprivate:
6468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // \brief When source contains omp pragmas, but user does not pass the
6568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // '-fopenmp' flag, we emit a warning message saying so, but only once per
6668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // source file.
6768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  void SetPragmaOmpUnknownWarned() { OmpUnknownWarned = true; }
6868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  void ResetPragmaOmpUnknownWarned() { OmpUnknownWarned = false; }
6968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  bool PragmaOmpUnknownWarned() { return OmpUnknownWarned; }
7068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
7168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // \brief Handles "\#pragma omp ...".
7268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // It Checks if the user has passed the flag, '-fopenmp', if so,
7368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // it enters the *token* which is *representing* the current OpenMP
7468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // pragma *directive* into the TokenStream, so that the Parser can
7568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // recognizes it and can parse the respective OpenMP pragma directive
7668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // statement. Otherwise, it reports a warning that the current OpenMP
7768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // directive statement will be ignored. However, note that only
7868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // *one* warning message per translation unit is reported
7968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // irrespective of the number of OpenMP directive statments which
8068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // appear in the translation unit.
8168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  void HandlePragma(Token &OmpTok, tok::TokenKind TKind) {
8268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    if (!PP.getLangOpts().OpenMP) {
8368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S      if (!PragmaOmpUnknownWarned()) {
8468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S        PP.Diag(OmpTok, diag::warn_pragma_omp_ignored);
8568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S        SetPragmaOmpUnknownWarned();
8668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S      }
8768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    } else {
8868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S      Token *Toks = new Token[1];
8968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S      Toks[0].startToken();
9068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S      Toks[0].setKind(TKind);
9168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S      Toks[0].setLocation(OmpTok.getLocation());
9268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S      PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
9368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S                          /*OwnsTokens=*/false);
9468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    }
9568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  }
9668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
9768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // Constructors and Destructors.
9868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha Spublic:
9968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OpenMP(Preprocessor &pp);
10068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  ~OpenMP();
10168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
10268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha Sprivate:
10368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // \brief An OpenMP pragma handler interface. It acts as an intermediate
10468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // between the Preprocessor class and the OpenMP (this) class. It by passes
10568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // the omp pragma handling call to OpenMP class upon requested by the
10668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // Preprocessor.
10768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  struct PragmaOmpHandler : public PragmaHandler {
10868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    friend class OpenMP;
10968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    tok::TokenKind TKind;
11068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    OpenMP *Omp;
11168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    PragmaOmpHandler(tok::TokenKind Kind, StringRef Name, OpenMP *omp)
11268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    : PragmaHandler(Name), TKind(Kind), Omp(omp) {}
11368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    virtual ~PragmaOmpHandler() { Omp = 0; }
11468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
11568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S                              Token &OmpTok) {
11668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S      Omp->HandlePragma(OmpTok, TKind);
11768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S    }
11868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  };
11968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S};
12068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
12168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S} // end of namespace clang
12268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
12368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha SOpenMP::OpenMP(Preprocessor &pp)
12468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  : PP(pp) {
12568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
12668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // Add "#pragma omp ..." handlers. These are removed and destroyed
12768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // in the destructor.
12868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  //
12968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // Note that OpenMP pragma annotations below are listed in the same order
13068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // as listed in the OpenMP 3.1 standard specification document. Please
13168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // do *adhere* to the same order.
13268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
13368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP parallel constructs.
13468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  ParallelHandler.reset(new PragmaOmpHandler(
13568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_parallel, "parallel", this));
13668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", ParallelHandler.get());
13768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
13868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP work sharing constructs.
13968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  ForHandler.reset(new PragmaOmpHandler(
14068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_for, "for", this));
14168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", ForHandler.get());
14268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
14368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  SectionsHandler.reset(new PragmaOmpHandler(
14468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_sections, "sections", this));
14568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", SectionsHandler.get());
14668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
14768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  SingleHandler.reset(new PragmaOmpHandler(
14868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_single, "single", this));
14968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", SingleHandler.get());
15068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
15168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  SectionHandler.reset(new PragmaOmpHandler(
15268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_section, "section", this));
15368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", SectionHandler.get());
15468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
15568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP combined parallel work sharing constructs.
15668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // FIXME: not supported yet.
15768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
15868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP tasking constructs.
15968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  TaskHandler.reset(new PragmaOmpHandler(
16068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_task, "task", this));
16168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", TaskHandler.get());
16268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
16368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  TaskyieldHandler.reset(new PragmaOmpHandler(
16468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_taskyield, "taskyield", this));
16568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", TaskyieldHandler.get());
16668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
16768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP master and synchronization constructs.
16868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  MasterHandler.reset(new PragmaOmpHandler(
16968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_master, "master", this));
17068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", MasterHandler.get());
17168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
17268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  CriticalHandler.reset(new PragmaOmpHandler(
17368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_critical, "critical", this));
17468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", CriticalHandler.get());
17568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
17668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  BarrierHandler.reset(new PragmaOmpHandler(
17768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_barrier, "barrier", this));
17868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", BarrierHandler.get());
17968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
18068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  TaskwaitHandler.reset(new PragmaOmpHandler(
18168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_taskwait, "taskwait", this));
18268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", TaskwaitHandler.get());
18368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
18468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  AtomicHandler.reset(new PragmaOmpHandler(
18568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_atomic, "atomic", this));
18668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", AtomicHandler.get());
18768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
18868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  FlushHandler.reset(new PragmaOmpHandler(
18968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_flush, "flush", this));
19068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", FlushHandler.get());
19168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
19268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OrderedHandler.reset(new PragmaOmpHandler(
19368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_ordered, "ordered", this));
19468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", OrderedHandler.get());
19568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
19668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP data environment related constructs.
19768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  ThreadPrivateHandler.reset(new PragmaOmpHandler(
19868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S              tok::annot_pragma_omp_threadprivate, "threadprivate", this));
19968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.AddPragmaHandler("omp", ThreadPrivateHandler.get());
20068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
20168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // We have not yet reported the unknown OpenMP pragma *warning* message
20268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // as we have not yet started the processing of translation unit.
20368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  ResetPragmaOmpUnknownWarned();
20468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S}
20568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
20668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha SOpenMP::~OpenMP() {
20768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // Remove the "#pragma omp ..." handlers we installed.
20868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  //
20968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // Note that OpenMP pragma annotations below are listed in the same order
21068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // as listed in the OpenMP 3.1 standard specification document. Please
21168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // do *adhere* to the same order.
21268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
21368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP parallel constructs.
21468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", ParallelHandler.get());
21568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  ParallelHandler.reset();
21668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
21768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP work sharing constructs.
21868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", ForHandler.get());
21968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  ForHandler.reset();
22068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", SectionsHandler.get());
22168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  SectionsHandler.reset();
22268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", SingleHandler.get());
22368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  SingleHandler.reset();
22468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", SectionHandler.get());
22568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  SectionHandler.reset();
22668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
22768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP combined parallel work sharing constructs.
22868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // FIXME: Yet to support.
22968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
23068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP tasking constructs.
23168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", TaskHandler.get());
23268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  TaskHandler.reset();
23368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", TaskyieldHandler.get());
23468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  TaskyieldHandler.reset();
23568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
23668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP master and synchronization constructs.
23768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", MasterHandler.get());
23868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  MasterHandler.reset();
23968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", CriticalHandler.get());
24068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  CriticalHandler.reset();
24168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", BarrierHandler.get());
24268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  BarrierHandler.reset();
24368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", TaskwaitHandler.get());
24468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  TaskwaitHandler.reset();
24568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", AtomicHandler.get());
24668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  AtomicHandler.reset();
24768de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", FlushHandler.get());
24868de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  FlushHandler.reset();
24968de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", OrderedHandler.get());
25068de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OrderedHandler.reset();
25168de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
25268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  // OpenMP data environment related constructs.
25368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  PP.RemovePragmaHandler("omp", ThreadPrivateHandler.get());
25468de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  ThreadPrivateHandler.reset();
25568de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S}
25668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
25769b5e952c56f95673064ad1815a240e0fb595865Benjamin Kramernamespace {
258aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief A comment handler that passes comments found by the preprocessor
259aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// to the parser action.
260aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass ActionCommentHandler : public CommentHandler {
261aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  Sema &S;
262aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
263aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkopublic:
264aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  explicit ActionCommentHandler(Sema &S) : S(S) { }
265aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
266aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) {
267aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    S.ActOnComment(Comment);
268aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return false;
269aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
270aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko};
27169b5e952c56f95673064ad1815a240e0fb595865Benjamin Kramer} // end anonymous namespace
272aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
273b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas GregorIdentifierInfo *Parser::getSEHExceptKeyword() {
274b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor  // __except is accepted as a (contextual) keyword
2754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland))
276b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor    Ident__except = PP.getIdentifierInfo("__except");
277b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor
278b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor  return Ident__except;
279b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor}
280b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor
2816a91d385618ea4d28236c496f540a26877c95525Erik VerbruggenParser::Parser(Preprocessor &pp, Sema &actions, bool SkipFunctionBodies)
282614f96a7cf94805c2d336639300b62dc2f54e9e0Ted Kremenek  : PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
2830fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor    GreaterThanIsOperator(true), ColonIsSacred(false),
2846a91d385618ea4d28236c496f540a26877c95525Erik Verbruggen    InMessageExpression(false), TemplateParameterDepth(0),
28594f29f4bf5d6b49dd1b7fc16cfa1521adc0c71c0Jordan Rose    ParsingInObjCContainer(false), SkipFunctionBodies(SkipFunctionBodies) {
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Tok.setKind(tok::eof);
28723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = 0;
2889e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  NumCachedScopes = 0;
2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ParenCount = BracketCount = BraceCount = 0;
290849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  CurParsedObjCImpl = 0;
291fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar
292fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // Add #pragma handlers. These are removed and destroyed in the
293fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // destructor.
2949595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  AlignHandler.reset(new PragmaAlignHandler());
295cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  PP.AddPragmaHandler(AlignHandler.get());
296cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar
2979595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
298aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
299aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
3009595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  OptionsHandler.reset(new PragmaOptionsHandler());
3019b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(OptionsHandler.get());
3029b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis
3039595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  PackHandler.reset(new PragmaPackHandler());
3049b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(PackHandler.get());
30562c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian
3069595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  MSStructHandler.reset(new PragmaMSStructHandler());
30762c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  PP.AddPragmaHandler(MSStructHandler.get());
3089b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis
3099595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  UnusedHandler.reset(new PragmaUnusedHandler());
3109b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(UnusedHandler.get());
3119b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis
3129595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  WeakHandler.reset(new PragmaWeakHandler());
3139b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(WeakHandler.get());
314321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne
3159595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
3165f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall  PP.AddPragmaHandler(RedefineExtnameHandler.get());
3175f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall
3189595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  FPContractHandler.reset(new PragmaFPContractHandler());
319321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  PP.AddPragmaHandler("STDC", FPContractHandler.get());
320f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
3214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().OpenCL) {
3229595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
323f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
324f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
325f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
326f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
327aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
328056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko  CommentSemaHandler.reset(new ActionCommentHandler(actions));
329056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko  PP.addCommentHandler(CommentSemaHandler.get());
330aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
331f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  PP.setCodeCompletionHandler(*this);
33268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
33368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OpenMPHandler.reset(new OpenMP(pp));
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3360102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner/// If a crash happens while the parser is active, print out a line indicating
3370102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner/// what the current token is.
3385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
3390102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  const Token &Tok = P.getCurToken();
340ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner  if (Tok.is(tok::eof)) {
3410102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner    OS << "<eof> parser at end of file\n";
3420102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner    return;
3430102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  }
3441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
345ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner  if (Tok.getLocation().isInvalid()) {
346ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner    OS << "<unknown> parser at unknown location\n";
347ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner    return;
348ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner  }
3491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3500102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  const Preprocessor &PP = P.getPreprocessor();
3510102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  Tok.getLocation().print(OS, PP.getSourceManager());
3529fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar  if (Tok.isAnnotation())
3539fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar    OS << ": at annotation token \n";
3549fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar  else
3559fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar    OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
356f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor}
3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3580102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner
3593cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
36033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return Diags.Report(Loc, DiagID);
3611ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner}
3621ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner
3633cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
3641ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  return Diag(Tok.getLocation(), DiagID);
3655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3674b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \brief Emits a diagnostic suggesting parentheses surrounding a
3684b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// given range.
3694b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor///
3704b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param Loc The location where we'll emit the diagnostic.
37170517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko/// \param DK The kind of diagnostic to emit.
3724b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param ParenRange Source range enclosing code that should be parenthesized.
3734b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorvoid Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
3744b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor                                SourceRange ParenRange) {
375b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
376b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
3774b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // We can't display the parentheses, so just dig the
3784b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // warning/error and return.
3794b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Loc, DK);
3804b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    return;
3814b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
3821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Diag(Loc, DK)
384849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
385849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(EndLoc, ")");
3864b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor}
3874b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
388837b1a37116cf4e64f8bb7db34982dee1fba7647John McCallstatic bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
389837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  switch (ExpectedTok) {
3904b0824229b96d024a96f3c7dd75ab70652c05c5bRichard Smith  case tok::semi:
3914b0824229b96d024a96f3c7dd75ab70652c05c5bRichard Smith    return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ;
392837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  default: return false;
393837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  }
394837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall}
395837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
3965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// input.  If so, it is consumed and false is returned.
3985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If the input is malformed, this emits the specified diagnostic.  Next, if
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipToTok is specified, it calls SkipUntil(SkipToTok).  Finally, true is
4015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// returned.
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const char *Msg, tok::TokenKind SkipToTok) {
404dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
4055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeAnyToken();
4065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
4075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
408a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
409837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  // Detect common single-character typos and resume.
410837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  if (IsCommonTypo(ExpectedTok, Tok)) {
411837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    SourceLocation Loc = Tok.getLocation();
412837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    Diag(Loc, DiagID)
413837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall      << Msg
414837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall      << FixItHint::CreateReplacement(SourceRange(Loc),
415837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall                                      getTokenSimpleSpelling(ExpectedTok));
416837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    ConsumeAnyToken();
417837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
418837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    // Pretend there wasn't a problem.
419837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    return false;
420837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  }
421837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
4224b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  const char *Spelling = 0;
423b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (EndLoc.isValid() &&
425b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor      (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
4264b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // Show what code to insert to fix this problem.
4271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(EndLoc, DiagID)
4284b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor      << Msg
429849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateInsertion(EndLoc, Spelling);
4304b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  } else
4314b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Tok, DiagID) << Msg;
4324b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (SkipToTok != tok::unknown)
4345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(SkipToTok);
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4389ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregorbool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
4399ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
440fb5825dca4e95fee463fdeaddb8b729294fb4d10Douglas Gregor    ConsumeToken();
4419ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    return false;
4429ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  }
4439ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
4449ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
4459ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      NextToken().is(tok::semi)) {
4469ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    Diag(Tok, diag::err_extraneous_token_before_semi)
4479ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      << PP.getSpelling(Tok)
4489ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      << FixItHint::CreateRemoval(Tok.getLocation());
4499ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeAnyToken(); // The ')' or ']'.
4509ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeToken(); // The ';'.
4519ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    return false;
4529ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  }
4539ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
4549ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  return ExpectAndConsume(tok::semi, DiagID);
4559ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor}
4569ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
457eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smithvoid Parser::ConsumeExtraSemi(ExtraSemiKind Kind, unsigned TST) {
4584b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  if (!Tok.is(tok::semi)) return;
4594b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
460eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  bool HadMultipleSemis = false;
4614b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  SourceLocation StartLoc = Tok.getLocation();
4624b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  SourceLocation EndLoc = Tok.getLocation();
4634b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  ConsumeToken();
4644b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
4654b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) {
466eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    HadMultipleSemis = true;
4674b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    EndLoc = Tok.getLocation();
4684b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    ConsumeToken();
4694b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  }
4704b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
471eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  // C++11 allows extra semicolons at namespace scope, but not in any of the
472eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  // other contexts.
473eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  if (Kind == OutsideFunction && getLangOpts().CPlusPlus) {
474eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    if (getLangOpts().CPlusPlus0x)
475eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith      Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi)
476eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith          << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
477eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    else
478eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith      Diag(StartLoc, diag::ext_extra_semi_cxx11)
479eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith          << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
4804b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    return;
4814b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  }
4824b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
483eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis)
484eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    Diag(StartLoc, diag::ext_extra_semi)
485eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith        << Kind << DeclSpec::getSpecifierName((DeclSpec::TST)TST)
486eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith        << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
487eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  else
488eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    // A single semicolon is valid after a member function definition.
489eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def)
490eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith      << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
4914b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu}
4924b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
4945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Error recovery.
4955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
4965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipUntil - Read tokens until we get to the specified token, then consume
498012cf464254804279efa84e21b4b493dde76c5f1Chris Lattner/// it (unless DontConsume is true).  Because we cannot guarantee that the
4995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// token will ever occur, this skips to the next token, or to some likely
5005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// good stopping point.  If StopAtSemi is true, skipping will stop at a ';'
5015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character.
502a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///
5035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If SkipUntil finds the specified token, it returns true, otherwise it
504a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// returns false.
505eb52f86a62db523e3c993686b3ed92c55d59d53cDavid Blaikiebool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, bool StopAtSemi,
506eb52f86a62db523e3c993686b3ed92c55d59d53cDavid Blaikie                       bool DontConsume, bool StopAtCodeCompletion) {
5075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We always want this function to skip at least one token if the first token
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // isn't T and if not at EOF.
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isFirstTokenSkipped = true;
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we found one of the tokens, stop and return true.
512eb52f86a62db523e3c993686b3ed92c55d59d53cDavid Blaikie    for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) {
513000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(Toks[i])) {
5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (DontConsume) {
5155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Noop, don't consume the token.
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        } else {
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ConsumeAnyToken();
5185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
5195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return true;
5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
5215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
522a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Tok.getKind()) {
5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::eof:
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ran out of tokens.
5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
527dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
528dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    case tok::code_completion:
5293437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      if (!StopAtCodeCompletion)
5303437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis        ConsumeToken();
531dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      return false;
532dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
5335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_paren:
5345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested parens.
5355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
5363437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion);
5375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_square:
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested square brackets.
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
5413437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_square, false, false, StopAtCodeCompletion);
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_brace:
5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested braces.
5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
5463437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion);
5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
548a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
5505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Since the user wasn't looking for this token (if they were, it would
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // already be handled), this isn't balanced.  If there is a LHS token at a
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // higher level, we will assume that this matches the unbalanced token
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // and return it.  Otherwise, this is a spurious RHS token, which we skip.
5545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_paren:
5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ParenCount && !isFirstTokenSkipped)
5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
5575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
5595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_square:
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BracketCount && !isFirstTokenSkipped)
5615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_brace:
5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BraceCount && !isFirstTokenSkipped)
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
5675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
569a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
5705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::string_literal:
5715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::wide_string_literal:
5725cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf8_string_literal:
5735cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf16_string_literal:
5745cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf32_string_literal:
5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeStringToken();
5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
57755edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian
5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::semi:
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (StopAtSemi)
5805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip this token.
5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
5865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
5875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    isFirstTokenSkipped = false;
588a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump  }
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Scope manipulation
5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EnterScope - Start a new scope.
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::EnterScope(unsigned ScopeFlags) {
5979e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes) {
5989e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    Scope *N = ScopeCache[--NumCachedScopes];
59923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    N->Init(getCurScope(), ScopeFlags);
60023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CurScope = N;
6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
6029c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis    Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
6035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExitScope - Pop a scope off the scope stack.
6075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ExitScope() {
60823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() && "Scope imbalance!");
6095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
61090ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // Inform the actions module that this scope is going away if there are any
61190ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // decls in it.
61223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!getCurScope()->decl_empty())
61323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
614a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
61523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Scope *OldScope = getCurScope();
61623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = OldScope->getParent();
617a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
6189e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes == ScopeCacheSize)
6199e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete OldScope;
6205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
6219e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    ScopeCache[NumCachedScopes++] = OldScope;
6225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6247a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
6257a614d8380297fcd2bc23986241905d97222948cRichard Smith/// this object does nothing.
6267a614d8380297fcd2bc23986241905d97222948cRichard SmithParser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
6277a614d8380297fcd2bc23986241905d97222948cRichard Smith                                 bool ManageFlags)
6287a614d8380297fcd2bc23986241905d97222948cRichard Smith  : CurScope(ManageFlags ? Self->getCurScope() : 0) {
6297a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (CurScope) {
6307a614d8380297fcd2bc23986241905d97222948cRichard Smith    OldFlags = CurScope->getFlags();
6317a614d8380297fcd2bc23986241905d97222948cRichard Smith    CurScope->setFlags(ScopeFlags);
6327a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
6337a614d8380297fcd2bc23986241905d97222948cRichard Smith}
6345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6357a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Restore the flags for the current scope to what they were before this
6367a614d8380297fcd2bc23986241905d97222948cRichard Smith/// object overrode them.
6377a614d8380297fcd2bc23986241905d97222948cRichard SmithParser::ParseScopeFlags::~ParseScopeFlags() {
6387a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (CurScope)
6397a614d8380297fcd2bc23986241905d97222948cRichard Smith    CurScope->setFlags(OldFlags);
6407a614d8380297fcd2bc23986241905d97222948cRichard Smith}
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
6445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// C99 6.9: External Definitions.
6455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
6465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerParser::~Parser() {
6485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we still have scopes active, delete the scope tree.
64923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  delete getCurScope();
65023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = 0;
65123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
6525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Free the scope cache.
6539e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
6549e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete ScopeCache[i];
655fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar
6568387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Free LateParsedTemplatedFunction nodes.
6578387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin();
6588387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      it != LateParsedTemplateMap.end(); ++it)
6598387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    delete it->second;
6608387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
661fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // Remove the pragma handlers we installed.
662cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  PP.RemovePragmaHandler(AlignHandler.get());
663cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  AlignHandler.reset();
664aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
665aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  GCCVisibilityHandler.reset();
6669b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(OptionsHandler.get());
667861800c676004eabed5927f0552620d06c80a40aDaniel Dunbar  OptionsHandler.reset();
6689b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(PackHandler.get());
6694726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PackHandler.reset();
67062c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  PP.RemovePragmaHandler(MSStructHandler.get());
67162c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructHandler.reset();
6729b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(UnusedHandler.get());
6734726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  UnusedHandler.reset();
6749b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(WeakHandler.get());
6759991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman  WeakHandler.reset();
6765f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall  PP.RemovePragmaHandler(RedefineExtnameHandler.get());
6775f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall  RedefineExtnameHandler.reset();
678f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
6794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().OpenCL) {
680f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
681f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    OpenCLExtensionHandler.reset();
682f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
683f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
684f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
685321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  PP.RemovePragmaHandler("STDC", FPContractHandler.get());
686321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  FPContractHandler.reset();
687aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
688056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko  PP.removeCommentHandler(CommentSemaHandler.get());
689aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
690f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  PP.clearCodeCompletionHandler();
69113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer
69268de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S  OpenMPHandler.reset();
69368de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
69413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  assert(TemplateIds.empty() && "Still alive TemplateIdAnnotations around?");
6955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Initialize - Warm up the parser.
6985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
6995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::Initialize() {
70031e057270232c1c37602579cb6461c2704175672Chris Lattner  // Create the translation unit scope.  Install it as the current scope.
70123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() == 0 && "A scope is already active?");
70231e057270232c1c37602579cb6461c2704175672Chris Lattner  EnterScope(Scope::DeclScope);
703c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  Actions.ActOnTranslationUnitScope(getCurScope());
704c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor
705c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  // Prime the lexer look-ahead.
706c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  ConsumeToken();
707a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
70834870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  // Initialization for Objective-C context sensitive keywords recognition.
709a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  // Referenced in Parser::ParseObjCTypeQualifierList.
7104e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC1) {
711a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
712a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
713a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
714a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
715a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
716a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
71734870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  }
718662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar
719e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  Ident_instancetype = 0;
7207eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson  Ident_final = 0;
7217eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson  Ident_override = 0;
7221f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson
723662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar  Ident_super = &PP.getIdentifierTable().get("super");
72482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson
7254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().AltiVec) {
72682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_vector = &PP.getIdentifierTable().get("vector");
72782287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_pixel = &PP.getIdentifierTable().get("pixel");
72882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  }
7290a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
7300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_introduced = 0;
7310a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_deprecated = 0;
7320a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_obsoleted = 0;
733b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  Ident_unavailable = 0;
73428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
735b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor  Ident__except = 0;
736b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor
73728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0;
73828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0;
73928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0;
74028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
7414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if(getLangOpts().Borland) {
74228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__exception_info        = PP.getIdentifierInfo("_exception_info");
74328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___exception_info       = PP.getIdentifierInfo("__exception_info");
74428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_GetExceptionInfo       = PP.getIdentifierInfo("GetExceptionInformation");
74528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__exception_code        = PP.getIdentifierInfo("_exception_code");
74628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___exception_code       = PP.getIdentifierInfo("__exception_code");
74728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_GetExceptionCode       = PP.getIdentifierInfo("GetExceptionCode");
74828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__abnormal_termination  = PP.getIdentifierInfo("_abnormal_termination");
74928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
75028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_AbnormalTermination    = PP.getIdentifierInfo("AbnormalTermination");
75128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
75228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
75328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
75428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
75528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
75628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
75728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
75828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
75928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
76028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
76128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
7625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
76413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramernamespace {
76513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  /// \brief RAIIObject to destroy the contents of a SmallVector of
76613bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  /// TemplateIdAnnotation pointers and clear the vector.
76713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  class DestroyTemplateIdAnnotationsRAIIObj {
76813bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    SmallVectorImpl<TemplateIdAnnotation *> &Container;
76913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  public:
77013bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    DestroyTemplateIdAnnotationsRAIIObj(SmallVectorImpl<TemplateIdAnnotation *>
77113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer                                       &Container)
77213bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      : Container(Container) {}
77313bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer
77413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    ~DestroyTemplateIdAnnotationsRAIIObj() {
77513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      for (SmallVectorImpl<TemplateIdAnnotation *>::iterator I =
77613bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer           Container.begin(), E = Container.end();
77713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer           I != E; ++I)
77813bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer        (*I)->Destroy();
77913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      Container.clear();
78013bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    }
78113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  };
78213bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer}
78313bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer
7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// action tells us to.  This returns true if the EOF was encountered.
786682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattnerbool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
78713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
788b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
789e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  // Skip over the EOF token, flagging end of previous input for incremental
790e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  // processing
791e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof))
792e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    ConsumeToken();
793e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
794b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  while (Tok.is(tok::annot_pragma_unused))
795b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis    HandlePragmaUnused();
796b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
797682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  Result = DeclGroupPtrTy();
7989299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  if (Tok.is(tok::eof)) {
7998387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    // Late template parsing can begin.
8004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().DelayedTemplateParsing)
8018387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
802e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    if (!PP.isIncrementalProcessingEnabled())
803e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann      Actions.ActOnEndOfTranslationUnit();
804e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    //else don't tell Sema that we ended parsing: more input might come.
8058387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
8069299f3fa85796613cc787a2062c9562d07c8613eChris Lattner    return true;
8079299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  }
808a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8090b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributesWithRange attrs(AttrFactory);
8107f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseCXX0XAttributes(attrs);
8117f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseMicrosoftAttributes(attrs);
812e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
8137f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  Result = ParseExternalDeclaration(attrs);
8145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
8155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTranslationUnit:
8185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       translation-unit: [C99 6.9]
819a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///         external-declaration
820a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///         translation-unit external-declaration
8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseTranslationUnit() {
8228935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  Initialize();
823a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
824682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  DeclGroupPtrTy Res;
82589307ffaf8acf4d6fdffd72b607ca4fbcfdffc9dSteve Naroff  while (!ParseTopLevelDecl(Res))
8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /*parse them all*/;
8271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82806f548596beef4c0a227a45cba996497f99566c0Chris Lattner  ExitScope();
82923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() == 0 && "Scope imbalance!");
8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseExternalDeclaration:
83390b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner///
834c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
835c3018153a11afe91849748a93d920040a571b76cChris Lattner///         function-definition
836c3018153a11afe91849748a93d920040a571b76cChris Lattner///         declaration
837a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration
8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU]   asm-definition
839c3018153a11afe91849748a93d920040a571b76cChris Lattner/// [GNU]   __extension__ external-declaration
8405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
8415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-declaration
8425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-alias-declaration
8435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-protocol-definition
8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-method-definition
8455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  @end
846c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor/// [C++]   linkage-specification
8475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-definition:
8485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         simple-asm-expr ';'
8495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
850a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration:
851a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///           ';'
852a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///
85345f965581935791a018df829a14dff53c1dd8f47Douglas Gregor/// [C++0x/GNU] 'extern' 'template' declaration
8547f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::DeclGroupPtrTy
8557f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
8567f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                 ParsingDeclSpec *DS) {
85713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
85836d36806f1972f7ec1d2a3f59155187278c56508Argyrios Kyrtzidis  ParenBraceBracketBalancer BalancerRAIIObj(*this);
8597d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
8607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  if (PP.isCodeCompletionReached()) {
8617d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
8627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
8637d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  }
8647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
865d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *SingleDecl = 0;
8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Tok.getKind()) {
867426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola  case tok::annot_pragma_vis:
868426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola    HandlePragmaVisibility();
869426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola    return DeclGroupPtrTy();
870aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman  case tok::annot_pragma_pack:
871aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman    HandlePragmaPack();
872aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman    return DeclGroupPtrTy();
8739595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_msstruct:
8749595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaMSStruct();
8759595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
8769595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_align:
8779595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaAlign();
8789595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
8799595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_weak:
8809595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaWeak();
8819595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
8829595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_weakalias:
8839595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaWeakAlias();
8849595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
8859595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_redefine_extname:
8869595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaRedefineExtname();
8879595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
8889595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_fp_contract:
8899595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaFPContract();
8909595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
8919595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_opencl_extension:
8929595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaOpenCLExtension();
8939595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
8945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::semi:
8954b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    ConsumeExtraSemi(OutsideFunction);
8965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TODO: Invoke action for top-level semicolon.
897682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
89890b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::r_brace:
899883692ebd421c40b44e2c2665e5f54dade5621bcNico Weber    Diag(Tok, diag::err_extraneous_closing_brace);
90090b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    ConsumeBrace();
901682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
90290b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::eof:
90390b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    Diag(Tok, diag::err_expected_external_declaration);
904682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
905c3018153a11afe91849748a93d920040a571b76cChris Lattner  case tok::kw___extension__: {
906c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    // __extension__ silences extension warnings in the subexpression.
907c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
90839146d6497ad5e7ca8ef639221e7b3e15d07c888Chris Lattner    ConsumeToken();
9097f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    return ParseExternalDeclaration(attrs);
910c3018153a11afe91849748a93d920040a571b76cChris Lattner  }
911dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  case tok::kw_asm: {
9127f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ProhibitAttributes(attrs);
913bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
91421e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation StartLoc = Tok.getLocation();
91521e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation EndLoc;
91621e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    ExprResult Result(ParseSimpleAsm(&EndLoc));
917a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
9183f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson    ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
9193f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson                     "top-level asm block");
920dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson
921682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    if (Result.isInvalid())
922682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
92321e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
924682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
925dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  }
9265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::at:
92795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    return ParseObjCAtDirectives();
9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::minus:
9295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::plus:
9304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().ObjC1) {
931682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      Diag(Tok, diag::err_expected_external_declaration);
932682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      ConsumeToken();
933682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
934682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    }
935682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = ParseObjCMethodDefinition();
936682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
937791215b7a24666912c0b71175d2ca5ba082f666eDouglas Gregor  case tok::code_completion:
93823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
939849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                             CurParsedObjCImpl? Sema::PCC_ObjCImplementation
940f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                              : Sema::PCC_Namespace);
9417d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
9427d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
943f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  case tok::kw_using:
9448f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  case tok::kw_namespace:
9455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_typedef:
946adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_template:
947adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_export:    // As in 'export template'
948511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  case tok::kw_static_assert:
949c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne  case tok::kw__Static_assert:
95026d6023cb0d343bf8fc8836f97d39709bbd4afa0Chad Rosier    // A function definition cannot start with any of these keywords.
95197144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    {
95297144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner      SourceLocation DeclEnd;
9534e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer      StmtVector Stmts;
9547f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
95597144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    }
956d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
9577306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_static:
9587306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // Parse (then ignore) 'static' prior to a template instantiation. This is
9597306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // a GCC extension that we intentionally do not support.
9604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
9617306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
9627306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        << 0;
963d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl      SourceLocation DeclEnd;
9644e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer      StmtVector Stmts;
9657f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
9667306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    }
9677306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    goto dont_know;
9687306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
9697306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_inline:
9704e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus) {
9717306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      tok::TokenKind NextKind = NextToken().getKind();
9727306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
9737306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Inline namespaces. Allowed as an extension even in C++03.
9747306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_namespace) {
9757306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
9764e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer        StmtVector Stmts;
9777f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
9787306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
9797306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
9807306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Parse (then ignore) 'inline' prior to a template instantiation. This is
9817306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // a GCC extension that we intentionally do not support.
9827306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_template) {
9837306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
9847306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor          << 1;
9857306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
9864e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer        StmtVector Stmts;
9877f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
9887306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
989d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    }
990d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
991d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
99245f965581935791a018df829a14dff53c1dd8f47Douglas Gregor  case tok::kw_extern:
9934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
99445f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      // Extern templates
99545f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation ExternLoc = ConsumeToken();
99645f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation TemplateLoc = ConsumeToken();
9974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      Diag(ExternLoc, getLangOpts().CPlusPlus0x ?
9989324583ad2afd09db8c9967cd05c4fa44bac9555Richard Smith             diag::warn_cxx98_compat_extern_template :
9999324583ad2afd09db8c9967cd05c4fa44bac9555Richard Smith             diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
100045f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation DeclEnd;
100145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      return Actions.ConvertDeclToDeclGroup(
10029241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis                  ParseExplicitInstantiation(Declarator::FileContext,
10039241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis                                             ExternLoc, TemplateLoc, DeclEnd));
100445f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    }
100545f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    // FIXME: Detect C++ linkage specifications here?
1006d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
10071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1008f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_exists:
1009f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_not_exists:
1010563a645de82231a55e221fe655b7188bf8369662Francois Pichet    ParseMicrosoftIfExistsExternalDeclaration();
1011f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return DeclGroupPtrTy();
10126aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
10135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
1014d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl  dont_know:
10155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // We can't tell whether this is a function-definition or declaration yet.
10167f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (DS) {
10172edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt      return ParseDeclarationOrFunctionDefinition(attrs, DS);
10187f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    } else {
10197f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclarationOrFunctionDefinition(attrs);
10207f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    }
10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1023682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // This routine returns a DeclGroup, if the thing we parsed only contains a
1024682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // single decl, convert it now.
1025682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  return Actions.ConvertDeclToDeclGroup(SingleDecl);
10265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10281426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
10291426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, continues a declaration or declaration list.
1030e4246a633b13197634225971b25df0cbdcec0c5dSean Huntbool Parser::isDeclarationAfterDeclarator() {
1031e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  // Check for '= delete' or '= default'
10324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
1033e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
1034e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
1035e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      return false;
1036e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
10376c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian
10381426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor  return Tok.is(tok::equal) ||      // int X()=  -> not a function def
10391426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::comma) ||           // int X(),  -> not a function def
10401426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::semi)  ||           // int X();  -> not a function def
10411426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
10421426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
10434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    (getLangOpts().CPlusPlus &&
104439700f81c5b42e6be93be10275602915f872fc86Fariborz Jahanian     Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
10451426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
10461426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
10471426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
10481426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, indicates the start of a function definition.
1049004659a56916f2f81ede507c12516c146d6c0df3Chris Lattnerbool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
1050075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
10515d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  if (Tok.is(tok::l_brace))   // int X() {}
10525d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner    return true;
10535d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner
1054004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  // Handle K&R C argument lists: int X(f) int f; {}
10554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CPlusPlus &&
1056075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      Declarator.getFunctionTypeInfo().isKNRPrototype())
1057004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner    return isDeclarationSpecifier();
1058e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
10594e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
1060e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
1061e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
1062e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
1063004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner
10645d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
10655d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner         Tok.is(tok::kw_try);          // X() try { ... }
10661426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
10671426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// a declaration.  We can't tell which we have until we read up to the
1070c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// compound-statement in function-definition. TemplateParams, if
1071c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// non-NULL, provides the template parameters when we're parsing a
10721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ template-declaration.
10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       function-definition: [C99 6.9.1]
1075a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
1076a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
1077a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
1078a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///
10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       declaration: [C99 6.7]
1080697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner///         declaration-specifiers init-declarator-list[opt] ';'
1081697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner/// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OMP]   threadprivate-directive                              [TODO]
10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1084682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerParser::DeclGroupPtrTy
10852edf0a2520313cde900799b1eb9bd11c9c776afeSean HuntParser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs,
10862edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                       ParsingDeclSpec &DS,
10872edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                       AccessSpecifier AS) {
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Parse the common declaration-specifiers piece.
10890efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
1090a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // declaration-specifiers init-declarator-list[opt] ';'
1093000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(tok::semi)) {
10942edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ProhibitAttributes(attrs);
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
1096d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
109754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.complete(TheDecl);
1098682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1100a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11012edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  DS.takeAttributesFrom(attrs);
11022edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
1103246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // ObjC2 allows prefix attributes on class interfaces and protocols.
1104246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // FIXME: This still needs better diagnostics. We should only accept
1105246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // attributes here, no types, etc.
11064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC2 && Tok.is(tok::at)) {
1107dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation AtLoc = ConsumeToken(); // the "@"
11081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
1109246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar        !Tok.isObjCAtKeyword(tok::objc_protocol)) {
1110246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar      Diag(Tok, diag::err_objc_unexpected_attr);
1111cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      SkipUntil(tok::semi); // FIXME: better skip?
1112682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
1113cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    }
1114d8ac05753dc4506224d445ff98399c01da3136e5John McCall
111554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.abort();
111654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
11170de2ae28c603322f05e2d9200c7d457c8b928983Fariborz Jahanian    const char *PrevSpec = 0;
1118fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
1119fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
1120fec54013fcd0eb72642741584ca04c1bc292bef8John McCall      Diag(AtLoc, DiagID) << PrevSpec;
11211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1122246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar    if (Tok.isObjCAtKeyword(tok::objc_protocol))
1123bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor      return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
1124bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor
1125bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return Actions.ConvertDeclToDeclGroup(
1126bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor            ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
1127dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
1128a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1129c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // If the declspec consisted only of 'extern' and we have a string
1130c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // literal following it, this must be a C++ linkage specifier like
1131c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // 'extern "C"'.
11324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Tok.is(tok::string_literal) && getLangOpts().CPlusPlus &&
1133c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner      DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
1134682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
1135d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
1136682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
1137682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  }
1138c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
1139d8ac05753dc4506224d445ff98399c01da3136e5John McCall  return ParseDeclGroup(DS, Declarator::FileContext, true);
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11423acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::DeclGroupPtrTy
11432edf0a2520313cde900799b1eb9bd11c9c776afeSean HuntParser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs,
11442edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                             ParsingDeclSpec *DS,
11453acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian                                             AccessSpecifier AS) {
11462edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  if (DS) {
11472edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    return ParseDeclOrFunctionDefInternal(attrs, *DS, AS);
11482edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  } else {
11492edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ParsingDeclSpec PDS(*this);
11502edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // Must temporarily exit the objective-c container scope for
11512edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // parsing c constructs and re-enter objc container scope
11522edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // afterwards.
11532edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ObjCDeclContextSwitch ObjCDC(*this);
11542edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
11552edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    return ParseDeclOrFunctionDefInternal(attrs, PDS, AS);
11562edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  }
11573acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian}
11583acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseFunctionDefinition - We parsed and verified that the specified
11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Declarator is well formed.  If this is a K&R-style function, read the
11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// parameters declaration-list, then start the compound-statement.
11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1163a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///       function-definition: [C99 6.9.1]
1164a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
1165a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
1166a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
11677ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
116823c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
116923c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         function-body
11707ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
1171d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl///         decl-specifier-seq[opt] declarator function-try-block
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1173d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
1174c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins                                      const ParsedTemplateInfo &TemplateInfo,
1175c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins                                      LateParsedAttrList *LateParsedAttrs) {
117628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // Poison the SEH identifiers so they are flagged as illegal in function bodies
117728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
1178075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
1179a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1180a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // If this is C90 and the declspecs were completely missing, fudge in an
1181a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // implicit int.  We do this here because this is the only place where
1182a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // declaration-specifiers are completely optional in the grammar.
11834e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
1184a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner    const char *PrevSpec;
1185fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
118631c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner    D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
118731c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner                                           D.getIdentifierLoc(),
1188fec54013fcd0eb72642741584ca04c1bc292bef8John McCall                                           PrevSpec, DiagID);
1189ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
1190a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  }
1191a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this declaration was formed with a K&R-style identifier list for the
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // arguments, parse declarations for all of the args next.
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // int foo(a,b) int a; float b; {}
1195004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  if (FTI.isKNRPrototype())
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseKNRParamDeclarations(D);
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11987ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // We should have either an opening brace or, in a C++ constructor,
11997ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // we may have a colon.
1200758afbcc86ef15f8d433f5f87db1495e50effeb3Douglas Gregor  if (Tok.isNot(tok::l_brace) &&
12014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      (!getLangOpts().CPlusPlus ||
1202cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt       (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
1203cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        Tok.isNot(tok::equal)))) {
12045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::err_expected_fn_body);
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(tok::l_brace, true, true);
1208a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we didn't find the '{', bail out.
1210000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.isNot(tok::l_brace))
1211d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1213a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1214c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // Check to make sure that any normal attributes are allowed to be on
1215c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // a definition.  Late parsed attributes are checked at the end.
1216c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  if (Tok.isNot(tok::equal)) {
1217c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    AttributeList *DtorAttrs = D.getAttributes();
1218c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    while (DtorAttrs) {
1219c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins      if (!IsThreadSafetyAttribute(DtorAttrs->getName()->getName())) {
1220c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins        Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition)
1221c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins          << DtorAttrs->getName()->getName();
1222c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins      }
1223c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins      DtorAttrs = DtorAttrs->getNext();
1224c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    }
1225c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  }
1226c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins
12278387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // In delayed template parsing mode, for function template we consume the
12288387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // tokens and store them for late parsing at the end of the translation unit.
12294e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().DelayedTemplateParsing &&
12300963017dcbc32176c79a251c3ab23bc35ac784e5Douglas Gregor      Tok.isNot(tok::equal) &&
12318387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      TemplateInfo.Kind == ParsedTemplateInfo::Template) {
12325354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
12338387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
12348387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
12358387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Scope *ParentScope = getCurScope()->getParent();
12368387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
123745fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor    D.setFunctionDefinitionKind(FDK_Definition);
12388387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Decl *DP = Actions.HandleDeclarator(ParentScope, D,
12393fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                        TemplateParameterLists);
12408387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.complete(DP);
12418387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.getMutableDeclSpec().abort();
12428387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
12438387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (DP) {
1244e1fca502e7f1349e9b4520a4ca9a02413bcf2b14Francois Pichet      LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(DP);
12458387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
12468387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      FunctionDecl *FnD = 0;
12478387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
12488387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = FunTmpl->getTemplatedDecl();
12498387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      else
12508387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = cast<FunctionDecl>(DP);
1251d4a0caf78e7c18e7aca65fbfd799a6c024ff51fbFrancois Pichet      Actions.CheckForFunctionRedefinition(FnD);
12528387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
12538387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateParsedTemplateMap[FnD] = LPT;
12548387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.MarkAsLateParsedTemplate(FnD);
12558387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(LPT->Toks);
12568387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    } else {
12578387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      CachedTokens Toks;
12588387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(Toks);
12598387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    }
12608387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return DP;
12618387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
12622eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian  else if (CurParsedObjCImpl &&
12639e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian           !TemplateInfo.TemplateParams &&
12649e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian           (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
12659e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian            Tok.is(tok::colon)) &&
1266be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      Actions.CurContext->isTranslationUnit()) {
1267be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1268be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    Scope *ParentScope = getCurScope()->getParent();
1269be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian
1270be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.setFunctionDefinitionKind(FDK_Definition);
1271be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
12725354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                              MultiTemplateParamsArg());
1273be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.complete(FuncDecl);
1274be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.getMutableDeclSpec().abort();
1275be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    if (FuncDecl) {
1276be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      // Consume the tokens and store them for later parsing.
1277be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      StashAwayMethodOrFunctionBodyTokens(FuncDecl);
1278be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      CurParsedObjCImpl->HasCFunction = true;
1279be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      return FuncDecl;
1280be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    }
1281be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian  }
1282be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian
1283b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Enter a scope for the function body.
12848935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1285a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1286b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Tell the actions module that we have entered a function definition with the
1287b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // specified Declarator for the function.
1288d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Res = TemplateInfo.TemplateParams?
128923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
12905354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                              *TemplateInfo.TemplateParams, D)
129123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
1292a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
129354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclarator context before we parse the body.
129454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.complete(Res);
129554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
129654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclSpec context, too.  This const_cast is
129754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // safe because we're always the sole owner.
129854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.getMutableDeclSpec().abort();
129954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
1300cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  if (Tok.is(tok::equal)) {
13014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
1302cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ConsumeToken();
1303cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1304cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    Actions.ActOnFinishFunctionBody(Res, 0, false);
1305cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1306cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    bool Delete = false;
1307cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    SourceLocation KWLoc;
1308cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Tok.is(tok::kw_delete)) {
13094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      Diag(Tok, getLangOpts().CPlusPlus0x ?
13107fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith           diag::warn_cxx98_compat_deleted_function :
1311d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith           diag::ext_deleted_function);
1312cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1313cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      KWLoc = ConsumeToken();
1314cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Actions.SetDeclDeleted(Res, KWLoc);
1315cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Delete = true;
1316cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else if (Tok.is(tok::kw_default)) {
13174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      Diag(Tok, getLangOpts().CPlusPlus0x ?
13187fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith           diag::warn_cxx98_compat_defaulted_function :
1319d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith           diag::ext_defaulted_function);
1320cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1321cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      KWLoc = ConsumeToken();
1322cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Actions.SetDeclDefaulted(Res, KWLoc);
1323cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else {
1324cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      llvm_unreachable("function definition after = not 'delete' or 'default'");
1325cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
1326cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1327cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Tok.is(tok::comma)) {
1328cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
1329cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        << Delete;
1330cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      SkipUntil(tok::semi);
1331cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else {
1332cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1333cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                       Delete ? "delete" : "default", tok::semi);
1334cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
1335cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1336cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    return Res;
1337cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  }
1338cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1339d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (Tok.is(tok::kw_try))
1340c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor    return ParseFunctionTryBlock(Res, BodyScope);
1341d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
13427ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // If we have a colon, then we're probably parsing a C++
13437ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // ctor-initializer.
1344d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  if (Tok.is(tok::colon)) {
13457ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    ParseConstructorInitializer(Res);
1346d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall
1347d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    // Recover from error.
1348d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    if (!Tok.is(tok::l_brace)) {
1349c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor      BodyScope.Exit();
13509ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.ActOnFinishFunctionBody(Res, 0);
1351d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall      return Res;
1352d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    }
1353d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  } else
1354393612e6c7727f1fee50039254d9f434364cc0b2Fariborz Jahanian    Actions.ActOnDefaultCtorInitializers(Res);
13557ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
1356c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // Late attributes are parsed in the same scope as the function body.
1357c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  if (LateParsedAttrs)
1358c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
1359c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins
1360c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor  return ParseFunctionStatementBody(Res, BodyScope);
13615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
13645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// types for a function with a K&R-style identifier list for arguments.
13655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseKNRParamDeclarations(Declarator &D) {
13665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We know that the top-level of this declarator is a function.
1367075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
13685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
136904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // Enter function-declaration scope, limiting any declarators to the
137004421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // function prototype scope, including parameter declarators.
13713218c4bb3b5d7250f12420de6db7ef3e3f805a75Douglas Gregor  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
137204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner
13735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read all the argument declarations.
13745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isDeclarationSpecifier()) {
13755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SourceLocation DSStart = Tok.getLocation();
1376a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
13775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the common declaration-specifiers piece.
13780b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
13795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarationSpecifiers(DS);
1380a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: 'each declaration in the declaration list shall have at
13825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // least one declarator'.
13835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
13845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // the declarations though.  It's trivial to ignore them, really hard to do
13855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // anything else with them.
1386000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.is(tok::semi)) {
13875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DSStart, diag::err_declaration_does_not_declare_param);
13885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
13895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
13905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1391a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
13925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
13935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // than register.
13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
13955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        DS.getStorageClassSpec() != DeclSpec::SCS_register) {
13965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getStorageClassSpecLoc(),
13975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
13985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
13995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
14005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.isThreadSpecified()) {
14015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getThreadSpecLoc(),
14025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
14035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
14045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1405a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
14065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the first declarator attached to this declspec.
14075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
14085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarator(ParmDeclarator);
14095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Handle the full declarator list.
14115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (1) {
14125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If attributes are present, parse them.
14137f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      MaybeParseGNUAttributes(ParmDeclarator);
1414a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
14155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ask the actions module to compute the type for this declarator.
1416d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *Param =
141723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
14182bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff
1419a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump      if (Param &&
14205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // A missing identifier has already been diagnosed.
14215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ParmDeclarator.getIdentifier()) {
14225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Scan the argument list looking for the correct param to apply this
14245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // type.
14255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0; ; ++i) {
14265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // C99 6.9.1p6: those declarators shall declare only identifiers from
14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // the identifier list.
14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (i == FTI.NumArgs) {
14291ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner            Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
14306898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner              << ParmDeclarator.getIdentifier();
14315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
1433a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Reject redefinitions of parameters.
143604421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner            if (FTI.ArgInfo[i].Param) {
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(ParmDeclarator.getIdentifierLoc(),
14381ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner                   diag::err_param_redefinition)
14396898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner                 << ParmDeclarator.getIdentifier();
14405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            } else {
144104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner              FTI.ArgInfo[i].Param = Param;
14425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
14435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
14445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we don't have a comma, it is either the end of the list (a ';') or
14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // an error, bail out.
1450000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.isNot(tok::comma))
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
1452a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
14537984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith      ParmDeclarator.clear();
14547984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith
14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Consume the comma.
14567984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith      ParmDeclarator.setCommaLoc(ConsumeToken());
1457a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
14585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Parse the next declarator.
14595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParseDeclarator(ParmDeclarator);
14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1461a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
14628bb21d32e9ccc9d9c221506dff26acafa8724ccaChris Lattner    if (ExpectAndConsumeSemi(diag::err_expected_semi_declaration)) {
14635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip to end of block or statement
14645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi, true);
1465000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(tok::semi))
14665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ConsumeToken();
14675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
14685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1469a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
14705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The actions module must verify that all arguments were declared.
147123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
14725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
14765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// allowed to be a wide string, and is not subject to character translation.
14775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
14785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-string-literal:
14795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         string-literal
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
148160d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseAsmStringLiteral() {
14827f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek  switch (Tok.getKind()) {
14837f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    case tok::string_literal:
14847f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      break;
148599831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf8_string_literal:
148699831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf16_string_literal:
148799831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf32_string_literal:
14887f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    case tok::wide_string_literal: {
14897f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      SourceLocation L = Tok.getLocation();
14907f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      Diag(Tok, diag::err_asm_operand_wide_string_literal)
149199831e4677a7e2e051af636221694d60ba31fcdbRichard Smith        << (Tok.getKind() == tok::wide_string_literal)
14927f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek        << SourceRange(L, L);
14937f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      return ExprError();
14947f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    }
14957f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    default:
14967f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      Diag(Tok, diag::err_expected_string_literal);
14977f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      return ExprError();
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1499a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
150099831e4677a7e2e051af636221694d60ba31fcdbRichard Smith  return ParseStringLiteralExpression();
15015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseSimpleAsm
15045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] simple-asm-expr:
15065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'asm' '(' asm-string-literal ')'
15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
150860d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
1509000732226610650837478cba97843d19b75f648eChris Lattner  assert(Tok.is(tok::kw_asm) && "Not an asm!");
1510dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  SourceLocation Loc = ConsumeToken();
1511a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
15127a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  if (Tok.is(tok::kw_volatile)) {
1513841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    // Remove from the end of 'asm' to the end of 'volatile'.
1514841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1515841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall                             PP.getLocForEndOfToken(Tok.getLocation()));
1516841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall
1517841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    Diag(Tok, diag::warn_file_asm_volatile)
1518849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateRemoval(RemovalRange);
15197a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall    ConsumeToken();
15207a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  }
15217a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall
15224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
15234a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
15241ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "asm";
152561364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl    return ExprError();
15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1527a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
152860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(ParseAsmStringLiteral());
1529a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1530ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Result.isInvalid()) {
1531ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SkipUntil(tok::r_paren, true, true);
1532ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1533ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Tok.getLocation();
1534ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    ConsumeAnyToken();
1535ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  } else {
15364a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    // Close the paren and get the location of the end bracket
15374a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
1538ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
15394a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      *EndLoc = T.getCloseLocation();
1540ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
1541a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
15423fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
15435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
154525a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// \brief Get the TemplateIdAnnotation from the token and put it in the
154625a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// cleanup pool so that it gets destroyed when parsing the current top level
154725a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// declaration is finished.
154825a767651d14db87aa03dd5fe3e011d877dd4100Argyrios KyrtzidisTemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
154925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  assert(tok.is(tok::annot_template_id) && "Expected template-id token");
155025a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  TemplateIdAnnotation *
155125a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
155225a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  return Id;
155325a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis}
155425a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis
15550576681bac125be07f77f66b02a3dba2c3a24557Richard Smithvoid Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {
15560576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Push the current token back into the token stream (or revert it if it is
15570576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // cached) and use an annotation scope token for current token.
15580576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (PP.isBacktrackEnabled())
15590576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.RevertCachedTokens(1);
15600576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  else
15610576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.EnterToken(Tok);
15620576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setKind(tok::annot_cxxscope);
15630576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
15640576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setAnnotationRange(SS.getRange());
15650576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
15660576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // In case the tokens were cached, have Preprocessor replace them
15670576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // with the annotation token.  We don't need to do this if we've
15680576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // just reverted back to a prior state.
15690576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (IsNewAnnotation)
15700576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
15710576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
15720576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
15730576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \brief Attempt to classify the name at the current token position. This may
15740576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// form a type, scope or primary expression annotation, or replace the token
15750576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// with a typo-corrected keyword. This is only appropriate when the current
15760576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// name must refer to an entity which has already been declared.
15770576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///
15780576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \param IsAddressOfOperand Must be \c true if the name is preceded by an '&'
15790576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///        and might possibly have a dependent nested name specifier.
15800576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \param CCC Indicates how to perform typo-correction for this name. If NULL,
15810576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///        no typo correction will be performed.
15820576681bac125be07f77f66b02a3dba2c3a24557Richard SmithParser::AnnotatedNameKind
15830576681bac125be07f77f66b02a3dba2c3a24557Richard SmithParser::TryAnnotateName(bool IsAddressOfOperand,
15840576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                        CorrectionCandidateCallback *CCC) {
15850576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));
15860576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
15870576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  const bool EnteringContext = false;
15880576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
15890576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
15900576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  CXXScopeSpec SS;
15910576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (getLangOpts().CPlusPlus &&
15920576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
15930576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Error;
15940576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
15950576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
15960576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
15970576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                  !WasScopeAnnotation))
15980576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
15990576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Unresolved;
16000576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
16010576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16020576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  IdentifierInfo *Name = Tok.getIdentifierInfo();
16030576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  SourceLocation NameLoc = Tok.getLocation();
16040576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16050576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // FIXME: Move the tentative declaration logic into ClassifyName so we can
16060576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // typo-correct to tentatively-declared identifiers.
16070576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (isTentativelyDeclared(Name)) {
16080576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // Identifier has been tentatively declared, and thus cannot be resolved as
16090576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // an expression. Fall back to annotating it as a type.
16100576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
16110576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                  !WasScopeAnnotation))
16120576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
16130576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl;
16140576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
16150576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16160576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Token Next = NextToken();
16170576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16180576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Look up and classify the identifier. We don't perform any typo-correction
16190576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // after a scope specifier, because in general we can't recover from typos
16200576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // there (eg, after correcting 'A::tempalte B<X>::C', we would need to jump
16210576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // back into scope specifier parsing).
16220576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Sema::NameClassification Classification
16230576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next,
16240576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                           IsAddressOfOperand, SS.isEmpty() ? CCC : 0);
16250576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16260576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  switch (Classification.getKind()) {
16270576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Error:
16280576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Error;
16290576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16300576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Keyword:
16310576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // The identifier was typo-corrected to a keyword.
16320576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setIdentifierInfo(Name);
16330576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(Name->getTokenID());
16340576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.TypoCorrectToken(Tok);
16350576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
16360576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      AnnotateScopeToken(SS, !WasScopeAnnotation);
16370576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // We've "annotated" this as a keyword.
16380576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
16390576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16400576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Unknown:
16410576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // It's not something we know about. Leave it unannotated.
16420576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    break;
16430576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16440576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Type:
16450576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(tok::annot_typename);
16460576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    setTypeAnnotation(Tok, Classification.getType());
16470576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setAnnotationEndLoc(NameLoc);
16480576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
16490576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      Tok.setLocation(SS.getBeginLoc());
16500576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
16510576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
16520576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16530576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Expression:
16540576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(tok::annot_primary_expr);
16550576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    setExprAnnotation(Tok, Classification.getExpression());
16560576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setAnnotationEndLoc(NameLoc);
16570576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
16580576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      Tok.setLocation(SS.getBeginLoc());
16590576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
16600576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
16610576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16620576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_TypeTemplate:
16630576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (Next.isNot(tok::less)) {
16640576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      // This may be a type template being used as a template template argument.
16650576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      if (SS.isNotEmpty())
16660576681bac125be07f77f66b02a3dba2c3a24557Richard Smith        AnnotateScopeToken(SS, !WasScopeAnnotation);
16670576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_TemplateName;
16680576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    }
16690576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // Fall through.
16700576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_FunctionTemplate: {
16710576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // We have a type or function template followed by '<'.
16720576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    ConsumeToken();
16730576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    UnqualifiedId Id;
16740576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Id.setIdentifier(Name, NameLoc);
16750576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (AnnotateTemplateIdToken(
16760576681bac125be07f77f66b02a3dba2c3a24557Richard Smith            TemplateTy::make(Classification.getTemplateName()),
16770576681bac125be07f77f66b02a3dba2c3a24557Richard Smith            Classification.getTemplateNameKind(), SS, SourceLocation(), Id))
16780576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
16790576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
16800576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
16810576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16820576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_NestedNameSpecifier:
16830576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    llvm_unreachable("already parsed nested name specifier");
16840576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
16850576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
16860576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Unable to classify the name, but maybe we can annotate a scope specifier.
16870576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (SS.isNotEmpty())
16880576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    AnnotateScopeToken(SS, !WasScopeAnnotation);
16890576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  return ANK_Unresolved;
16900576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
16910576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
1692eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateTypeOrScopeToken - If the current token position is on a
1693eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typename (possibly qualified in C++) or a C++ scope specifier not followed
1694eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1695eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// with a single annotation token representing the typename or C++ scope
1696eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// respectively.
1697eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This simplifies handling of C++ scope specifiers and allows efficient
1698eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// backtracking without the need to re-parse and resolve nested-names and
1699eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typenames.
170044802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// It will mainly be called when we expect to treat identifiers as typenames
170144802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// (if they are typenames). For example, in C we do not expect identifiers
170244802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// inside expressions to be treated as typenames so it will not be called
170344802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// for expressions in C.
170444802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// The benefit for C/ObjC is that a typename will be annotated and
1705b43a50ff1b0b171ece84425b0ad83a9a31f038faSteve Naroff/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
170644802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// will not be called twice, once to check whether we have a declaration
170744802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// specifier, and another one to get the actual type inside
170844802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// ParseDeclarationSpecifiers).
1709a7bc7c880f86bc180684ef032d06df51bcae7a23Chris Lattner///
17109ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// This returns true if an error occurred.
17111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
171255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
171355a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1714fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrainbool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
171642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie          || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)
171723756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          || Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id))
171823756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          && "Cannot be a type or scope token!");
17191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1720d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Tok.is(tok::kw_typename)) {
1721d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Parse a C++ typename-specifier, e.g., "typename T::type".
1722d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //
1723d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //   typename-specifier:
1724d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //     'typename' '::' [opt] nested-name-specifier identifier
17251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     'typename' '::' [opt] nested-name-specifier template [opt]
17261734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    //            simple-template-id
1727d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    SourceLocation TypenameLoc = ConsumeToken();
1728d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    CXXScopeSpec SS;
1729efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(),
1730efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor                                       /*EnteringContext=*/false,
17314147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                       0, /*IsTypename*/true))
17329ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
17339ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    if (!SS.isSet()) {
1734b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet      if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
1735b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet          Tok.is(tok::annot_decltype)) {
173623756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith        // Attempt to recover by skipping the invalid 'typename'
1737b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet        if (Tok.is(tok::annot_decltype) ||
1738b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet            (!TryAnnotateTypeOrScopeToken(EnteringContext, NeedType) &&
1739b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet            Tok.isAnnotation())) {
174023756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          unsigned DiagID = diag::err_expected_qualified_after_typename;
174123756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          // MS compatibility: MSVC permits using known types with typename.
174223756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          // e.g. "typedef typename T* pointer_type"
174323756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          if (getLangOpts().MicrosoftExt)
174423756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith            DiagID = diag::warn_expected_qualified_after_typename;
174523756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          Diag(Tok.getLocation(), DiagID);
174623756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          return false;
174723756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith        }
174823756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith      }
174923756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith
175023756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith      Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
17519ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1752d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    }
1753d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1754d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    TypeResult Ty;
1755d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (Tok.is(tok::identifier)) {
1756d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      // FIXME: check whether the next token is '<', first!
175723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
17581a15dae8be2b28e02b6639aa92b832465c5be420Douglas Gregor                                     *Tok.getIdentifierInfo(),
1759d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     Tok.getLocation());
17601734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else if (Tok.is(tok::annot_template_id)) {
176125a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
17621734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      if (TemplateId->Kind == TNK_Function_template) {
17631734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        Diag(Tok, diag::err_typename_refers_to_non_type_template)
17641734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor          << Tok.getAnnotationRange();
17659ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
17661734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      }
1767d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
17685354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer      ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1769a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                         TemplateId->NumArgs);
177066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara
1771a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
177266581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                                     TemplateId->TemplateKWLoc,
1773a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->Template,
1774a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->TemplateNameLoc,
1775a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->LAngleLoc,
177666581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                                     TemplateArgsPtr,
1777a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->RAngleLoc);
17781734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else {
17791734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Diag(Tok, diag::err_expected_type_name_after_typename)
17801734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        << SS.getRange();
17819ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
17821734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
17831734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
178439d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    SourceLocation EndLoc = Tok.getLastLoc();
17851734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setKind(tok::annot_typename);
1786b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
178739d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    Tok.setAnnotationEndLoc(EndLoc);
17881734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setLocation(TypenameLoc);
17891734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    PP.AnnotateCachedTokens(Tok);
17909ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1791d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1792d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1793ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // Remembers whether the token was originally a scope annotation.
17940576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1795ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall
1796eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
17974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
1798b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
17999ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1800eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
18010576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  return TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, NeedType,
18020576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                   SS, !WasScopeAnnotation);
18030576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
18040576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
18050576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \brief Try to annotate a type or scope token, having already parsed an
18060576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// optional scope specifier. \p IsNewScope should be \c true unless the scope
18070576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// specifier was extracted from an existing tok::annot_cxxscope annotation.
18080576681bac125be07f77f66b02a3dba2c3a24557Richard Smithbool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(bool EnteringContext,
18090576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       bool NeedType,
18100576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       CXXScopeSpec &SS,
18110576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       bool IsNewScope) {
1812eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::identifier)) {
1813fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain    IdentifierInfo *CorrectedII = 0;
1814608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner    // Determine whether the identifier is a type name.
1815b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
1816b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            Tok.getLocation(), getCurScope(),
18171e52dfc648ce0b25ef57ae29ef1b4337d80011efFariborz Jahanian                                            &SS, false,
18189e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            NextToken().is(tok::period),
18199e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            ParsedType(),
1820fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*IsCtorOrDtorName=*/false,
1821fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain                                            /*NonTrivialTypeSourceInfo*/true,
1822fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain                                            NeedType ? &CorrectedII : NULL)) {
1823fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain      // A FixIt was applied as a result of typo correction
1824fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain      if (CorrectedII)
1825fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain        Tok.setIdentifierInfo(CorrectedII);
1826608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // This is a typename. Replace the current token in-place with an
1827608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // annotation type token.
1828b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner      Tok.setKind(tok::annot_typename);
1829b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      setTypeAnnotation(Tok, Ty);
1830608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      Tok.setAnnotationEndLoc(Tok.getLocation());
1831608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      if (SS.isNotEmpty()) // it was a C++ qualified type name.
1832608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner        Tok.setLocation(SS.getBeginLoc());
18331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1834608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // In case the tokens were cached, have Preprocessor replace
1835608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them with the annotation token.
1836608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      PP.AnnotateCachedTokens(Tok);
18379ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
18381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
183939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
18404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus) {
1841608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // If we're in C, we can't have :: tokens at all (the lexer won't return
1842608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them).  If the identifier is not a type, then it can't be scope either,
18431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // just early exit.
1844608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      return false;
1845eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    }
18461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
184739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // If this is a template-id, annotate with a template-id or type token.
184855f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    if (NextToken().is(tok::less)) {
18497532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor      TemplateTy Template;
1850014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
1851014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
18521fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
18531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateNameKind TNK
18547c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara          = Actions.isTemplateName(getCurScope(), SS,
18557c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   /*hasTemplateKeyword=*/false, TemplateName,
1856b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   /*ObjectType=*/ ParsedType(),
1857b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   EnteringContext,
18587c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   Template, MemberOfUnknownSpecialization)) {
1859ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
1860ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
1861e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1862e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName)) {
1863c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // If an unrecoverable error occurred, we need to return true here,
1864c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // because the token stream is in a damaged state.  We may not return
1865c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // a valid identifier.
18669ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
1867c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner        }
1868ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
186955f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    }
1870d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
187139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // The current token, which is either an identifier or a
187239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // template-id, is not part of the annotation. Fall through to
187339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // push that token back into the stream and complete the C++ scope
187439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // specifier annotation.
18751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1876eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
187739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  if (Tok.is(tok::annot_template_id)) {
187825a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1879c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor    if (TemplateId->Kind == TNK_Type_template) {
188039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // A template-id that refers to a type was parsed into a
188139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // template-id annotation in a context where we weren't allowed
188239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // to produce a type annotation token. Update the template-id
188339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // annotation token to a type annotation token now.
1884059101f922de6eb765601459925f4c8914420b23Douglas Gregor      AnnotateTemplateIdTokenAsType();
18859ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
188639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
188739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
1888d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
18896ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (SS.isEmpty())
18909ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
18911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18926ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // A C++ scope specifier that isn't followed by a typename.
18930576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  AnnotateScopeToken(SS, IsNewScope);
18949ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1895eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
1896eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
1897eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
189839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor/// annotates C++ scope specifiers and template-ids.  This returns
189983a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith/// true if there was an error that could not be recovered from.
19001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
190155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
190255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1903495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
19044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus &&
19056ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner         "Call sites of this function should be guarded by checking for C++");
19063b887354b1b667c97d070ddc67b5354353c4c07bDouglas Gregor  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
190742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie          (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
190842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie         Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
1909eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
19104bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  CXXScopeSpec SS;
1911b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
19129ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return true;
1913edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin  if (SS.isEmpty())
19149ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1915eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
19160576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  AnnotateScopeToken(SS, true);
19179ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1918eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
19196c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall
1920fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieubool Parser::isTokenEqualOrEqualTypo() {
1921fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  tok::TokenKind Kind = Tok.getKind();
1922fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  switch (Kind) {
1923fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  default:
1924d6c7c67313634b317a0d63c32be0511a121bb33dRichard Trieu    return false;
1925fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::ampequal:            // &=
1926fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::starequal:           // *=
1927fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::plusequal:           // +=
1928fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::minusequal:          // -=
1929fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::exclaimequal:        // !=
1930fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::slashequal:          // /=
1931fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::percentequal:        // %=
1932fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::lessequal:           // <=
1933fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::lesslessequal:       // <<=
1934fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::greaterequal:        // >=
1935fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::greatergreaterequal: // >>=
1936fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::caretequal:          // ^=
1937fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::pipeequal:           // |=
1938fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::equalequal:          // ==
1939fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu    Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
1940fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu      << getTokenSimpleSpelling(Kind)
1941fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu      << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
1942fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::equal:
1943fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu    return true;
1944fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  }
1945a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis}
1946a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
19477d100872341f233c81e1d7b72b40457e62c36862Argyrios KyrtzidisSourceLocation Parser::handleUnexpectedCodeCompletionToken() {
19487d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  assert(Tok.is(tok::code_completion));
19497d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  PrevTokLocation = Tok.getLocation();
19507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
195123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
1952dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::FnScope) {
1953f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
19547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
19557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return PrevTokLocation;
1956dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1957dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1958dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::ClassScope) {
1959f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
19607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
19617d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return PrevTokLocation;
1962dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1963dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  }
1964dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1965f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
19667d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  cutOffParsing();
19677d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  return PrevTokLocation;
1968dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor}
1969dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
19706c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// Anchor the Parser::FieldCallback vtable to this translation unit.
19716c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// We use a spurious method instead of the destructor because
19726c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// destroying FieldCallbacks can actually be slightly
19736c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// performance-sensitive.
19746c94a6d77f456f23ecd4c2061e6413786b5e6571John McCallvoid Parser::FieldCallback::_anchor() {
19756c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall}
1976f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1977f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor// Code-completion pass-through functions
1978f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1979f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteDirective(bool InConditional) {
1980f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorDirective(InConditional);
1981f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
1982f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1983f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteInConditionalExclusion() {
1984f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
1985f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
19861fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor
19871fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregorvoid Parser::CodeCompleteMacroName(bool IsDefinition) {
1988f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1989f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1990f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1991f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompletePreprocessorExpression() {
1992f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorExpression();
1993f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1994f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1995f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1996f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       MacroInfo *MacroInfo,
1997f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       unsigned ArgumentIndex) {
1998f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1999f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                                ArgumentIndex);
20001fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor}
200155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor
200255817afdf9d453a443262a733f6caf6692dca118Douglas Gregorvoid Parser::CodeCompleteNaturalLanguage() {
200355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  Actions.CodeCompleteNaturalLanguage();
200455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor}
2005f986038beed360c031de8654cfba43a5d3184605Francois Pichet
20063896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregorbool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
2007f986038beed360c031de8654cfba43a5d3184605Francois Pichet  assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
2008f986038beed360c031de8654cfba43a5d3184605Francois Pichet         "Expected '__if_exists' or '__if_not_exists'");
20093896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Result.IsIfExists = Tok.is(tok::kw___if_exists);
20103896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Result.KeywordLoc = ConsumeToken();
2011f986038beed360c031de8654cfba43a5d3184605Francois Pichet
20124a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
20134a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
20143896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Diag(Tok, diag::err_expected_lparen_after)
20153896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor      << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
2016f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
2017f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
2018f986038beed360c031de8654cfba43a5d3184605Francois Pichet
2019f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Parse nested-name-specifier.
2020efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(),
2021efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor                                 /*EnteringContext=*/false);
2022f986038beed360c031de8654cfba43a5d3184605Francois Pichet
2023f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check nested-name specifier.
20243896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (Result.SS.isInvalid()) {
20253896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    T.skipToEnd();
2026f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
2027f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
2028f986038beed360c031de8654cfba43a5d3184605Francois Pichet
2029e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  // Parse the unqualified-id.
2030e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
2031e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(),
2032e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         TemplateKWLoc, Result.Name)) {
20333896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    T.skipToEnd();
2034f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
2035f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
2036f986038beed360c031de8654cfba43a5d3184605Francois Pichet
20373896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (T.consumeClose())
2038f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
20393896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
2040f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check if the symbol exists.
204165019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
204265019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor                                               Result.IsIfExists, Result.SS,
20433896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor                                               Result.Name)) {
20443896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_Exists:
20453896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
20463896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
2047f986038beed360c031de8654cfba43a5d3184605Francois Pichet
20483896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_DoesNotExist:
20493896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
20503896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
20513896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
20523896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_Dependent:
20533896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = IEB_Dependent;
20543896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
205565019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor
205665019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  case Sema::IER_Error:
205765019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor    return true;
20583896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  }
2059f986038beed360c031de8654cfba43a5d3184605Francois Pichet
2060f986038beed360c031de8654cfba43a5d3184605Francois Pichet  return false;
2061f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
2062f986038beed360c031de8654cfba43a5d3184605Francois Pichet
2063563a645de82231a55e221fe655b7188bf8369662Francois Pichetvoid Parser::ParseMicrosoftIfExistsExternalDeclaration() {
20643896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  IfExistsCondition Result;
2065f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (ParseMicrosoftIfExistsCondition(Result))
2066f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
2067f986038beed360c031de8654cfba43a5d3184605Francois Pichet
20683896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  BalancedDelimiterTracker Braces(*this, tok::l_brace);
20693896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (Braces.consumeOpen()) {
2070f986038beed360c031de8654cfba43a5d3184605Francois Pichet    Diag(Tok, diag::err_expected_lbrace);
2071f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
2072f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
2073f986038beed360c031de8654cfba43a5d3184605Francois Pichet
20743896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  switch (Result.Behavior) {
20753896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Parse:
20763896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    // Parse declarations below.
20773896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
20783896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
20793896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Dependent:
20803896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    llvm_unreachable("Cannot have a dependent external declaration");
20813896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
20823896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Skip:
20833896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Braces.skipToEnd();
2084f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
2085f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
2086f986038beed360c031de8654cfba43a5d3184605Francois Pichet
20873896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  // Parse the declarations.
20883896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2089f986038beed360c031de8654cfba43a5d3184605Francois Pichet    ParsedAttributesWithRange attrs(AttrFactory);
2090f986038beed360c031de8654cfba43a5d3184605Francois Pichet    MaybeParseCXX0XAttributes(attrs);
2091f986038beed360c031de8654cfba43a5d3184605Francois Pichet    MaybeParseMicrosoftAttributes(attrs);
2092f986038beed360c031de8654cfba43a5d3184605Francois Pichet    DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
2093f986038beed360c031de8654cfba43a5d3184605Francois Pichet    if (Result && !getCurScope()->getParent())
2094f986038beed360c031de8654cfba43a5d3184605Francois Pichet      Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
20953896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  }
20963896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Braces.consumeClose();
2097f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
20986aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
20995948ae1021122164b22f74353bb7fe325a64f616Douglas GregorParser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) {
210032ad2ee2618745ce3da51c2ae066ed5f21157c07Ted Kremenek  assert(Tok.isObjCAtKeyword(tok::objc___experimental_modules_import) &&
210165030af6526748ce11534e92f0ccefc44091ba13Douglas Gregor         "Improper start to module import");
21026aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  SourceLocation ImportLoc = ConsumeToken();
21036aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
21043d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
21053d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
21063d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  // Parse the module path.
21073d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  do {
21083d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    if (!Tok.is(tok::identifier)) {
2109c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor      if (Tok.is(tok::code_completion)) {
2110c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        Actions.CodeCompleteModuleImport(ImportLoc, Path);
2111c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        ConsumeCodeCompletionToken();
2112c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        SkipUntil(tok::semi);
2113c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        return DeclGroupPtrTy();
2114c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor      }
2115c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor
21163d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      Diag(Tok, diag::err_module_expected_ident);
21173d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      SkipUntil(tok::semi);
21183d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      return DeclGroupPtrTy();
21193d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    }
21203d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
21213d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    // Record this part of the module path.
21223d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
21233d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    ConsumeToken();
21243d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
21253d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    if (Tok.is(tok::period)) {
21263d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      ConsumeToken();
21273d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      continue;
21283d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    }
21293d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
21303d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    break;
21313d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  } while (true);
21326aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
21335948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor  DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path);
21346aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  ExpectAndConsumeSemi(diag::err_module_expected_semi);
21356aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  if (Import.isInvalid())
21366aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    return DeclGroupPtrTy();
21376aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
21386aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  return Actions.ConvertDeclToDeclGroup(Import.get());
21396aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor}
21404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2141c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::diagnoseOverflow() {
2142d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
2143d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.SkipUntil(tok::eof);
2144d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  return true;
21454a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
21464a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2147c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
21484a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            const char *Msg,
21494a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            tok::TokenKind SkipToToc ) {
21504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  LOpen = P.Tok.getLocation();
2151d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc))
2152d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor    return true;
2153d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
2154d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  if (getDepth() < MaxDepth)
2155d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor    return false;
2156d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
2157d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  return diagnoseOverflow();
21584a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
21594a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2160c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::diagnoseMissingClose() {
2161d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
2162d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
2163d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  const char *LHSName = "unknown";
2164b031eab1c07fa2c5bd74c7e92f7c938bf3304729David Blaikie  diag::kind DID;
2165d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  switch (Close) {
2166b031eab1c07fa2c5bd74c7e92f7c938bf3304729David Blaikie  default: llvm_unreachable("Unexpected balanced token");
2167d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
2168d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
2169d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
21704a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  }
2171d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(P.Tok, DID);
2172d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(LOpen, diag::note_matching) << LHSName;
2173d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  if (P.SkipUntil(Close))
2174d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor    LClose = P.Tok.getLocation();
21754a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return true;
21764a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
21773896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
2178c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorvoid BalancedDelimiterTracker::skipToEnd() {
21793896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  P.SkipUntil(Close, false);
21803896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor}
2181