Parser.cpp revision e4246a633b13197634225971b25df0cbdcec0c5d
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
26f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallParser::Parser(Preprocessor &pp, Sema &actions)
27614f96a7cf94805c2d336639300b62dc2f54e9e0Ted Kremenek  : PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
280fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor    GreaterThanIsOperator(true), ColonIsSacred(false),
290fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor    InMessageExpression(false), TemplateParameterDepth(0) {
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Tok.setKind(tok::eof);
3123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = 0;
329e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  NumCachedScopes = 0;
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ParenCount = BracketCount = BraceCount = 0;
34d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ObjCImpDecl = 0;
35fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar
36fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // Add #pragma handlers. These are removed and destroyed in the
37fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // destructor.
38cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  AlignHandler.reset(new PragmaAlignHandler(actions));
39cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  PP.AddPragmaHandler(AlignHandler.get());
40cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar
41aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler(actions));
42aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
43aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
449b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  OptionsHandler.reset(new PragmaOptionsHandler(actions));
459b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(OptionsHandler.get());
469b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis
479b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PackHandler.reset(new PragmaPackHandler(actions));
489b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(PackHandler.get());
4962c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian
5062c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructHandler.reset(new PragmaMSStructHandler(actions));
5162c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  PP.AddPragmaHandler(MSStructHandler.get());
529b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis
539b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  UnusedHandler.reset(new PragmaUnusedHandler(actions, *this));
549b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(UnusedHandler.get());
559b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis
569b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  WeakHandler.reset(new PragmaWeakHandler(actions));
579b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(WeakHandler.get());
58321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne
59321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  FPContractHandler.reset(new PragmaFPContractHandler(actions, *this));
60321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  PP.AddPragmaHandler("STDC", FPContractHandler.get());
61f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
62f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  if (getLang().OpenCL) {
63f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    OpenCLExtensionHandler.reset(
64f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne                  new PragmaOpenCLExtensionHandler(actions, *this));
65f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
66f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
67f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
68f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
69f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
70f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  PP.setCodeCompletionHandler(*this);
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
730102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner/// If a crash happens while the parser is active, print out a line indicating
740102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner/// what the current token is.
750102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattnervoid PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const {
760102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  const Token &Tok = P.getCurToken();
77ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner  if (Tok.is(tok::eof)) {
780102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner    OS << "<eof> parser at end of file\n";
790102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner    return;
800102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  }
811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner  if (Tok.getLocation().isInvalid()) {
83ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner    OS << "<unknown> parser at unknown location\n";
84ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner    return;
85ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner  }
861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
870102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  const Preprocessor &PP = P.getPreprocessor();
880102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  Tok.getLocation().print(OS, PP.getSourceManager());
899fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar  if (Tok.isAnnotation())
909fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar    OS << ": at annotation token \n";
919fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar  else
929fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar    OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
93f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor}
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
950102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner
963cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
9733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return Diags.Report(Loc, DiagID);
981ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner}
991ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner
1003cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
1011ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  return Diag(Tok.getLocation(), DiagID);
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1044b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \brief Emits a diagnostic suggesting parentheses surrounding a
1054b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// given range.
1064b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor///
1074b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param Loc The location where we'll emit the diagnostic.
1084b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param Loc The kind of diagnostic to emit.
1094b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param ParenRange Source range enclosing code that should be parenthesized.
1104b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorvoid Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
1114b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor                                SourceRange ParenRange) {
112b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
113b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
1144b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // We can't display the parentheses, so just dig the
1154b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // warning/error and return.
1164b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Loc, DK);
1174b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    return;
1184b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
1191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Diag(Loc, DK)
121849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
122849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(EndLoc, ")");
1234b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor}
1244b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// this helper function matches and consumes the specified RHS token if
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// present.  If not present, it emits the specified diagnostic indicating
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// that the parser failed to match the RHS of the token at LHSLoc.  LHSName
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// should be the name of the unmatched LHS token.
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                           SourceLocation LHSLoc) {
132a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
133000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(RHSTok))
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return ConsumeAnyToken();
135a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation R = Tok.getLocation();
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *LHSName = "unknown";
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  diag::kind DID = diag::err_parse_error;
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (RHSTok) {
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: break;
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::greater:  LHSName = "<"; DID = diag::err_expected_greater; break;
145bf36e25224b959595af84337339103ebc542ff8cPeter Collingbourne  case tok::greatergreatergreater:
146bf36e25224b959595af84337339103ebc542ff8cPeter Collingbourne                      LHSName = "<<<"; DID = diag::err_expected_ggg; break;
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Diag(Tok, DID);
14928eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner  Diag(LHSLoc, diag::note_matching) << LHSName;
1509fc1873a3da0af8ef91e4f1927f3540f9af2038fChris Lattner  SkipUntil(RHSTok);
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return R;
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
154837b1a37116cf4e64f8bb7db34982dee1fba7647John McCallstatic bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
155837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  switch (ExpectedTok) {
156837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  case tok::semi: return Tok.is(tok::colon); // : for ;
157837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  default: return false;
158837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  }
159837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall}
160837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// input.  If so, it is consumed and false is returned.
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If the input is malformed, this emits the specified diagnostic.  Next, if
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipToTok is specified, it calls SkipUntil(SkipToTok).  Finally, true is
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// returned.
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const char *Msg, tok::TokenKind SkipToTok) {
169dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeAnyToken();
1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
173a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
174837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  // Detect common single-character typos and resume.
175837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  if (IsCommonTypo(ExpectedTok, Tok)) {
176837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    SourceLocation Loc = Tok.getLocation();
177837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    Diag(Loc, DiagID)
178837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall      << Msg
179837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall      << FixItHint::CreateReplacement(SourceRange(Loc),
180837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall                                      getTokenSimpleSpelling(ExpectedTok));
181837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    ConsumeAnyToken();
182837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
183837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    // Pretend there wasn't a problem.
184837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    return false;
185837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  }
186837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
1874b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  const char *Spelling = 0;
188b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
1891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (EndLoc.isValid() &&
190b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor      (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
1914b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // Show what code to insert to fix this problem.
1921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(EndLoc, DiagID)
1934b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor      << Msg
194849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateInsertion(EndLoc, Spelling);
1954b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  } else
1964b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Tok, DiagID) << Msg;
1974b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (SkipToTok != tok::unknown)
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(SkipToTok);
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2039ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregorbool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
2049ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
2059ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeAnyToken();
2069ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    return false;
2079ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  }
2089ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
2099ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
2109ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      NextToken().is(tok::semi)) {
2119ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    Diag(Tok, diag::err_extraneous_token_before_semi)
2129ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      << PP.getSpelling(Tok)
2139ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      << FixItHint::CreateRemoval(Tok.getLocation());
2149ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeAnyToken(); // The ')' or ']'.
2159ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeToken(); // The ';'.
2169ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    return false;
2179ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  }
2189ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
2199ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  return ExpectAndConsume(tok::semi, DiagID);
2209ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor}
2219ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Error recovery.
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipUntil - Read tokens until we get to the specified token, then consume
227012cf464254804279efa84e21b4b493dde76c5f1Chris Lattner/// it (unless DontConsume is true).  Because we cannot guarantee that the
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// token will ever occur, this skips to the next token, or to some likely
2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// good stopping point.  If StopAtSemi is true, skipping will stop at a ';'
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character.
231a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If SkipUntil finds the specified token, it returns true, otherwise it
233a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// returns false.
2345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
2353437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis                       bool StopAtSemi, bool DontConsume,
2363437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis                       bool StopAtCodeCompletion) {
2375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We always want this function to skip at least one token if the first token
2385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // isn't T and if not at EOF.
2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isFirstTokenSkipped = true;
2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we found one of the tokens, stop and return true.
2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumToks; ++i) {
243000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(Toks[i])) {
2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (DontConsume) {
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Noop, don't consume the token.
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        } else {
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ConsumeAnyToken();
2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return true;
2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
252a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Tok.getKind()) {
2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::eof:
2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ran out of tokens.
2565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
257dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
258dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    case tok::code_completion:
2593437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      if (!StopAtCodeCompletion)
2603437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis        ConsumeToken();
261dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      return false;
262dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_paren:
2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested parens.
2655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
2663437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion);
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_square:
2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested square brackets.
2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
2713437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_square, false, false, StopAtCodeCompletion);
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_brace:
2745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested braces.
2755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
2763437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion);
2775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
278a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Since the user wasn't looking for this token (if they were, it would
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // already be handled), this isn't balanced.  If there is a LHS token at a
2825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // higher level, we will assume that this matches the unbalanced token
2835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // and return it.  Otherwise, this is a spurious RHS token, which we skip.
2845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_paren:
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ParenCount && !isFirstTokenSkipped)
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
2885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_square:
2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BracketCount && !isFirstTokenSkipped)
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
2935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_brace:
2955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BraceCount && !isFirstTokenSkipped)
2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
299a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::string_literal:
3015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::wide_string_literal:
3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeStringToken();
3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
30455edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian
30555edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian    case tok::at:
30655edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian      return false;
30755edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::semi:
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (StopAtSemi)
3105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip this token.
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
3165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    isFirstTokenSkipped = false;
318a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump  }
3195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Scope manipulation
3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EnterScope - Start a new scope.
3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::EnterScope(unsigned ScopeFlags) {
3279e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes) {
3289e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    Scope *N = ScopeCache[--NumCachedScopes];
32923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    N->Init(getCurScope(), ScopeFlags);
33023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CurScope = N;
3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
3329c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis    Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExitScope - Pop a scope off the scope stack.
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ExitScope() {
33823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() && "Scope imbalance!");
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
34090ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // Inform the actions module that this scope is going away if there are any
34190ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // decls in it.
34223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!getCurScope()->decl_empty())
34323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
344a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
34523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Scope *OldScope = getCurScope();
34623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = OldScope->getParent();
347a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
3489e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes == ScopeCacheSize)
3499e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete OldScope;
3505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
3519e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    ScopeCache[NumCachedScopes++] = OldScope;
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// C99 6.9: External Definitions.
3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerParser::~Parser() {
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we still have scopes active, delete the scope tree.
36323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  delete getCurScope();
36423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = 0;
36523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Free the scope cache.
3679e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
3689e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete ScopeCache[i];
369fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar
3708387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Free LateParsedTemplatedFunction nodes.
3718387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin();
3728387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      it != LateParsedTemplateMap.end(); ++it)
3738387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    delete it->second;
3748387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
375fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // Remove the pragma handlers we installed.
376cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  PP.RemovePragmaHandler(AlignHandler.get());
377cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  AlignHandler.reset();
378aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
379aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  GCCVisibilityHandler.reset();
3809b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(OptionsHandler.get());
381861800c676004eabed5927f0552620d06c80a40aDaniel Dunbar  OptionsHandler.reset();
3829b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(PackHandler.get());
3834726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PackHandler.reset();
38462c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  PP.RemovePragmaHandler(MSStructHandler.get());
38562c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructHandler.reset();
3869b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(UnusedHandler.get());
3874726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  UnusedHandler.reset();
3889b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(WeakHandler.get());
3899991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman  WeakHandler.reset();
390f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
391f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  if (getLang().OpenCL) {
392f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
393f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    OpenCLExtensionHandler.reset();
394f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
395f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
396f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
397321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  PP.RemovePragmaHandler("STDC", FPContractHandler.get());
398321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  FPContractHandler.reset();
399f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  PP.clearCodeCompletionHandler();
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Initialize - Warm up the parser.
4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::Initialize() {
40531e057270232c1c37602579cb6461c2704175672Chris Lattner  // Create the translation unit scope.  Install it as the current scope.
40623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() == 0 && "A scope is already active?");
40731e057270232c1c37602579cb6461c2704175672Chris Lattner  EnterScope(Scope::DeclScope);
408c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  Actions.ActOnTranslationUnitScope(getCurScope());
409c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor
410c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  // Prime the lexer look-ahead.
411c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  ConsumeToken();
412a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
413000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(tok::eof) &&
414f72617544287acf0281c3b1a733bcb22a02e6ca4Chris Lattner      !getLang().CPlusPlus)  // Empty source file is an extension in C
4155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::ext_empty_source_file);
416a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
41734870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  // Initialization for Objective-C context sensitive keywords recognition.
418a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  // Referenced in Parser::ParseObjCTypeQualifierList.
41934870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  if (getLang().ObjC1) {
420a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
421a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
422a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
423a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
424a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
425a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
42634870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  }
427662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar
4287eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson  Ident_final = 0;
4297eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson  Ident_override = 0;
4301f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson
431662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar  Ident_super = &PP.getIdentifierTable().get("super");
43282287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson
43382287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  if (getLang().AltiVec) {
43482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_vector = &PP.getIdentifierTable().get("vector");
43582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_pixel = &PP.getIdentifierTable().get("pixel");
43682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  }
4370a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4380a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_introduced = 0;
4390a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_deprecated = 0;
4400a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_obsoleted = 0;
441b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  Ident_unavailable = 0;
44228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
44328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0;
44428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0;
44528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0;
44628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
44728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  if(getLang().Borland) {
44828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__exception_info        = PP.getIdentifierInfo("_exception_info");
44928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___exception_info       = PP.getIdentifierInfo("__exception_info");
45028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_GetExceptionInfo       = PP.getIdentifierInfo("GetExceptionInformation");
45128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__exception_code        = PP.getIdentifierInfo("_exception_code");
45228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___exception_code       = PP.getIdentifierInfo("__exception_code");
45328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_GetExceptionCode       = PP.getIdentifierInfo("GetExceptionCode");
45428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__abnormal_termination  = PP.getIdentifierInfo("_abnormal_termination");
45528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
45628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_AbnormalTermination    = PP.getIdentifierInfo("AbnormalTermination");
45728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
45828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
45928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
46028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
46128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
46228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
46328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
46428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
46528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
46628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
46728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
4685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
4715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// action tells us to.  This returns true if the EOF was encountered.
472682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattnerbool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
473b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
474b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  while (Tok.is(tok::annot_pragma_unused))
475b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis    HandlePragmaUnused();
476b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
477682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  Result = DeclGroupPtrTy();
4789299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  if (Tok.is(tok::eof)) {
4798387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    // Late template parsing can begin.
4808387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (getLang().DelayedTemplateParsing)
4818387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
4828387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
4839299f3fa85796613cc787a2062c9562d07c8613eChris Lattner    Actions.ActOnEndOfTranslationUnit();
4849299f3fa85796613cc787a2062c9562d07c8613eChris Lattner    return true;
4859299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  }
486a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
4870b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributesWithRange attrs(AttrFactory);
4887f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseCXX0XAttributes(attrs);
4897f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseMicrosoftAttributes(attrs);
490334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet
4917f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  Result = ParseExternalDeclaration(attrs);
4925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTranslationUnit:
4965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       translation-unit: [C99 6.9]
497a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///         external-declaration
498a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///         translation-unit external-declaration
4995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseTranslationUnit() {
5008935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  Initialize();
501a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
502682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  DeclGroupPtrTy Res;
50389307ffaf8acf4d6fdffd72b607ca4fbcfdffc9dSteve Naroff  while (!ParseTopLevelDecl(Res))
5045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /*parse them all*/;
5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50606f548596beef4c0a227a45cba996497f99566c0Chris Lattner  ExitScope();
50723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() == 0 && "Scope imbalance!");
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseExternalDeclaration:
51190b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner///
512c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
513c3018153a11afe91849748a93d920040a571b76cChris Lattner///         function-definition
514c3018153a11afe91849748a93d920040a571b76cChris Lattner///         declaration
515a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU]   asm-definition
517c3018153a11afe91849748a93d920040a571b76cChris Lattner/// [GNU]   __extension__ external-declaration
5185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
5195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-declaration
5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-alias-declaration
5215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-protocol-definition
5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-method-definition
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  @end
524c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor/// [C++]   linkage-specification
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-definition:
5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         simple-asm-expr ';'
5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
528a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration:
529a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///           ';'
530a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///
53145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor/// [C++0x/GNU] 'extern' 'template' declaration
5327f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::DeclGroupPtrTy
5337f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
5347f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                 ParsingDeclSpec *DS) {
53536d36806f1972f7ec1d2a3f59155187278c56508Argyrios Kyrtzidis  ParenBraceBracketBalancer BalancerRAIIObj(*this);
53636d36806f1972f7ec1d2a3f59155187278c56508Argyrios Kyrtzidis
537d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *SingleDecl = 0;
5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Tok.getKind()) {
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::semi:
540a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor    if (!getLang().CPlusPlus0x)
541a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor      Diag(Tok, diag::ext_top_level_semi)
542849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
5431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TODO: Invoke action for top-level semicolon.
546682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
54790b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::r_brace:
54890b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    Diag(Tok, diag::err_expected_external_declaration);
54990b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    ConsumeBrace();
550682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
55190b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::eof:
55290b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    Diag(Tok, diag::err_expected_external_declaration);
553682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
554c3018153a11afe91849748a93d920040a571b76cChris Lattner  case tok::kw___extension__: {
555c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    // __extension__ silences extension warnings in the subexpression.
556c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
55739146d6497ad5e7ca8ef639221e7b3e15d07c888Chris Lattner    ConsumeToken();
5587f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    return ParseExternalDeclaration(attrs);
559c3018153a11afe91849748a93d920040a571b76cChris Lattner  }
560dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  case tok::kw_asm: {
5617f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ProhibitAttributes(attrs);
562bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
56321e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation StartLoc = Tok.getLocation();
56421e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation EndLoc;
56521e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    ExprResult Result(ParseSimpleAsm(&EndLoc));
566a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
5673f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson    ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
5683f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson                     "top-level asm block");
569dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson
570682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    if (Result.isInvalid())
571682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
57221e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
573682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
574dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  }
5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::at:
576682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    // @ is not a legal token unless objc is enabled, no need to check for ObjC.
577682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    /// FIXME: ParseObjCAtDirectives should return a DeclGroup for things like
578682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    /// @class foo, bar;
579682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = ParseObjCAtDirectives();
580682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::minus:
5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::plus:
583682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    if (!getLang().ObjC1) {
584682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      Diag(Tok, diag::err_expected_external_declaration);
585682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      ConsumeToken();
586682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
587682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    }
588682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = ParseObjCMethodDefinition();
589682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
590791215b7a24666912c0b71175d2ca5ba082f666eDouglas Gregor  case tok::code_completion:
59123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
592f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                   ObjCImpDecl? Sema::PCC_ObjCImplementation
593f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                              : Sema::PCC_Namespace);
594dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
5957f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    return ParseExternalDeclaration(attrs);
596f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  case tok::kw_using:
5978f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  case tok::kw_namespace:
5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_typedef:
599adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_template:
600adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_export:    // As in 'export template'
601511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  case tok::kw_static_assert:
602c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne  case tok::kw__Static_assert:
603bae35118fc5cea2da08567dbb9763af7f906dae2Chris Lattner    // A function definition cannot start with a these keywords.
60497144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    {
60597144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner      SourceLocation DeclEnd;
606c5be7b0fc804d8e6f87298ec03c94d8cccd74f29Fariborz Jahanian      StmtVector Stmts(Actions);
6077f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
60897144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    }
609d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
6107306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_static:
6117306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // Parse (then ignore) 'static' prior to a template instantiation. This is
6127306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // a GCC extension that we intentionally do not support.
6137306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
6147306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
6157306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        << 0;
616d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl      SourceLocation DeclEnd;
617c5be7b0fc804d8e6f87298ec03c94d8cccd74f29Fariborz Jahanian      StmtVector Stmts(Actions);
6187f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
6197306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    }
6207306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    goto dont_know;
6217306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
6227306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_inline:
6237306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    if (getLang().CPlusPlus) {
6247306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      tok::TokenKind NextKind = NextToken().getKind();
6257306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
6267306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Inline namespaces. Allowed as an extension even in C++03.
6277306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_namespace) {
6287306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
6297306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        StmtVector Stmts(Actions);
6307f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
6317306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
6327306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
6337306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Parse (then ignore) 'inline' prior to a template instantiation. This is
6347306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // a GCC extension that we intentionally do not support.
6357306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_template) {
6367306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
6377306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor          << 1;
6387306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
6397306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        StmtVector Stmts(Actions);
6407f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
6417306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
642d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    }
643d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
644d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
64545f965581935791a018df829a14dff53c1dd8f47Douglas Gregor  case tok::kw_extern:
64645f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
64745f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      // Extern templates
64845f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation ExternLoc = ConsumeToken();
64945f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation TemplateLoc = ConsumeToken();
65045f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation DeclEnd;
65145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      return Actions.ConvertDeclToDeclGroup(
65245f965581935791a018df829a14dff53c1dd8f47Douglas Gregor                  ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd));
65345f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    }
65445f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    // FIXME: Detect C++ linkage specifications here?
655d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
657f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_exists:
658f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_not_exists:
659f986038beed360c031de8654cfba43a5d3184605Francois Pichet    ParseMicrosoftIfExistsDeclaration();
660f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return DeclGroupPtrTy();
661f986038beed360c031de8654cfba43a5d3184605Francois Pichet
6625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
663d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl  dont_know:
6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // We can't tell whether this is a function-definition or declaration yet.
6657f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (DS) {
6667f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      DS->takeAttributesFrom(attrs);
6677f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclarationOrFunctionDefinition(*DS);
6687f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    } else {
6697f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclarationOrFunctionDefinition(attrs);
6707f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    }
6715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
673682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // This routine returns a DeclGroup, if the thing we parsed only contains a
674682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // single decl, convert it now.
675682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  return Actions.ConvertDeclToDeclGroup(SingleDecl);
6765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6781426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
6791426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, continues a declaration or declaration list.
680e4246a633b13197634225971b25df0cbdcec0c5dSean Huntbool Parser::isDeclarationAfterDeclarator() {
681e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  // Check for '= delete' or '= default'
682e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  if (getLang().CPlusPlus && Tok.is(tok::equal)) {
683e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
684e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
685e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      return false;
686e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
687e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
6881426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor  return Tok.is(tok::equal) ||      // int X()=  -> not a function def
6891426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::comma) ||           // int X(),  -> not a function def
6901426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::semi)  ||           // int X();  -> not a function def
6911426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
6921426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
6931426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    (getLang().CPlusPlus &&
6941426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor     Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
6951426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
6961426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
6971426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
6981426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, indicates the start of a function definition.
699004659a56916f2f81ede507c12516c146d6c0df3Chris Lattnerbool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
700075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
7015d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  if (Tok.is(tok::l_brace))   // int X() {}
7025d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner    return true;
7035d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner
704004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  // Handle K&R C argument lists: int X(f) int f; {}
705004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  if (!getLang().CPlusPlus &&
706075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      Declarator.getFunctionTypeInfo().isKNRPrototype())
707004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner    return isDeclarationSpecifier();
708e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
709e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  if (getLang().CPlusPlus && Tok.is(tok::equal)) {
710e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
711e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
712e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
713004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner
7145d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
7155d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner         Tok.is(tok::kw_try);          // X() try { ... }
7161426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
7171426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
7185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// a declaration.  We can't tell which we have until we read up to the
720c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// compound-statement in function-definition. TemplateParams, if
721c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// non-NULL, provides the template parameters when we're parsing a
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ template-declaration.
7235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
7245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       function-definition: [C99 6.9.1]
725a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
726a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
727a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
728a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       declaration: [C99 6.7]
730697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner///         declaration-specifiers init-declarator-list[opt] ';'
731697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner/// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OMP]   threadprivate-directive                              [TODO]
7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
734682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerParser::DeclGroupPtrTy
7353acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
736bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt                                             AccessSpecifier AS) {
7375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Parse the common declaration-specifiers piece.
7380efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
739a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
7405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
7415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // declaration-specifiers init-declarator-list[opt] ';'
742000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(tok::semi)) {
7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
744d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
74554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.complete(TheDecl);
746682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
748a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
749246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // ObjC2 allows prefix attributes on class interfaces and protocols.
750246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // FIXME: This still needs better diagnostics. We should only accept
751246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // attributes here, no types, etc.
752000732226610650837478cba97843d19b75f648eChris Lattner  if (getLang().ObjC2 && Tok.is(tok::at)) {
753dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation AtLoc = ConsumeToken(); // the "@"
7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
755246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar        !Tok.isObjCAtKeyword(tok::objc_protocol)) {
756246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar      Diag(Tok, diag::err_objc_unexpected_attr);
757cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      SkipUntil(tok::semi); // FIXME: better skip?
758682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
759cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    }
760d8ac05753dc4506224d445ff98399c01da3136e5John McCall
76154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.abort();
76254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
7630de2ae28c603322f05e2d9200c7d457c8b928983Fariborz Jahanian    const char *PrevSpec = 0;
764fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
765fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
766fec54013fcd0eb72642741584ca04c1bc292bef8John McCall      Diag(AtLoc, DiagID) << PrevSpec;
7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
768d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = 0;
769246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar    if (Tok.isObjCAtKeyword(tok::objc_protocol))
770682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
771682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    else
772682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
773682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
774dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
775a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
776c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // If the declspec consisted only of 'extern' and we have a string
777c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // literal following it, this must be a C++ linkage specifier like
778c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // 'extern "C"'.
7793c6f6a7a1bb9969112617a26d2333bab2f6efd65Chris Lattner  if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
780c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner      DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
781682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
782d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
783682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
784682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  }
785c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
786d8ac05753dc4506224d445ff98399c01da3136e5John McCall  return ParseDeclGroup(DS, Declarator::FileContext, true);
7875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7893acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::DeclGroupPtrTy
7907f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::ParseDeclarationOrFunctionDefinition(ParsedAttributes &attrs,
7913acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian                                             AccessSpecifier AS) {
7923acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian  ParsingDeclSpec DS(*this);
7937f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  DS.takeAttributesFrom(attrs);
7947f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  return ParseDeclarationOrFunctionDefinition(DS, AS);
7953acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian}
7963acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian
7975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseFunctionDefinition - We parsed and verified that the specified
7985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Declarator is well formed.  If this is a K&R-style function, read the
7995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// parameters declaration-list, then start the compound-statement.
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
801a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///       function-definition: [C99 6.9.1]
802a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
803a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
804a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
8057ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
80623c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
80723c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         function-body
8087ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
809d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl///         decl-specifier-seq[opt] declarator function-try-block
8105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
811d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
812075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara                                      const ParsedTemplateInfo &TemplateInfo) {
81328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // Poison the SEH identifiers so they are flagged as illegal in function bodies
81428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
815075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
816a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
817a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // If this is C90 and the declspecs were completely missing, fudge in an
818a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // implicit int.  We do this here because this is the only place where
819a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // declaration-specifiers are completely optional in the grammar.
8202a327d11a07dfbdf20910cebbae38910eda111fdChris Lattner  if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) {
821a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner    const char *PrevSpec;
822fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
82331c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner    D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
82431c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner                                           D.getIdentifierLoc(),
825fec54013fcd0eb72642741584ca04c1bc292bef8John McCall                                           PrevSpec, DiagID);
826ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
827a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  }
828a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this declaration was formed with a K&R-style identifier list for the
8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // arguments, parse declarations for all of the args next.
8315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // int foo(a,b) int a; float b; {}
832004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  if (FTI.isKNRPrototype())
8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseKNRParamDeclarations(D);
8345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
835e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  if (Tok.is(tok::equal)) {
836e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    assert(getLang().CPlusPlus && "Only C++ function definitions have '='");
837e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    ConsumeToken();
838e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
839e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    Decl *Decl = 0;
840e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    // Here we complete the declaration as if it were normal
841e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    switch (TemplateInfo.Kind) {
842e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    case ParsedTemplateInfo::NonTemplate:
843e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Decl = Actions.ActOnDeclarator(getCurScope(), D, true);
844e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      break;
845e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
846e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    case ParsedTemplateInfo::Template:
847e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    case ParsedTemplateInfo::ExplicitSpecialization:
848e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Decl = Actions.ActOnTemplateDeclarator(getCurScope(),
849e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt                                   MultiTemplateParamsArg(Actions,
850e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt                                          TemplateInfo.TemplateParams->data(),
851e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt                                          TemplateInfo.TemplateParams->size()),
852e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt                                             D);
853e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      break;
854e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
855e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    case ParsedTemplateInfo::ExplicitInstantiation: {
856e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      DeclResult Result
857e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        = Actions.ActOnExplicitInstantiation(getCurScope(),
858e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt                                             TemplateInfo.ExternLoc,
859e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt                                             TemplateInfo.TemplateLoc,
860e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt                                             D);
861e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      if (Result.isInvalid()) {
862e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        SkipUntil(tok::semi);
863e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        return 0;
864e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      }
865e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
866e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Decl = Result.get();
867e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      break;
868e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    }
869e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    }
870e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
871e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    bool Delete = false;
872e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    SourceLocation KWLoc;
873e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (Tok.is(tok::kw_delete)) {
874e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      if (!getLang().CPlusPlus0x)
875e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
876e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
877e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      KWLoc = ConsumeToken();
878e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Actions.SetDeclDeleted(Decl, KWLoc);
879e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Delete = true;
880e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    } else if (Tok.is(tok::kw_default)) {
881e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      if (!getLang().CPlusPlus0x)
882e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        Diag(Tok, diag::warn_defaulted_function_accepted_as_extension);
883e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
884e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      KWLoc = ConsumeToken();
885e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Actions.SetDeclDefaulted(Decl, KWLoc);
886e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    } else {
887e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      llvm_unreachable("function definition after = not 'delete' or 'default'");
888e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    }
889e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
890e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (Tok.is(tok::comma)) {
891e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
892e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt        << Delete;
893e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      SkipUntil(tok::semi);
894e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    } else {
895e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
896e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt                       Delete ? "delete" : "default", tok::semi);
897e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    }
898e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
899e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    return Decl;
900e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
901e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
9027ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // We should have either an opening brace or, in a C++ constructor,
9037ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // we may have a colon.
904758afbcc86ef15f8d433f5f87db1495e50effeb3Douglas Gregor  if (Tok.isNot(tok::l_brace) &&
905758afbcc86ef15f8d433f5f87db1495e50effeb3Douglas Gregor      (!getLang().CPlusPlus ||
906758afbcc86ef15f8d433f5f87db1495e50effeb3Douglas Gregor       (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try)))) {
9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::err_expected_fn_body);
9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(tok::l_brace, true, true);
911a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
9125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we didn't find the '{', bail out.
913000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.isNot(tok::l_brace))
914d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
916a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
9178387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // In delayed template parsing mode, for function template we consume the
9188387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // tokens and store them for late parsing at the end of the translation unit.
9198387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  if (getLang().DelayedTemplateParsing &&
9208387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      TemplateInfo.Kind == ParsedTemplateInfo::Template) {
9218387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    MultiTemplateParamsArg TemplateParameterLists(Actions,
9228387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet                                         TemplateInfo.TemplateParams->data(),
9238387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet                                         TemplateInfo.TemplateParams->size());
9248387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9258387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
9268387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Scope *ParentScope = getCurScope()->getParent();
9278387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9288387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Decl *DP = Actions.HandleDeclarator(ParentScope, D,
9298387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet                                move(TemplateParameterLists),
9308387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet                                /*IsFunctionDefinition=*/true);
9318387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.complete(DP);
9328387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.getMutableDeclSpec().abort();
9338387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9348387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (DP) {
9358387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(this, DP);
9368387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9378387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      FunctionDecl *FnD = 0;
9388387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
9398387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = FunTmpl->getTemplatedDecl();
9408387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      else
9418387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = cast<FunctionDecl>(DP);
942d4a0caf78e7c18e7aca65fbfd799a6c024ff51fbFrancois Pichet      Actions.CheckForFunctionRedefinition(FnD);
9438387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9448387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateParsedTemplateMap[FnD] = LPT;
9458387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.MarkAsLateParsedTemplate(FnD);
9468387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(LPT->Toks);
9478387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    } else {
9488387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      CachedTokens Toks;
9498387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(Toks);
9508387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    }
9518387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return DP;
9528387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
9538387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9548387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
955b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Enter a scope for the function body.
9568935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
957a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
958b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Tell the actions module that we have entered a function definition with the
959b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // specified Declarator for the function.
960d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Res = TemplateInfo.TemplateParams?
96123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
962f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                              MultiTemplateParamsArg(Actions,
96352591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                                          TemplateInfo.TemplateParams->data(),
96452591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                                         TemplateInfo.TemplateParams->size()),
96552591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                                              D)
96623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
967a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
96854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclarator context before we parse the body.
96954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.complete(Res);
97054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
97154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclSpec context, too.  This const_cast is
97254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // safe because we're always the sole owner.
97354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.getMutableDeclSpec().abort();
97454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
975d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (Tok.is(tok::kw_try))
976c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor    return ParseFunctionTryBlock(Res, BodyScope);
977d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
9787ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // If we have a colon, then we're probably parsing a C++
9797ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // ctor-initializer.
980d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  if (Tok.is(tok::colon)) {
9817ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    ParseConstructorInitializer(Res);
982d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall
983d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    // Recover from error.
984d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    if (!Tok.is(tok::l_brace)) {
985c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor      BodyScope.Exit();
9869ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.ActOnFinishFunctionBody(Res, 0);
987d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall      return Res;
988d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    }
989d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  } else
990393612e6c7727f1fee50039254d9f434364cc0b2Fariborz Jahanian    Actions.ActOnDefaultCtorInitializers(Res);
9917ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
992c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor  return ParseFunctionStatementBody(Res, BodyScope);
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// types for a function with a K&R-style identifier list for arguments.
9975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseKNRParamDeclarations(Declarator &D) {
9985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We know that the top-level of this declarator is a function.
999075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
10005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
100104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // Enter function-declaration scope, limiting any declarators to the
100204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // function prototype scope, including parameter declarators.
10033218c4bb3b5d7250f12420de6db7ef3e3f805a75Douglas Gregor  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
100404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner
10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read all the argument declarations.
10065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isDeclarationSpecifier()) {
10075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SourceLocation DSStart = Tok.getLocation();
1008a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the common declaration-specifiers piece.
10100b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
10115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarationSpecifiers(DS);
1012a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: 'each declaration in the declaration list shall have at
10145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // least one declarator'.
10155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // the declarations though.  It's trivial to ignore them, really hard to do
10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // anything else with them.
1018000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.is(tok::semi)) {
10195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DSStart, diag::err_declaration_does_not_declare_param);
10205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1023a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // than register.
10265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
10275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        DS.getStorageClassSpec() != DeclSpec::SCS_register) {
10285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getStorageClassSpecLoc(),
10295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
10305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
10315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.isThreadSpecified()) {
10335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getThreadSpecLoc(),
10345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
10355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
10365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1037a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the first declarator attached to this declspec.
10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarator(ParmDeclarator);
10415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Handle the full declarator list.
10435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (1) {
10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If attributes are present, parse them.
10457f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      MaybeParseGNUAttributes(ParmDeclarator);
1046a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ask the actions module to compute the type for this declarator.
1048d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *Param =
104923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
10502bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff
1051a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump      if (Param &&
10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // A missing identifier has already been diagnosed.
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ParmDeclarator.getIdentifier()) {
10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Scan the argument list looking for the correct param to apply this
10565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // type.
10575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0; ; ++i) {
10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // C99 6.9.1p6: those declarators shall declare only identifiers from
10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // the identifier list.
10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (i == FTI.NumArgs) {
10611ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner            Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
10626898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner              << ParmDeclarator.getIdentifier();
10635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
10645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
1065a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Reject redefinitions of parameters.
106804421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner            if (FTI.ArgInfo[i].Param) {
10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(ParmDeclarator.getIdentifierLoc(),
10701ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner                   diag::err_param_redefinition)
10716898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner                 << ParmDeclarator.getIdentifier();
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            } else {
107304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner              FTI.ArgInfo[i].Param = Param;
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we don't have a comma, it is either the end of the list (a ';') or
10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // an error, bail out.
1082000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.isNot(tok::comma))
10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
1084a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Consume the comma.
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
1087a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Parse the next declarator.
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParmDeclarator.clear();
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParseDeclarator(ParmDeclarator);
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1092a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1093000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.is(tok::semi)) {
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_parse_error);
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip to end of block or statement
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi, true);
1099000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(tok::semi))
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ConsumeToken();
11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1103a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The actions module must verify that all arguments were declared.
110523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// allowed to be a wide string, and is not subject to character translation.
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-string-literal:
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         string-literal
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
111560d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseAsmStringLiteral() {
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!isTokenStringLiteral()) {
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::err_expected_string_literal);
111861364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl    return ExprError();
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1120a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
112160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseStringLiteralExpression());
1122effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  if (Res.isInvalid()) return move(Res);
1123a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // TODO: Diagnose: wide string literal in 'asm'
1125a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1126effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  return move(Res);
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseSimpleAsm
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] simple-asm-expr:
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'asm' '(' asm-string-literal ')'
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
113460d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
1135000732226610650837478cba97843d19b75f648eChris Lattner  assert(Tok.is(tok::kw_asm) && "Not an asm!");
1136dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  SourceLocation Loc = ConsumeToken();
1137a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11387a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  if (Tok.is(tok::kw_volatile)) {
1139841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    // Remove from the end of 'asm' to the end of 'volatile'.
1140841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1141841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall                             PP.getLocForEndOfToken(Tok.getLocation()));
1142841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall
1143841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    Diag(Tok, diag::warn_file_asm_volatile)
1144849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateRemoval(RemovalRange);
11457a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall    ConsumeToken();
11467a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  }
11477a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall
1148000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.isNot(tok::l_paren)) {
11491ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "asm";
115061364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl    return ExprError();
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1152a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1153ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  Loc = ConsumeParen();
1154a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
115560d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(ParseAsmStringLiteral());
1156a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1157ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Result.isInvalid()) {
1158ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SkipUntil(tok::r_paren, true, true);
1159ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1160ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Tok.getLocation();
1161ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    ConsumeAnyToken();
1162ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  } else {
1163ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    Loc = MatchRHSPunctuation(tok::r_paren, Loc);
1164ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1165ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Loc;
1166ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
1167a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1168effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  return move(Result);
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1171eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateTypeOrScopeToken - If the current token position is on a
1172eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typename (possibly qualified in C++) or a C++ scope specifier not followed
1173eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1174eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// with a single annotation token representing the typename or C++ scope
1175eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// respectively.
1176eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This simplifies handling of C++ scope specifiers and allows efficient
1177eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// backtracking without the need to re-parse and resolve nested-names and
1178eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typenames.
117944802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// It will mainly be called when we expect to treat identifiers as typenames
118044802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// (if they are typenames). For example, in C we do not expect identifiers
118144802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// inside expressions to be treated as typenames so it will not be called
118244802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// for expressions in C.
118344802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// The benefit for C/ObjC is that a typename will be annotated and
1184b43a50ff1b0b171ece84425b0ad83a9a31f038faSteve Naroff/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
118544802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// will not be called twice, once to check whether we have a declaration
118644802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// specifier, and another one to get the actual type inside
118744802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// ParseDeclarationSpecifiers).
1188a7bc7c880f86bc180684ef032d06df51bcae7a23Chris Lattner///
11899ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// This returns true if an error occurred.
11901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
119155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
119255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1193495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext) {
11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
1195ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall          || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) &&
11967452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Cannot be a type or scope token!");
11971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1198d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Tok.is(tok::kw_typename)) {
1199d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Parse a C++ typename-specifier, e.g., "typename T::type".
1200d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //
1201d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //   typename-specifier:
1202d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //     'typename' '::' [opt] nested-name-specifier identifier
12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     'typename' '::' [opt] nested-name-specifier template [opt]
12041734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    //            simple-template-id
1205d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    SourceLocation TypenameLoc = ConsumeToken();
1206d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    CXXScopeSpec SS;
12074147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(), false,
12084147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                       0, /*IsTypename*/true))
12099ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
12109ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    if (!SS.isSet()) {
1211a5d318ad4ad3a110b3abb34f765e69b76f7c0bcdFrancois Pichet      if (getLang().Microsoft)
1212a5d318ad4ad3a110b3abb34f765e69b76f7c0bcdFrancois Pichet        Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
1213a5d318ad4ad3a110b3abb34f765e69b76f7c0bcdFrancois Pichet      else
1214a5d318ad4ad3a110b3abb34f765e69b76f7c0bcdFrancois Pichet        Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
12159ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1216d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    }
1217d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1218d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    TypeResult Ty;
1219d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (Tok.is(tok::identifier)) {
1220d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      // FIXME: check whether the next token is '<', first!
122123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
12221a15dae8be2b28e02b6639aa92b832465c5be420Douglas Gregor                                     *Tok.getIdentifierInfo(),
1223d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     Tok.getLocation());
12241734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else if (Tok.is(tok::annot_template_id)) {
12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateIdAnnotation *TemplateId
12261734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
12271734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      if (TemplateId->Kind == TNK_Function_template) {
12281734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        Diag(Tok, diag::err_typename_refers_to_non_type_template)
12291734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor          << Tok.getAnnotationRange();
12309ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
12311734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      }
1232d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1233a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor      ASTTemplateArgsPtr TemplateArgsPtr(Actions,
1234a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                         TemplateId->getTemplateArgs(),
1235a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                         TemplateId->NumArgs);
1236a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor
1237a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1238a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     /*FIXME:*/SourceLocation(),
1239a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->Template,
1240a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->TemplateNameLoc,
1241a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->LAngleLoc,
1242a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateArgsPtr,
1243a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->RAngleLoc);
1244aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      TemplateId->Destroy();
12451734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else {
12461734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Diag(Tok, diag::err_expected_type_name_after_typename)
12471734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        << SS.getRange();
12489ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
12491734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
12501734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
125139d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    SourceLocation EndLoc = Tok.getLastLoc();
12521734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setKind(tok::annot_typename);
1253b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
125439d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    Tok.setAnnotationEndLoc(EndLoc);
12551734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setLocation(TypenameLoc);
12561734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    PP.AnnotateCachedTokens(Tok);
12579ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1258d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1259d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1260ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // Remembers whether the token was originally a scope annotation.
1261ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1262ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall
1263eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
12644bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  if (getLang().CPlusPlus)
1265b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
12669ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1267eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
1268eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::identifier)) {
1269608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner    // Determine whether the identifier is a type name.
1270b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
1271b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            Tok.getLocation(), getCurScope(),
12721e52dfc648ce0b25ef57ae29ef1b4337d80011efFariborz Jahanian                                            &SS, false,
12739e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            NextToken().is(tok::period),
12749e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            ParsedType(),
12759e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            /*NonTrivialTypeSourceInfo*/true)) {
1276608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // This is a typename. Replace the current token in-place with an
1277608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // annotation type token.
1278b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner      Tok.setKind(tok::annot_typename);
1279b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      setTypeAnnotation(Tok, Ty);
1280608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      Tok.setAnnotationEndLoc(Tok.getLocation());
1281608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      if (SS.isNotEmpty()) // it was a C++ qualified type name.
1282608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner        Tok.setLocation(SS.getBeginLoc());
12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1284608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // In case the tokens were cached, have Preprocessor replace
1285608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them with the annotation token.
1286608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      PP.AnnotateCachedTokens(Tok);
12879ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
128939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
129039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (!getLang().CPlusPlus) {
1291608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // If we're in C, we can't have :: tokens at all (the lexer won't return
1292608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them).  If the identifier is not a type, then it can't be scope either,
12931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // just early exit.
1294608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      return false;
1295eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    }
12961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
129739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // If this is a template-id, annotate with a template-id or type token.
129855f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    if (NextToken().is(tok::less)) {
12997532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor      TemplateTy Template;
1300014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
1301014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
13021fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
13031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateNameKind TNK
13047c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara          = Actions.isTemplateName(getCurScope(), SS,
13057c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   /*hasTemplateKeyword=*/false, TemplateName,
1306b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   /*ObjectType=*/ ParsedType(),
1307b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   EnteringContext,
13087c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   Template, MemberOfUnknownSpecialization)) {
1309ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
1310ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
1311059101f922de6eb765601459925f4c8914420b23Douglas Gregor        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName)) {
1312c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // If an unrecoverable error occurred, we need to return true here,
1313c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // because the token stream is in a damaged state.  We may not return
1314c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // a valid identifier.
13159ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
1316c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner        }
1317ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
131855f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    }
1319d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
132039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // The current token, which is either an identifier or a
132139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // template-id, is not part of the annotation. Fall through to
132239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // push that token back into the stream and complete the C++ scope
132339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // specifier annotation.
13241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1325eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
132639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  if (Tok.is(tok::annot_template_id)) {
13271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    TemplateIdAnnotation *TemplateId
132839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1329c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor    if (TemplateId->Kind == TNK_Type_template) {
133039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // A template-id that refers to a type was parsed into a
133139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // template-id annotation in a context where we weren't allowed
133239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // to produce a type annotation token. Update the template-id
133339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // annotation token to a type annotation token now.
1334059101f922de6eb765601459925f4c8914420b23Douglas Gregor      AnnotateTemplateIdTokenAsType();
13359ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
133639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
133739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
1338d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
13396ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (SS.isEmpty())
13409ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
13411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13426ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // A C++ scope specifier that isn't followed by a typename.
13436ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // Push the current token back into the token stream (or revert it if it is
13446ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // cached) and use an annotation scope token for current token.
13456ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (PP.isBacktrackEnabled())
13466ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.RevertCachedTokens(1);
13476ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  else
13486ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.EnterToken(Tok);
13496ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setKind(tok::annot_cxxscope);
1350c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
13516ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setAnnotationRange(SS.getRange());
13526ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner
1353ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // In case the tokens were cached, have Preprocessor replace them
1354ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // with the annotation token.  We don't need to do this if we've
1355ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // just reverted back to the state we were in before being called.
1356ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  if (!wasScopeAnnotation)
1357ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall    PP.AnnotateCachedTokens(Tok);
13589ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1359eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
1360eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
1361eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
136239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor/// annotates C++ scope specifiers and template-ids.  This returns
1363c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner/// true if the token was annotated or there was an error that could not be
1364c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner/// recovered from.
13651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
136655a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
136755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1368495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
13694bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  assert(getLang().CPlusPlus &&
13706ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner         "Call sites of this function should be guarded by checking for C++");
13713b887354b1b667c97d070ddc67b5354353c4c07bDouglas Gregor  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
13723b887354b1b667c97d070ddc67b5354353c4c07bDouglas Gregor          (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)))&&
13737452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Cannot be a type or scope token!");
1374eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
13754bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  CXXScopeSpec SS;
1376b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
13779ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return true;
1378edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin  if (SS.isEmpty())
13799ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1380eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
13816ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // Push the current token back into the token stream (or revert it if it is
13826ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // cached) and use an annotation scope token for current token.
13836ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (PP.isBacktrackEnabled())
13846ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.RevertCachedTokens(1);
13856ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  else
13866ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.EnterToken(Tok);
13876ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setKind(tok::annot_cxxscope);
1388c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
13896ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setAnnotationRange(SS.getRange());
13906ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner
13916ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // In case the tokens were cached, have Preprocessor replace them with the
13926ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // annotation token.
13936ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  PP.AnnotateCachedTokens(Tok);
13949ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1395eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
13966c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall
1397a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidisbool Parser::isTokenEqualOrMistypedEqualEqual(unsigned DiagID) {
1398a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis  if (Tok.is(tok::equalequal)) {
1399a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis    // We have '==' in a context that we would expect a '='.
1400a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis    // The user probably made a typo, intending to type '='. Emit diagnostic,
1401a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis    // fixit hint to turn '==' -> '=' and continue as if the user typed '='.
1402a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis    Diag(Tok, DiagID)
1403a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis      << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()),
1404a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis                                      getTokenSimpleSpelling(tok::equal));
1405a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis    return true;
1406a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis  }
1407a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
1408a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis  return Tok.is(tok::equal);
1409a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis}
1410a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
1411dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregorvoid Parser::CodeCompletionRecovery() {
141223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
1413dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::FnScope) {
1414f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
1415dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      return;
1416dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1417dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1418dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::ClassScope) {
1419f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
1420dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      return;
1421dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1422dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  }
1423dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1424f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
1425dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor}
1426dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
14276c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// Anchor the Parser::FieldCallback vtable to this translation unit.
14286c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// We use a spurious method instead of the destructor because
14296c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// destroying FieldCallbacks can actually be slightly
14306c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// performance-sensitive.
14316c94a6d77f456f23ecd4c2061e6413786b5e6571John McCallvoid Parser::FieldCallback::_anchor() {
14326c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall}
1433f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1434f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor// Code-completion pass-through functions
1435f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1436f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteDirective(bool InConditional) {
1437f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorDirective(InConditional);
1438f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
1439f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1440f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteInConditionalExclusion() {
1441f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
1442f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
14431fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor
14441fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregorvoid Parser::CodeCompleteMacroName(bool IsDefinition) {
1445f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1446f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1447f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1448f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompletePreprocessorExpression() {
1449f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorExpression();
1450f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1451f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1452f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1453f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       MacroInfo *MacroInfo,
1454f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       unsigned ArgumentIndex) {
1455f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1456f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                                ArgumentIndex);
14571fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor}
145855817afdf9d453a443262a733f6caf6692dca118Douglas Gregor
145955817afdf9d453a443262a733f6caf6692dca118Douglas Gregorvoid Parser::CodeCompleteNaturalLanguage() {
146055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  Actions.CodeCompleteNaturalLanguage();
146155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor}
1462f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1463f986038beed360c031de8654cfba43a5d3184605Francois Pichetbool Parser::ParseMicrosoftIfExistsCondition(bool& Result) {
1464f986038beed360c031de8654cfba43a5d3184605Francois Pichet  assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
1465f986038beed360c031de8654cfba43a5d3184605Francois Pichet         "Expected '__if_exists' or '__if_not_exists'");
1466f986038beed360c031de8654cfba43a5d3184605Francois Pichet  Token Condition = Tok;
1467f986038beed360c031de8654cfba43a5d3184605Francois Pichet  SourceLocation IfExistsLoc = ConsumeToken();
1468f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1469f986038beed360c031de8654cfba43a5d3184605Francois Pichet  SourceLocation LParenLoc = Tok.getLocation();
1470f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (Tok.isNot(tok::l_paren)) {
1471f986038beed360c031de8654cfba43a5d3184605Francois Pichet    Diag(Tok, diag::err_expected_lparen_after) << IfExistsLoc;
1472f986038beed360c031de8654cfba43a5d3184605Francois Pichet    SkipUntil(tok::semi);
1473f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1474f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1475f986038beed360c031de8654cfba43a5d3184605Francois Pichet  ConsumeParen(); // eat the '('.
1476f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1477f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Parse nested-name-specifier.
1478f986038beed360c031de8654cfba43a5d3184605Francois Pichet  CXXScopeSpec SS;
1479f986038beed360c031de8654cfba43a5d3184605Francois Pichet  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1480f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1481f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check nested-name specifier.
1482f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (SS.isInvalid()) {
1483f986038beed360c031de8654cfba43a5d3184605Francois Pichet    SkipUntil(tok::semi);
1484f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1485f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1486f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1487f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Parse the unqualified-id.
1488f986038beed360c031de8654cfba43a5d3184605Francois Pichet  UnqualifiedId Name;
1489f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
1490f986038beed360c031de8654cfba43a5d3184605Francois Pichet    SkipUntil(tok::semi);
1491f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1492f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1493f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1494f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1495f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1496f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1497f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check if the symbol exists.
1498f986038beed360c031de8654cfba43a5d3184605Francois Pichet  bool Exist = Actions.CheckMicrosoftIfExistsSymbol(SS, Name);
1499f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1500f986038beed360c031de8654cfba43a5d3184605Francois Pichet  Result = ((Condition.is(tok::kw___if_exists) && Exist) ||
1501f986038beed360c031de8654cfba43a5d3184605Francois Pichet            (Condition.is(tok::kw___if_not_exists) && !Exist));
1502f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1503f986038beed360c031de8654cfba43a5d3184605Francois Pichet  return false;
1504f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
1505f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1506f986038beed360c031de8654cfba43a5d3184605Francois Pichetvoid Parser::ParseMicrosoftIfExistsDeclaration() {
1507f986038beed360c031de8654cfba43a5d3184605Francois Pichet  bool Result;
1508f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (ParseMicrosoftIfExistsCondition(Result))
1509f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1510f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1511f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (Tok.isNot(tok::l_brace)) {
1512f986038beed360c031de8654cfba43a5d3184605Francois Pichet    Diag(Tok, diag::err_expected_lbrace);
1513f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1514f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1515f986038beed360c031de8654cfba43a5d3184605Francois Pichet  ConsumeBrace();
1516f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1517f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Condition is false skip all inside the {}.
1518f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (!Result) {
1519f986038beed360c031de8654cfba43a5d3184605Francois Pichet    SkipUntil(tok::r_brace, false);
1520f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1521f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1522f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1523f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Condition is true, parse the declaration.
1524f986038beed360c031de8654cfba43a5d3184605Francois Pichet  while (Tok.isNot(tok::r_brace)) {
1525f986038beed360c031de8654cfba43a5d3184605Francois Pichet    ParsedAttributesWithRange attrs(AttrFactory);
1526f986038beed360c031de8654cfba43a5d3184605Francois Pichet    MaybeParseCXX0XAttributes(attrs);
1527f986038beed360c031de8654cfba43a5d3184605Francois Pichet    MaybeParseMicrosoftAttributes(attrs);
1528f986038beed360c031de8654cfba43a5d3184605Francois Pichet    DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
1529f986038beed360c031de8654cfba43a5d3184605Francois Pichet    if (Result && !getCurScope()->getParent())
1530f986038beed360c031de8654cfba43a5d3184605Francois Pichet      Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
1531f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1532f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1533f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (Tok.isNot(tok::r_brace)) {
1534f986038beed360c031de8654cfba43a5d3184605Francois Pichet    Diag(Tok, diag::err_expected_rbrace);
1535f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1536f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1537f986038beed360c031de8654cfba43a5d3184605Francois Pichet  ConsumeBrace();
1538f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
1539