Parser.cpp revision 9735c5e60027b26a809df19677ff16a4d13f1321
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.
755f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid PrettyStackTraceParserEntry::print(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
127c261c76812a256c98cf3309c92f77e7ce1e8848bDouglas Gregor/// present.  If not present, it emits a corresponding diagnostic indicating
128c261c76812a256c98cf3309c92f77e7ce1e8848bDouglas Gregor/// that the parser failed to match the RHS of the token at LHSLoc.
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                           SourceLocation LHSLoc) {
131a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
132000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(RHSTok))
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return ConsumeAnyToken();
134a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation R = Tok.getLocation();
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *LHSName = "unknown";
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  diag::kind DID = diag::err_parse_error;
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (RHSTok) {
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: break;
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::greater:  LHSName = "<"; DID = diag::err_expected_greater; break;
144bf36e25224b959595af84337339103ebc542ff8cPeter Collingbourne  case tok::greatergreatergreater:
145bf36e25224b959595af84337339103ebc542ff8cPeter Collingbourne                      LHSName = "<<<"; DID = diag::err_expected_ggg; break;
1465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Diag(Tok, DID);
14828eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner  Diag(LHSLoc, diag::note_matching) << LHSName;
1499fc1873a3da0af8ef91e4f1927f3540f9af2038fChris Lattner  SkipUntil(RHSTok);
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return R;
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
153837b1a37116cf4e64f8bb7db34982dee1fba7647John McCallstatic bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
154837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  switch (ExpectedTok) {
155837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  case tok::semi: return Tok.is(tok::colon); // : for ;
156837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  default: return false;
157837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  }
158837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall}
159837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// input.  If so, it is consumed and false is returned.
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If the input is malformed, this emits the specified diagnostic.  Next, if
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipToTok is specified, it calls SkipUntil(SkipToTok).  Finally, true is
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// returned.
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const char *Msg, tok::TokenKind SkipToTok) {
168dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeAnyToken();
1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
172a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
173837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  // Detect common single-character typos and resume.
174837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  if (IsCommonTypo(ExpectedTok, Tok)) {
175837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    SourceLocation Loc = Tok.getLocation();
176837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    Diag(Loc, DiagID)
177837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall      << Msg
178837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall      << FixItHint::CreateReplacement(SourceRange(Loc),
179837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall                                      getTokenSimpleSpelling(ExpectedTok));
180837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    ConsumeAnyToken();
181837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
182837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    // Pretend there wasn't a problem.
183837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    return false;
184837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  }
185837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
1864b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  const char *Spelling = 0;
187b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
1881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (EndLoc.isValid() &&
189b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor      (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
1904b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // Show what code to insert to fix this problem.
1911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(EndLoc, DiagID)
1924b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor      << Msg
193849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateInsertion(EndLoc, Spelling);
1944b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  } else
1954b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Tok, DiagID) << Msg;
1964b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (SkipToTok != tok::unknown)
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(SkipToTok);
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2029ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregorbool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
2039ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
2049ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeAnyToken();
2059ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    return false;
2069ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  }
2079ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
2089ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
2099ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      NextToken().is(tok::semi)) {
2109ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    Diag(Tok, diag::err_extraneous_token_before_semi)
2119ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      << PP.getSpelling(Tok)
2129ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      << FixItHint::CreateRemoval(Tok.getLocation());
2139ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeAnyToken(); // The ')' or ']'.
2149ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeToken(); // The ';'.
2159ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    return false;
2169ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  }
2179ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
2189ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  return ExpectAndConsume(tok::semi, DiagID);
2199ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor}
2209ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Error recovery.
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipUntil - Read tokens until we get to the specified token, then consume
226012cf464254804279efa84e21b4b493dde76c5f1Chris Lattner/// it (unless DontConsume is true).  Because we cannot guarantee that the
2275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// token will ever occur, this skips to the next token, or to some likely
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// good stopping point.  If StopAtSemi is true, skipping will stop at a ';'
2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character.
230a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If SkipUntil finds the specified token, it returns true, otherwise it
232a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// returns false.
2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
2343437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis                       bool StopAtSemi, bool DontConsume,
2353437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis                       bool StopAtCodeCompletion) {
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We always want this function to skip at least one token if the first token
2375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // isn't T and if not at EOF.
2385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isFirstTokenSkipped = true;
2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we found one of the tokens, stop and return true.
2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumToks; ++i) {
242000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(Toks[i])) {
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (DontConsume) {
2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Noop, don't consume the token.
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        } else {
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ConsumeAnyToken();
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return true;
2495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
251a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Tok.getKind()) {
2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::eof:
2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ran out of tokens.
2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
256dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
257dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    case tok::code_completion:
2583437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      if (!StopAtCodeCompletion)
2593437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis        ConsumeToken();
260dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      return false;
261dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_paren:
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested parens.
2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
2653437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion);
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_square:
2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested square brackets.
2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
2703437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_square, false, false, StopAtCodeCompletion);
2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_brace:
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested braces.
2745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
2753437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion);
2765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
277a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Since the user wasn't looking for this token (if they were, it would
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // already be handled), this isn't balanced.  If there is a LHS token at a
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // higher level, we will assume that this matches the unbalanced token
2825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // and return it.  Otherwise, this is a spurious RHS token, which we skip.
2835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_paren:
2845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ParenCount && !isFirstTokenSkipped)
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_square:
2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BracketCount && !isFirstTokenSkipped)
2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_brace:
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BraceCount && !isFirstTokenSkipped)
2955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
298a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::string_literal:
3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::wide_string_literal:
3015cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf8_string_literal:
3025cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf16_string_literal:
3035cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf32_string_literal:
3045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeStringToken();
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
30655edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian
30755edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian    case tok::at:
30855edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian      return false;
30955edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian
3105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::semi:
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (StopAtSemi)
3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip this token.
3165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
3175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
3185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    isFirstTokenSkipped = false;
320a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump  }
3215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Scope manipulation
3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EnterScope - Start a new scope.
3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::EnterScope(unsigned ScopeFlags) {
3299e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes) {
3309e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    Scope *N = ScopeCache[--NumCachedScopes];
33123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    N->Init(getCurScope(), ScopeFlags);
33223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CurScope = N;
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
3349c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis    Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExitScope - Pop a scope off the scope stack.
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ExitScope() {
34023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() && "Scope imbalance!");
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
34290ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // Inform the actions module that this scope is going away if there are any
34390ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // decls in it.
34423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!getCurScope()->decl_empty())
34523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
346a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
34723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Scope *OldScope = getCurScope();
34823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = OldScope->getParent();
349a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
3509e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes == ScopeCacheSize)
3519e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete OldScope;
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
3539e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    ScopeCache[NumCachedScopes++] = OldScope;
3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3567a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
3577a614d8380297fcd2bc23986241905d97222948cRichard Smith/// this object does nothing.
3587a614d8380297fcd2bc23986241905d97222948cRichard SmithParser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
3597a614d8380297fcd2bc23986241905d97222948cRichard Smith                                 bool ManageFlags)
3607a614d8380297fcd2bc23986241905d97222948cRichard Smith  : CurScope(ManageFlags ? Self->getCurScope() : 0) {
3617a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (CurScope) {
3627a614d8380297fcd2bc23986241905d97222948cRichard Smith    OldFlags = CurScope->getFlags();
3637a614d8380297fcd2bc23986241905d97222948cRichard Smith    CurScope->setFlags(ScopeFlags);
3647a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
3657a614d8380297fcd2bc23986241905d97222948cRichard Smith}
3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3677a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Restore the flags for the current scope to what they were before this
3687a614d8380297fcd2bc23986241905d97222948cRichard Smith/// object overrode them.
3697a614d8380297fcd2bc23986241905d97222948cRichard SmithParser::ParseScopeFlags::~ParseScopeFlags() {
3707a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (CurScope)
3717a614d8380297fcd2bc23986241905d97222948cRichard Smith    CurScope->setFlags(OldFlags);
3727a614d8380297fcd2bc23986241905d97222948cRichard Smith}
3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// C99 6.9: External Definitions.
3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerParser::~Parser() {
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we still have scopes active, delete the scope tree.
38123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  delete getCurScope();
38223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = 0;
38323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Free the scope cache.
3859e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
3869e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete ScopeCache[i];
387fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar
3888387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Free LateParsedTemplatedFunction nodes.
3898387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin();
3908387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      it != LateParsedTemplateMap.end(); ++it)
3918387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    delete it->second;
3928387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
393fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // Remove the pragma handlers we installed.
394cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  PP.RemovePragmaHandler(AlignHandler.get());
395cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  AlignHandler.reset();
396aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
397aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  GCCVisibilityHandler.reset();
3989b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(OptionsHandler.get());
399861800c676004eabed5927f0552620d06c80a40aDaniel Dunbar  OptionsHandler.reset();
4009b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(PackHandler.get());
4014726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PackHandler.reset();
40262c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  PP.RemovePragmaHandler(MSStructHandler.get());
40362c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructHandler.reset();
4049b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(UnusedHandler.get());
4054726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  UnusedHandler.reset();
4069b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(WeakHandler.get());
4079991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman  WeakHandler.reset();
408f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
409f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  if (getLang().OpenCL) {
410f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
411f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    OpenCLExtensionHandler.reset();
412f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
413f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
414f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
415321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  PP.RemovePragmaHandler("STDC", FPContractHandler.get());
416321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  FPContractHandler.reset();
417f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  PP.clearCodeCompletionHandler();
4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Initialize - Warm up the parser.
4215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::Initialize() {
42331e057270232c1c37602579cb6461c2704175672Chris Lattner  // Create the translation unit scope.  Install it as the current scope.
42423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() == 0 && "A scope is already active?");
42531e057270232c1c37602579cb6461c2704175672Chris Lattner  EnterScope(Scope::DeclScope);
426c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  Actions.ActOnTranslationUnitScope(getCurScope());
427c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor
428c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  // Prime the lexer look-ahead.
429c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  ConsumeToken();
430a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
431000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(tok::eof) &&
432f72617544287acf0281c3b1a733bcb22a02e6ca4Chris Lattner      !getLang().CPlusPlus)  // Empty source file is an extension in C
4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::ext_empty_source_file);
434a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
43534870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  // Initialization for Objective-C context sensitive keywords recognition.
436a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  // Referenced in Parser::ParseObjCTypeQualifierList.
43734870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  if (getLang().ObjC1) {
438a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
439a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
440a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
441a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
442a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
443a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
44434870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  }
445662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar
4467eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson  Ident_final = 0;
4477eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson  Ident_override = 0;
4481f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson
449662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar  Ident_super = &PP.getIdentifierTable().get("super");
45082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson
45182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  if (getLang().AltiVec) {
45282287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_vector = &PP.getIdentifierTable().get("vector");
45382287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_pixel = &PP.getIdentifierTable().get("pixel");
45482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  }
4550a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4560a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_introduced = 0;
4570a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_deprecated = 0;
4580a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_obsoleted = 0;
459b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  Ident_unavailable = 0;
46028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
46128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0;
46228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0;
46328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0;
46428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
46528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  if(getLang().Borland) {
46628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__exception_info        = PP.getIdentifierInfo("_exception_info");
46728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___exception_info       = PP.getIdentifierInfo("__exception_info");
46828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_GetExceptionInfo       = PP.getIdentifierInfo("GetExceptionInformation");
46928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__exception_code        = PP.getIdentifierInfo("_exception_code");
47028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___exception_code       = PP.getIdentifierInfo("__exception_code");
47128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_GetExceptionCode       = PP.getIdentifierInfo("GetExceptionCode");
47228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__abnormal_termination  = PP.getIdentifierInfo("_abnormal_termination");
47328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
47428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_AbnormalTermination    = PP.getIdentifierInfo("AbnormalTermination");
47528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
47628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
47728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
47828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
47928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
48028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
48128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
48228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
48328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
48428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
48528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
4895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// action tells us to.  This returns true if the EOF was encountered.
490682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattnerbool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
49125a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
492b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
493b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  while (Tok.is(tok::annot_pragma_unused))
494b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis    HandlePragmaUnused();
495b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
496682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  Result = DeclGroupPtrTy();
4979299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  if (Tok.is(tok::eof)) {
4988387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    // Late template parsing can begin.
4998387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (getLang().DelayedTemplateParsing)
5008387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
5018387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
5029299f3fa85796613cc787a2062c9562d07c8613eChris Lattner    Actions.ActOnEndOfTranslationUnit();
5039299f3fa85796613cc787a2062c9562d07c8613eChris Lattner    return true;
5049299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  }
505a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
5060b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributesWithRange attrs(AttrFactory);
5077f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseCXX0XAttributes(attrs);
5087f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseMicrosoftAttributes(attrs);
509334d47e92e9f241576fdeb7477b69a03136ba854Francois Pichet
5107f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  Result = ParseExternalDeclaration(attrs);
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTranslationUnit:
5155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       translation-unit: [C99 6.9]
516a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///         external-declaration
517a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///         translation-unit external-declaration
5185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseTranslationUnit() {
5198935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  Initialize();
520a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
521682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  DeclGroupPtrTy Res;
52289307ffaf8acf4d6fdffd72b607ca4fbcfdffc9dSteve Naroff  while (!ParseTopLevelDecl(Res))
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /*parse them all*/;
5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52506f548596beef4c0a227a45cba996497f99566c0Chris Lattner  ExitScope();
52623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() == 0 && "Scope imbalance!");
5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseExternalDeclaration:
53090b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner///
531c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
532c3018153a11afe91849748a93d920040a571b76cChris Lattner///         function-definition
533c3018153a11afe91849748a93d920040a571b76cChris Lattner///         declaration
534a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration
5355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU]   asm-definition
536c3018153a11afe91849748a93d920040a571b76cChris Lattner/// [GNU]   __extension__ external-declaration
5375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-declaration
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-alias-declaration
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-protocol-definition
5415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-method-definition
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  @end
543c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor/// [C++]   linkage-specification
5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-definition:
5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         simple-asm-expr ';'
5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
547a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration:
548a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///           ';'
549a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///
55045f965581935791a018df829a14dff53c1dd8f47Douglas Gregor/// [C++0x/GNU] 'extern' 'template' declaration
5517f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::DeclGroupPtrTy
5527f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
5537f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                 ParsingDeclSpec *DS) {
55425a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
55536d36806f1972f7ec1d2a3f59155187278c56508Argyrios Kyrtzidis  ParenBraceBracketBalancer BalancerRAIIObj(*this);
55636d36806f1972f7ec1d2a3f59155187278c56508Argyrios Kyrtzidis
557d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *SingleDecl = 0;
5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Tok.getKind()) {
5595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::semi:
560a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor    if (!getLang().CPlusPlus0x)
561a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor      Diag(Tok, diag::ext_top_level_semi)
562849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
5631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TODO: Invoke action for top-level semicolon.
566682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
56790b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::r_brace:
56890b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    Diag(Tok, diag::err_expected_external_declaration);
56990b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    ConsumeBrace();
570682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
57190b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::eof:
57290b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    Diag(Tok, diag::err_expected_external_declaration);
573682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
574c3018153a11afe91849748a93d920040a571b76cChris Lattner  case tok::kw___extension__: {
575c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    // __extension__ silences extension warnings in the subexpression.
576c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
57739146d6497ad5e7ca8ef639221e7b3e15d07c888Chris Lattner    ConsumeToken();
5787f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    return ParseExternalDeclaration(attrs);
579c3018153a11afe91849748a93d920040a571b76cChris Lattner  }
580dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  case tok::kw_asm: {
5817f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ProhibitAttributes(attrs);
582bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
58321e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation StartLoc = Tok.getLocation();
58421e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation EndLoc;
58521e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    ExprResult Result(ParseSimpleAsm(&EndLoc));
586a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
5873f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson    ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
5883f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson                     "top-level asm block");
589dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson
590682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    if (Result.isInvalid())
591682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
59221e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
593682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
594dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  }
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::at:
596682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    // @ is not a legal token unless objc is enabled, no need to check for ObjC.
597682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    /// FIXME: ParseObjCAtDirectives should return a DeclGroup for things like
598682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    /// @class foo, bar;
599682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = ParseObjCAtDirectives();
600682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::minus:
6025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::plus:
603682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    if (!getLang().ObjC1) {
604682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      Diag(Tok, diag::err_expected_external_declaration);
605682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      ConsumeToken();
606682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
607682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    }
608682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = ParseObjCMethodDefinition();
609682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
610791215b7a24666912c0b71175d2ca5ba082f666eDouglas Gregor  case tok::code_completion:
61123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
612f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                   ObjCImpDecl? Sema::PCC_ObjCImplementation
613f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                              : Sema::PCC_Namespace);
614dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
6157f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    return ParseExternalDeclaration(attrs);
616f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  case tok::kw_using:
6178f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  case tok::kw_namespace:
6185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_typedef:
619adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_template:
620adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_export:    // As in 'export template'
621511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  case tok::kw_static_assert:
622c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne  case tok::kw__Static_assert:
623bae35118fc5cea2da08567dbb9763af7f906dae2Chris Lattner    // A function definition cannot start with a these keywords.
62497144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    {
62597144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner      SourceLocation DeclEnd;
626c5be7b0fc804d8e6f87298ec03c94d8cccd74f29Fariborz Jahanian      StmtVector Stmts(Actions);
6277f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
62897144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    }
629d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
6307306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_static:
6317306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // Parse (then ignore) 'static' prior to a template instantiation. This is
6327306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // a GCC extension that we intentionally do not support.
6337306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
6347306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
6357306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        << 0;
636d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl      SourceLocation DeclEnd;
637c5be7b0fc804d8e6f87298ec03c94d8cccd74f29Fariborz Jahanian      StmtVector Stmts(Actions);
6387f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
6397306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    }
6407306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    goto dont_know;
6417306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
6427306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_inline:
6437306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    if (getLang().CPlusPlus) {
6447306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      tok::TokenKind NextKind = NextToken().getKind();
6457306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
6467306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Inline namespaces. Allowed as an extension even in C++03.
6477306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_namespace) {
6487306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
6497306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        StmtVector Stmts(Actions);
6507f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
6517306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
6527306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
6537306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Parse (then ignore) 'inline' prior to a template instantiation. This is
6547306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // a GCC extension that we intentionally do not support.
6557306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_template) {
6567306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
6577306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor          << 1;
6587306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
6597306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        StmtVector Stmts(Actions);
6607f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
6617306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
662d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    }
663d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
664d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
66545f965581935791a018df829a14dff53c1dd8f47Douglas Gregor  case tok::kw_extern:
66645f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
66745f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      // Extern templates
66845f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation ExternLoc = ConsumeToken();
66945f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation TemplateLoc = ConsumeToken();
67045f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation DeclEnd;
67145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      return Actions.ConvertDeclToDeclGroup(
67245f965581935791a018df829a14dff53c1dd8f47Douglas Gregor                  ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd));
67345f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    }
67445f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    // FIXME: Detect C++ linkage specifications here?
675d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
677f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_exists:
678f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_not_exists:
679563a645de82231a55e221fe655b7188bf8369662Francois Pichet    ParseMicrosoftIfExistsExternalDeclaration();
680f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return DeclGroupPtrTy();
681f986038beed360c031de8654cfba43a5d3184605Francois Pichet
6825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
683d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl  dont_know:
6845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // We can't tell whether this is a function-definition or declaration yet.
6857f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (DS) {
6867f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      DS->takeAttributesFrom(attrs);
6877f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclarationOrFunctionDefinition(*DS);
6887f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    } else {
6897f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclarationOrFunctionDefinition(attrs);
6907f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    }
6915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
693682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // This routine returns a DeclGroup, if the thing we parsed only contains a
694682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // single decl, convert it now.
695682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  return Actions.ConvertDeclToDeclGroup(SingleDecl);
6965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6981426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
6991426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, continues a declaration or declaration list.
700e4246a633b13197634225971b25df0cbdcec0c5dSean Huntbool Parser::isDeclarationAfterDeclarator() {
701e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  // Check for '= delete' or '= default'
702e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  if (getLang().CPlusPlus && Tok.is(tok::equal)) {
703e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
704e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
705e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      return false;
706e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
707e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
7081426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor  return Tok.is(tok::equal) ||      // int X()=  -> not a function def
7091426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::comma) ||           // int X(),  -> not a function def
7101426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::semi)  ||           // int X();  -> not a function def
7111426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
7121426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
7131426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    (getLang().CPlusPlus &&
7141426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor     Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
7151426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
7161426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
7171426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
7181426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, indicates the start of a function definition.
719004659a56916f2f81ede507c12516c146d6c0df3Chris Lattnerbool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
720075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
7215d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  if (Tok.is(tok::l_brace))   // int X() {}
7225d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner    return true;
7235d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner
724004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  // Handle K&R C argument lists: int X(f) int f; {}
725004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  if (!getLang().CPlusPlus &&
726075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      Declarator.getFunctionTypeInfo().isKNRPrototype())
727004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner    return isDeclarationSpecifier();
728e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
729e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  if (getLang().CPlusPlus && Tok.is(tok::equal)) {
730e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
731e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
732e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
733004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner
7345d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
7355d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner         Tok.is(tok::kw_try);          // X() try { ... }
7361426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
7371426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
7395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// a declaration.  We can't tell which we have until we read up to the
740c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// compound-statement in function-definition. TemplateParams, if
741c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// non-NULL, provides the template parameters when we're parsing a
7421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ template-declaration.
7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       function-definition: [C99 6.9.1]
745a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
746a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
747a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
748a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///
7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       declaration: [C99 6.7]
750697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner///         declaration-specifiers init-declarator-list[opt] ';'
751697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner/// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OMP]   threadprivate-directive                              [TODO]
7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
754682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerParser::DeclGroupPtrTy
7553acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
756bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt                                             AccessSpecifier AS) {
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Parse the common declaration-specifiers piece.
7580efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
759a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // declaration-specifiers init-declarator-list[opt] ';'
762000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(tok::semi)) {
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
764d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
76554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.complete(TheDecl);
766682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
7675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
768a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
769246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // ObjC2 allows prefix attributes on class interfaces and protocols.
770246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // FIXME: This still needs better diagnostics. We should only accept
771246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // attributes here, no types, etc.
772000732226610650837478cba97843d19b75f648eChris Lattner  if (getLang().ObjC2 && Tok.is(tok::at)) {
773dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation AtLoc = ConsumeToken(); // the "@"
7741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
775246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar        !Tok.isObjCAtKeyword(tok::objc_protocol)) {
776246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar      Diag(Tok, diag::err_objc_unexpected_attr);
777cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      SkipUntil(tok::semi); // FIXME: better skip?
778682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
779cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    }
780d8ac05753dc4506224d445ff98399c01da3136e5John McCall
78154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.abort();
78254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
7830de2ae28c603322f05e2d9200c7d457c8b928983Fariborz Jahanian    const char *PrevSpec = 0;
784fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
785fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
786fec54013fcd0eb72642741584ca04c1bc292bef8John McCall      Diag(AtLoc, DiagID) << PrevSpec;
7871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
788d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = 0;
789246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar    if (Tok.isObjCAtKeyword(tok::objc_protocol))
790682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
791682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    else
792682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
793682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
794dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
795a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
796c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // If the declspec consisted only of 'extern' and we have a string
797c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // literal following it, this must be a C++ linkage specifier like
798c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // 'extern "C"'.
7993c6f6a7a1bb9969112617a26d2333bab2f6efd65Chris Lattner  if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
800c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner      DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
801682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
802d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
803682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
804682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  }
805c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
806d8ac05753dc4506224d445ff98399c01da3136e5John McCall  return ParseDeclGroup(DS, Declarator::FileContext, true);
8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8093acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::DeclGroupPtrTy
8107f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::ParseDeclarationOrFunctionDefinition(ParsedAttributes &attrs,
8113acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian                                             AccessSpecifier AS) {
8123acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian  ParsingDeclSpec DS(*this);
8137f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  DS.takeAttributesFrom(attrs);
8149735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  // Must temporarily exit the objective-c container scope for
8159735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  // parsing c constructs and re-enter objc container scope
8169735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  // afterwards.
8179735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  ObjCDeclContextSwitch ObjCDC(*this);
8189735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian
8199735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  return ParseDeclarationOrFunctionDefinition(DS, AS);
8203acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian}
8213acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian
8225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseFunctionDefinition - We parsed and verified that the specified
8235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Declarator is well formed.  If this is a K&R-style function, read the
8245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// parameters declaration-list, then start the compound-statement.
8255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
826a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///       function-definition: [C99 6.9.1]
827a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
828a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
829a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
8307ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
83123c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
83223c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         function-body
8337ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
834d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl///         decl-specifier-seq[opt] declarator function-try-block
8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
836d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
837075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara                                      const ParsedTemplateInfo &TemplateInfo) {
83828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // Poison the SEH identifiers so they are flagged as illegal in function bodies
83928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
840075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
841a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
842a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // If this is C90 and the declspecs were completely missing, fudge in an
843a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // implicit int.  We do this here because this is the only place where
844a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // declaration-specifiers are completely optional in the grammar.
8452a327d11a07dfbdf20910cebbae38910eda111fdChris Lattner  if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) {
846a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner    const char *PrevSpec;
847fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
84831c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner    D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
84931c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner                                           D.getIdentifierLoc(),
850fec54013fcd0eb72642741584ca04c1bc292bef8John McCall                                           PrevSpec, DiagID);
851ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
852a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  }
853a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this declaration was formed with a K&R-style identifier list for the
8555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // arguments, parse declarations for all of the args next.
8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // int foo(a,b) int a; float b; {}
857004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  if (FTI.isKNRPrototype())
8585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseKNRParamDeclarations(D);
8595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
860e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
8617ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // We should have either an opening brace or, in a C++ constructor,
8627ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // we may have a colon.
863758afbcc86ef15f8d433f5f87db1495e50effeb3Douglas Gregor  if (Tok.isNot(tok::l_brace) &&
864758afbcc86ef15f8d433f5f87db1495e50effeb3Douglas Gregor      (!getLang().CPlusPlus ||
865cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt       (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
866cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        Tok.isNot(tok::equal)))) {
8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::err_expected_fn_body);
8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(tok::l_brace, true, true);
871a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we didn't find the '{', bail out.
873000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.isNot(tok::l_brace))
874d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
876a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8778387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // In delayed template parsing mode, for function template we consume the
8788387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // tokens and store them for late parsing at the end of the translation unit.
8798387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  if (getLang().DelayedTemplateParsing &&
8808387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      TemplateInfo.Kind == ParsedTemplateInfo::Template) {
8818387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    MultiTemplateParamsArg TemplateParameterLists(Actions,
8828387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet                                         TemplateInfo.TemplateParams->data(),
8838387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet                                         TemplateInfo.TemplateParams->size());
8848387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
8858387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
8868387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Scope *ParentScope = getCurScope()->getParent();
8878387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
8888387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Decl *DP = Actions.HandleDeclarator(ParentScope, D,
8898387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet                                move(TemplateParameterLists),
8908387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet                                /*IsFunctionDefinition=*/true);
8918387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.complete(DP);
8928387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.getMutableDeclSpec().abort();
8938387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
8948387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (DP) {
8958387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(this, DP);
8968387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
8978387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      FunctionDecl *FnD = 0;
8988387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
8998387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = FunTmpl->getTemplatedDecl();
9008387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      else
9018387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = cast<FunctionDecl>(DP);
902d4a0caf78e7c18e7aca65fbfd799a6c024ff51fbFrancois Pichet      Actions.CheckForFunctionRedefinition(FnD);
9038387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9048387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateParsedTemplateMap[FnD] = LPT;
9058387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.MarkAsLateParsedTemplate(FnD);
9068387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(LPT->Toks);
9078387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    } else {
9088387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      CachedTokens Toks;
9098387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(Toks);
9108387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    }
9118387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return DP;
9128387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
9138387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
914b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Enter a scope for the function body.
9158935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
916a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
917b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Tell the actions module that we have entered a function definition with the
918b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // specified Declarator for the function.
919d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Res = TemplateInfo.TemplateParams?
92023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
921f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                              MultiTemplateParamsArg(Actions,
92252591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                                          TemplateInfo.TemplateParams->data(),
92352591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                                         TemplateInfo.TemplateParams->size()),
92452591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                                              D)
92523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
926a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
92754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclarator context before we parse the body.
92854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.complete(Res);
92954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
93054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclSpec context, too.  This const_cast is
93154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // safe because we're always the sole owner.
93254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.getMutableDeclSpec().abort();
93354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
934cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  if (Tok.is(tok::equal)) {
935cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    assert(getLang().CPlusPlus && "Only C++ function definitions have '='");
936cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ConsumeToken();
937cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
938cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    Actions.ActOnFinishFunctionBody(Res, 0, false);
939cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
940cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    bool Delete = false;
941cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    SourceLocation KWLoc;
942cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Tok.is(tok::kw_delete)) {
943cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      if (!getLang().CPlusPlus0x)
944cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
945cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
946cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      KWLoc = ConsumeToken();
947cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Actions.SetDeclDeleted(Res, KWLoc);
948cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Delete = true;
949cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else if (Tok.is(tok::kw_default)) {
950cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      if (!getLang().CPlusPlus0x)
951cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        Diag(Tok, diag::warn_defaulted_function_accepted_as_extension);
952cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
953cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      KWLoc = ConsumeToken();
954cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Actions.SetDeclDefaulted(Res, KWLoc);
955cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else {
956cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      llvm_unreachable("function definition after = not 'delete' or 'default'");
957cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
958cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
959cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Tok.is(tok::comma)) {
960cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
961cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        << Delete;
962cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      SkipUntil(tok::semi);
963cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else {
964cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
965cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                       Delete ? "delete" : "default", tok::semi);
966cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
967cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
968cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    return Res;
969cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  }
970cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
971d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (Tok.is(tok::kw_try))
972c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor    return ParseFunctionTryBlock(Res, BodyScope);
973d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
9747ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // If we have a colon, then we're probably parsing a C++
9757ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // ctor-initializer.
976d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  if (Tok.is(tok::colon)) {
9777ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    ParseConstructorInitializer(Res);
978d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall
979d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    // Recover from error.
980d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    if (!Tok.is(tok::l_brace)) {
981c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor      BodyScope.Exit();
9829ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.ActOnFinishFunctionBody(Res, 0);
983d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall      return Res;
984d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    }
985d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  } else
986393612e6c7727f1fee50039254d9f434364cc0b2Fariborz Jahanian    Actions.ActOnDefaultCtorInitializers(Res);
9877ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
988c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor  return ParseFunctionStatementBody(Res, BodyScope);
9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
9925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// types for a function with a K&R-style identifier list for arguments.
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseKNRParamDeclarations(Declarator &D) {
9945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We know that the top-level of this declarator is a function.
995075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
99704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // Enter function-declaration scope, limiting any declarators to the
99804421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // function prototype scope, including parameter declarators.
9993218c4bb3b5d7250f12420de6db7ef3e3f805a75Douglas Gregor  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
100004421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner
10015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read all the argument declarations.
10025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isDeclarationSpecifier()) {
10035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SourceLocation DSStart = Tok.getLocation();
1004a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the common declaration-specifiers piece.
10060b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
10075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarationSpecifiers(DS);
1008a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: 'each declaration in the declaration list shall have at
10105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // least one declarator'.
10115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
10125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // the declarations though.  It's trivial to ignore them, really hard to do
10135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // anything else with them.
1014000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.is(tok::semi)) {
10155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DSStart, diag::err_declaration_does_not_declare_param);
10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
10185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1019a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // than register.
10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
10235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        DS.getStorageClassSpec() != DeclSpec::SCS_register) {
10245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getStorageClassSpecLoc(),
10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
10265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
10275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.isThreadSpecified()) {
10295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getThreadSpecLoc(),
10305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
10315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
10325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1033a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the first declarator attached to this declspec.
10355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
10365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarator(ParmDeclarator);
10375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Handle the full declarator list.
10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (1) {
10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If attributes are present, parse them.
10417f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      MaybeParseGNUAttributes(ParmDeclarator);
1042a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ask the actions module to compute the type for this declarator.
1044d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *Param =
104523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
10462bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff
1047a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump      if (Param &&
10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // A missing identifier has already been diagnosed.
10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ParmDeclarator.getIdentifier()) {
10505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Scan the argument list looking for the correct param to apply this
10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // type.
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0; ; ++i) {
10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // C99 6.9.1p6: those declarators shall declare only identifiers from
10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // the identifier list.
10565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (i == FTI.NumArgs) {
10571ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner            Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
10586898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner              << ParmDeclarator.getIdentifier();
10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
1061a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
10635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Reject redefinitions of parameters.
106404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner            if (FTI.ArgInfo[i].Param) {
10655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(ParmDeclarator.getIdentifierLoc(),
10661ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner                   diag::err_param_redefinition)
10676898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner                 << ParmDeclarator.getIdentifier();
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            } else {
106904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner              FTI.ArgInfo[i].Param = Param;
10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we don't have a comma, it is either the end of the list (a ';') or
10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // an error, bail out.
1078000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.isNot(tok::comma))
10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
1080a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Consume the comma.
10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
1083a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Parse the next declarator.
10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParmDeclarator.clear();
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParseDeclarator(ParmDeclarator);
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1088a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1089000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.is(tok::semi)) {
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_parse_error);
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip to end of block or statement
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi, true);
1095000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(tok::semi))
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ConsumeToken();
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1099a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The actions module must verify that all arguments were declared.
110123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// allowed to be a wide string, and is not subject to character translation.
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-string-literal:
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         string-literal
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
111160d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseAsmStringLiteral() {
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!isTokenStringLiteral()) {
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::err_expected_string_literal);
111461364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl    return ExprError();
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1116a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
111760d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseStringLiteralExpression());
1118effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  if (Res.isInvalid()) return move(Res);
1119a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // TODO: Diagnose: wide string literal in 'asm'
1121a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1122effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  return move(Res);
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseSimpleAsm
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] simple-asm-expr:
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'asm' '(' asm-string-literal ')'
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
113060d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
1131000732226610650837478cba97843d19b75f648eChris Lattner  assert(Tok.is(tok::kw_asm) && "Not an asm!");
1132dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  SourceLocation Loc = ConsumeToken();
1133a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11347a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  if (Tok.is(tok::kw_volatile)) {
1135841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    // Remove from the end of 'asm' to the end of 'volatile'.
1136841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1137841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall                             PP.getLocForEndOfToken(Tok.getLocation()));
1138841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall
1139841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    Diag(Tok, diag::warn_file_asm_volatile)
1140849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateRemoval(RemovalRange);
11417a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall    ConsumeToken();
11427a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  }
11437a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall
1144000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.isNot(tok::l_paren)) {
11451ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "asm";
114661364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl    return ExprError();
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1148a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1149ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  Loc = ConsumeParen();
1150a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
115160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(ParseAsmStringLiteral());
1152a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1153ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Result.isInvalid()) {
1154ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SkipUntil(tok::r_paren, true, true);
1155ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1156ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Tok.getLocation();
1157ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    ConsumeAnyToken();
1158ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  } else {
1159ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    Loc = MatchRHSPunctuation(tok::r_paren, Loc);
1160ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1161ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Loc;
1162ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
1163a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1164effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  return move(Result);
11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
116725a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// \brief Get the TemplateIdAnnotation from the token and put it in the
116825a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// cleanup pool so that it gets destroyed when parsing the current top level
116925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// declaration is finished.
117025a767651d14db87aa03dd5fe3e011d877dd4100Argyrios KyrtzidisTemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
117125a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  assert(tok.is(tok::annot_template_id) && "Expected template-id token");
117225a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  TemplateIdAnnotation *
117325a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
117425a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  TopLevelDeclCleanupPool.delayMemberFunc< TemplateIdAnnotation,
117525a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis                                          &TemplateIdAnnotation::Destroy>(Id);
117625a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  return Id;
117725a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis}
117825a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis
1179eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateTypeOrScopeToken - If the current token position is on a
1180eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typename (possibly qualified in C++) or a C++ scope specifier not followed
1181eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1182eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// with a single annotation token representing the typename or C++ scope
1183eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// respectively.
1184eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This simplifies handling of C++ scope specifiers and allows efficient
1185eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// backtracking without the need to re-parse and resolve nested-names and
1186eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typenames.
118744802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// It will mainly be called when we expect to treat identifiers as typenames
118844802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// (if they are typenames). For example, in C we do not expect identifiers
118944802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// inside expressions to be treated as typenames so it will not be called
119044802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// for expressions in C.
119144802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// The benefit for C/ObjC is that a typename will be annotated and
1192b43a50ff1b0b171ece84425b0ad83a9a31f038faSteve Naroff/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
119344802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// will not be called twice, once to check whether we have a declaration
119444802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// specifier, and another one to get the actual type inside
119544802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// ParseDeclarationSpecifiers).
1196a7bc7c880f86bc180684ef032d06df51bcae7a23Chris Lattner///
11979ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// This returns true if an error occurred.
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
119955a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
120055a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1201495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext) {
12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
1203ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall          || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) &&
12047452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Cannot be a type or scope token!");
12051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1206d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Tok.is(tok::kw_typename)) {
1207d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Parse a C++ typename-specifier, e.g., "typename T::type".
1208d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //
1209d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //   typename-specifier:
1210d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //     'typename' '::' [opt] nested-name-specifier identifier
12111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     'typename' '::' [opt] nested-name-specifier template [opt]
12121734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    //            simple-template-id
1213d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    SourceLocation TypenameLoc = ConsumeToken();
1214d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    CXXScopeSpec SS;
12154147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(), false,
12164147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                       0, /*IsTypename*/true))
12179ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
12189ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    if (!SS.isSet()) {
1219a5d318ad4ad3a110b3abb34f765e69b76f7c0bcdFrancois Pichet      if (getLang().Microsoft)
1220a5d318ad4ad3a110b3abb34f765e69b76f7c0bcdFrancois Pichet        Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
1221a5d318ad4ad3a110b3abb34f765e69b76f7c0bcdFrancois Pichet      else
1222a5d318ad4ad3a110b3abb34f765e69b76f7c0bcdFrancois Pichet        Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
12239ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1224d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    }
1225d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1226d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    TypeResult Ty;
1227d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (Tok.is(tok::identifier)) {
1228d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      // FIXME: check whether the next token is '<', first!
122923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
12301a15dae8be2b28e02b6639aa92b832465c5be420Douglas Gregor                                     *Tok.getIdentifierInfo(),
1231d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     Tok.getLocation());
12321734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else if (Tok.is(tok::annot_template_id)) {
123325a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
12341734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      if (TemplateId->Kind == TNK_Function_template) {
12351734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        Diag(Tok, diag::err_typename_refers_to_non_type_template)
12361734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor          << Tok.getAnnotationRange();
12379ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
12381734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      }
1239d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1240a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor      ASTTemplateArgsPtr TemplateArgsPtr(Actions,
1241a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                         TemplateId->getTemplateArgs(),
1242a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                         TemplateId->NumArgs);
1243a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor
1244a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1245a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     /*FIXME:*/SourceLocation(),
1246a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->Template,
1247a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->TemplateNameLoc,
1248a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->LAngleLoc,
1249a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateArgsPtr,
1250a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->RAngleLoc);
12511734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else {
12521734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Diag(Tok, diag::err_expected_type_name_after_typename)
12531734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        << SS.getRange();
12549ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
12551734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
12561734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
125739d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    SourceLocation EndLoc = Tok.getLastLoc();
12581734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setKind(tok::annot_typename);
1259b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
126039d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    Tok.setAnnotationEndLoc(EndLoc);
12611734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setLocation(TypenameLoc);
12621734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    PP.AnnotateCachedTokens(Tok);
12639ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1264d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1265d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1266ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // Remembers whether the token was originally a scope annotation.
1267ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1268ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall
1269eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
12704bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  if (getLang().CPlusPlus)
1271b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
12729ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1273eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
1274eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::identifier)) {
1275608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner    // Determine whether the identifier is a type name.
1276b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
1277b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            Tok.getLocation(), getCurScope(),
12781e52dfc648ce0b25ef57ae29ef1b4337d80011efFariborz Jahanian                                            &SS, false,
12799e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            NextToken().is(tok::period),
12809e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            ParsedType(),
12819e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            /*NonTrivialTypeSourceInfo*/true)) {
1282608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // This is a typename. Replace the current token in-place with an
1283608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // annotation type token.
1284b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner      Tok.setKind(tok::annot_typename);
1285b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      setTypeAnnotation(Tok, Ty);
1286608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      Tok.setAnnotationEndLoc(Tok.getLocation());
1287608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      if (SS.isNotEmpty()) // it was a C++ qualified type name.
1288608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner        Tok.setLocation(SS.getBeginLoc());
12891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1290608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // In case the tokens were cached, have Preprocessor replace
1291608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them with the annotation token.
1292608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      PP.AnnotateCachedTokens(Tok);
12939ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
12941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
129539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
129639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (!getLang().CPlusPlus) {
1297608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // If we're in C, we can't have :: tokens at all (the lexer won't return
1298608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them).  If the identifier is not a type, then it can't be scope either,
12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // just early exit.
1300608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      return false;
1301eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    }
13021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
130339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // If this is a template-id, annotate with a template-id or type token.
130455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    if (NextToken().is(tok::less)) {
13057532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor      TemplateTy Template;
1306014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
1307014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
13081fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
13091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateNameKind TNK
13107c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara          = Actions.isTemplateName(getCurScope(), SS,
13117c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   /*hasTemplateKeyword=*/false, TemplateName,
1312b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   /*ObjectType=*/ ParsedType(),
1313b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   EnteringContext,
13147c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   Template, MemberOfUnknownSpecialization)) {
1315ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
1316ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
1317059101f922de6eb765601459925f4c8914420b23Douglas Gregor        if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName)) {
1318c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // If an unrecoverable error occurred, we need to return true here,
1319c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // because the token stream is in a damaged state.  We may not return
1320c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // a valid identifier.
13219ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
1322c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner        }
1323ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
132455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    }
1325d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
132639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // The current token, which is either an identifier or a
132739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // template-id, is not part of the annotation. Fall through to
132839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // push that token back into the stream and complete the C++ scope
132939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // specifier annotation.
13301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1331eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
133239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  if (Tok.is(tok::annot_template_id)) {
133325a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1334c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor    if (TemplateId->Kind == TNK_Type_template) {
133539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // A template-id that refers to a type was parsed into a
133639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // template-id annotation in a context where we weren't allowed
133739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // to produce a type annotation token. Update the template-id
133839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // annotation token to a type annotation token now.
1339059101f922de6eb765601459925f4c8914420b23Douglas Gregor      AnnotateTemplateIdTokenAsType();
13409ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
134139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
134239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
1343d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
13446ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (SS.isEmpty())
13459ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13476ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // A C++ scope specifier that isn't followed by a typename.
13486ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // Push the current token back into the token stream (or revert it if it is
13496ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // cached) and use an annotation scope token for current token.
13506ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (PP.isBacktrackEnabled())
13516ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.RevertCachedTokens(1);
13526ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  else
13536ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.EnterToken(Tok);
13546ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setKind(tok::annot_cxxscope);
1355c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
13566ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setAnnotationRange(SS.getRange());
13576ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner
1358ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // In case the tokens were cached, have Preprocessor replace them
1359ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // with the annotation token.  We don't need to do this if we've
1360ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // just reverted back to the state we were in before being called.
1361ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  if (!wasScopeAnnotation)
1362ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall    PP.AnnotateCachedTokens(Tok);
13639ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1364eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
1365eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
1366eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
136739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor/// annotates C++ scope specifiers and template-ids.  This returns
1368c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner/// true if the token was annotated or there was an error that could not be
1369c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner/// recovered from.
13701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
137155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
137255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1373495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
13744bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  assert(getLang().CPlusPlus &&
13756ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner         "Call sites of this function should be guarded by checking for C++");
13763b887354b1b667c97d070ddc67b5354353c4c07bDouglas Gregor  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
13773b887354b1b667c97d070ddc67b5354353c4c07bDouglas Gregor          (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)))&&
13787452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Cannot be a type or scope token!");
1379eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
13804bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  CXXScopeSpec SS;
1381b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
13829ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return true;
1383edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin  if (SS.isEmpty())
13849ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1385eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
13866ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // Push the current token back into the token stream (or revert it if it is
13876ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // cached) and use an annotation scope token for current token.
13886ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (PP.isBacktrackEnabled())
13896ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.RevertCachedTokens(1);
13906ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  else
13916ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.EnterToken(Tok);
13926ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setKind(tok::annot_cxxscope);
1393c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
13946ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setAnnotationRange(SS.getRange());
13956ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner
13966ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // In case the tokens were cached, have Preprocessor replace them with the
13976ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // annotation token.
13986ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  PP.AnnotateCachedTokens(Tok);
13999ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1400eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
14016c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall
1402a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidisbool Parser::isTokenEqualOrMistypedEqualEqual(unsigned DiagID) {
1403a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis  if (Tok.is(tok::equalequal)) {
1404a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis    // We have '==' in a context that we would expect a '='.
1405a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis    // The user probably made a typo, intending to type '='. Emit diagnostic,
1406a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis    // fixit hint to turn '==' -> '=' and continue as if the user typed '='.
1407a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis    Diag(Tok, DiagID)
1408a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis      << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()),
1409a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis                                      getTokenSimpleSpelling(tok::equal));
1410a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis    return true;
1411a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis  }
1412a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
1413a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis  return Tok.is(tok::equal);
1414a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis}
1415a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
1416dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregorvoid Parser::CodeCompletionRecovery() {
141723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
1418dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::FnScope) {
1419f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
1420dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      return;
1421dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1422dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1423dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::ClassScope) {
1424f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
1425dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      return;
1426dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1427dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  }
1428dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1429f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
1430dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor}
1431dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
14326c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// Anchor the Parser::FieldCallback vtable to this translation unit.
14336c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// We use a spurious method instead of the destructor because
14346c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// destroying FieldCallbacks can actually be slightly
14356c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// performance-sensitive.
14366c94a6d77f456f23ecd4c2061e6413786b5e6571John McCallvoid Parser::FieldCallback::_anchor() {
14376c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall}
1438f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1439f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor// Code-completion pass-through functions
1440f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1441f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteDirective(bool InConditional) {
1442f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorDirective(InConditional);
1443f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
1444f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1445f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteInConditionalExclusion() {
1446f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
1447f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
14481fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor
14491fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregorvoid Parser::CodeCompleteMacroName(bool IsDefinition) {
1450f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1451f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1452f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1453f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompletePreprocessorExpression() {
1454f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorExpression();
1455f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1456f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1457f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1458f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       MacroInfo *MacroInfo,
1459f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       unsigned ArgumentIndex) {
1460f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1461f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                                ArgumentIndex);
14621fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor}
146355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor
146455817afdf9d453a443262a733f6caf6692dca118Douglas Gregorvoid Parser::CodeCompleteNaturalLanguage() {
146555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  Actions.CodeCompleteNaturalLanguage();
146655817afdf9d453a443262a733f6caf6692dca118Douglas Gregor}
1467f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1468f986038beed360c031de8654cfba43a5d3184605Francois Pichetbool Parser::ParseMicrosoftIfExistsCondition(bool& Result) {
1469f986038beed360c031de8654cfba43a5d3184605Francois Pichet  assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
1470f986038beed360c031de8654cfba43a5d3184605Francois Pichet         "Expected '__if_exists' or '__if_not_exists'");
1471f986038beed360c031de8654cfba43a5d3184605Francois Pichet  Token Condition = Tok;
1472f986038beed360c031de8654cfba43a5d3184605Francois Pichet  SourceLocation IfExistsLoc = ConsumeToken();
1473f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1474f986038beed360c031de8654cfba43a5d3184605Francois Pichet  SourceLocation LParenLoc = Tok.getLocation();
1475f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (Tok.isNot(tok::l_paren)) {
1476f986038beed360c031de8654cfba43a5d3184605Francois Pichet    Diag(Tok, diag::err_expected_lparen_after) << IfExistsLoc;
1477f986038beed360c031de8654cfba43a5d3184605Francois Pichet    SkipUntil(tok::semi);
1478f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1479f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1480f986038beed360c031de8654cfba43a5d3184605Francois Pichet  ConsumeParen(); // eat the '('.
1481f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1482f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Parse nested-name-specifier.
1483f986038beed360c031de8654cfba43a5d3184605Francois Pichet  CXXScopeSpec SS;
1484f986038beed360c031de8654cfba43a5d3184605Francois Pichet  ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1485f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1486f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check nested-name specifier.
1487f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (SS.isInvalid()) {
1488f986038beed360c031de8654cfba43a5d3184605Francois Pichet    SkipUntil(tok::semi);
1489f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1490f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1491f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1492f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Parse the unqualified-id.
1493f986038beed360c031de8654cfba43a5d3184605Francois Pichet  UnqualifiedId Name;
1494f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
1495f986038beed360c031de8654cfba43a5d3184605Francois Pichet    SkipUntil(tok::semi);
1496f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1497f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1498f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1499f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1500f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1501f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1502f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check if the symbol exists.
1503f986038beed360c031de8654cfba43a5d3184605Francois Pichet  bool Exist = Actions.CheckMicrosoftIfExistsSymbol(SS, Name);
1504f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1505f986038beed360c031de8654cfba43a5d3184605Francois Pichet  Result = ((Condition.is(tok::kw___if_exists) && Exist) ||
1506f986038beed360c031de8654cfba43a5d3184605Francois Pichet            (Condition.is(tok::kw___if_not_exists) && !Exist));
1507f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1508f986038beed360c031de8654cfba43a5d3184605Francois Pichet  return false;
1509f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
1510f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1511563a645de82231a55e221fe655b7188bf8369662Francois Pichetvoid Parser::ParseMicrosoftIfExistsExternalDeclaration() {
1512f986038beed360c031de8654cfba43a5d3184605Francois Pichet  bool Result;
1513f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (ParseMicrosoftIfExistsCondition(Result))
1514f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1515f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1516f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (Tok.isNot(tok::l_brace)) {
1517f986038beed360c031de8654cfba43a5d3184605Francois Pichet    Diag(Tok, diag::err_expected_lbrace);
1518f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1519f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1520f986038beed360c031de8654cfba43a5d3184605Francois Pichet  ConsumeBrace();
1521f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1522f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Condition is false skip all inside the {}.
1523f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (!Result) {
1524f986038beed360c031de8654cfba43a5d3184605Francois Pichet    SkipUntil(tok::r_brace, false);
1525f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1526f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1527f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1528f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Condition is true, parse the declaration.
1529f986038beed360c031de8654cfba43a5d3184605Francois Pichet  while (Tok.isNot(tok::r_brace)) {
1530f986038beed360c031de8654cfba43a5d3184605Francois Pichet    ParsedAttributesWithRange attrs(AttrFactory);
1531f986038beed360c031de8654cfba43a5d3184605Francois Pichet    MaybeParseCXX0XAttributes(attrs);
1532f986038beed360c031de8654cfba43a5d3184605Francois Pichet    MaybeParseMicrosoftAttributes(attrs);
1533f986038beed360c031de8654cfba43a5d3184605Francois Pichet    DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
1534f986038beed360c031de8654cfba43a5d3184605Francois Pichet    if (Result && !getCurScope()->getParent())
1535f986038beed360c031de8654cfba43a5d3184605Francois Pichet      Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
1536f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1537f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1538f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (Tok.isNot(tok::r_brace)) {
1539f986038beed360c031de8654cfba43a5d3184605Francois Pichet    Diag(Tok, diag::err_expected_rbrace);
1540f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1541f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1542f986038beed360c031de8654cfba43a5d3184605Francois Pichet  ConsumeBrace();
1543f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
1544