Parser.cpp revision d6ca8da0f5a4115813055729faaa5128e994806d
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"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/DeclSpec.h"
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Scope.h"
18314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor#include "clang/Parse/Template.h"
190102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner#include "llvm/Support/raw_ostream.h"
20d167ca0d26e43292b8b9e8d5300d92784ae0e27dChris Lattner#include "RAIIObjectsForParser.h"
21fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar#include "ParsePragma.h"
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerParser::Parser(Preprocessor &pp, Action &actions)
251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
2608d92ecf6e5b1fd23177a08c2312b58d63d863dbChris Lattner    GreaterThanIsOperator(true), ColonIsSacred(false),
2708d92ecf6e5b1fd23177a08c2312b58d63d863dbChris Lattner    TemplateParameterDepth(0) {
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Tok.setKind(tok::eof);
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CurScope = 0;
309e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  NumCachedScopes = 0;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ParenCount = BracketCount = BraceCount = 0;
32b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  ObjCImpDecl = DeclPtrTy();
33fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar
34fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // Add #pragma handlers. These are removed and destroyed in the
35fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // destructor.
364726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PackHandler.reset(new
374726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek          PragmaPackHandler(&PP.getIdentifierTable().get("pack"), actions));
384726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PP.AddPragmaHandler(0, PackHandler.get());
391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  UnusedHandler.reset(new
414726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek          PragmaUnusedHandler(&PP.getIdentifierTable().get("unused"), actions,
424726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek                              *this));
434726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PP.AddPragmaHandler(0, UnusedHandler.get());
449991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman
459991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman  WeakHandler.reset(new
469991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman          PragmaWeakHandler(&PP.getIdentifierTable().get("weak"), actions));
479991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman  PP.AddPragmaHandler(0, WeakHandler.get());
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
500102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner/// If a crash happens while the parser is active, print out a line indicating
510102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner/// what the current token is.
520102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattnervoid PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const {
530102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  const Token &Tok = P.getCurToken();
54ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner  if (Tok.is(tok::eof)) {
550102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner    OS << "<eof> parser at end of file\n";
560102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner    return;
570102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  }
581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner  if (Tok.getLocation().isInvalid()) {
60ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner    OS << "<unknown> parser at unknown location\n";
61ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner    return;
62ddcbc0a72a04a5ae2493088f1437200a9ea480b1Chris Lattner  }
631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
640102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  const Preprocessor &PP = P.getPreprocessor();
650102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  Tok.getLocation().print(OS, PP.getSourceManager());
669fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar  if (Tok.isAnnotation())
679fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar    OS << ": at annotation token \n";
689fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar  else
699fa31dd71e0414e2ca905380e2f8d42710b22c82Daniel Dunbar    OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
70f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor}
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
720102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner
733cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
740102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner  return Diags.Report(FullSourceLoc(Loc, PP.getSourceManager()), DiagID);
751ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner}
761ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner
773cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
781ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  return Diag(Tok.getLocation(), DiagID);
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
814b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \brief Emits a diagnostic suggesting parentheses surrounding a
824b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// given range.
834b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor///
844b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param Loc The location where we'll emit the diagnostic.
854b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param Loc The kind of diagnostic to emit.
864b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param ParenRange Source range enclosing code that should be parenthesized.
874b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorvoid Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
884b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor                                SourceRange ParenRange) {
89b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
90b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
914b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // We can't display the parentheses, so just dig the
924b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // warning/error and return.
934b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Loc, DK);
944b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    return;
954b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Diag(Loc, DK)
98849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
99849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(EndLoc, ")");
1004b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor}
1014b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// this helper function matches and consumes the specified RHS token if
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// present.  If not present, it emits the specified diagnostic indicating
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// that the parser failed to match the RHS of the token at LHSLoc.  LHSName
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// should be the name of the unmatched LHS token.
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                           SourceLocation LHSLoc) {
109a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
110000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(RHSTok))
1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return ConsumeAnyToken();
112a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation R = Tok.getLocation();
1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *LHSName = "unknown";
1155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  diag::kind DID = diag::err_parse_error;
1165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (RHSTok) {
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: break;
1185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::greater:  LHSName = "<"; DID = diag::err_expected_greater; break;
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Diag(Tok, DID);
12428eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner  Diag(LHSLoc, diag::note_matching) << LHSName;
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SkipUntil(RHSTok);
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return R;
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// input.  If so, it is consumed and false is returned.
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If the input is malformed, this emits the specified diagnostic.  Next, if
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipToTok is specified, it calls SkipUntil(SkipToTok).  Finally, true is
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// returned.
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const char *Msg, tok::TokenKind SkipToTok) {
137000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(ExpectedTok)) {
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeAnyToken();
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
141a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1424b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  const char *Spelling = 0;
143b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
1441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (EndLoc.isValid() &&
145b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor      (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
1464b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // Show what code to insert to fix this problem.
1471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(EndLoc, DiagID)
1484b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor      << Msg
149849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateInsertion(EndLoc, Spelling);
1504b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  } else
1514b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Tok, DiagID) << Msg;
1524b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (SkipToTok != tok::unknown)
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(SkipToTok);
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Error recovery.
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipUntil - Read tokens until we get to the specified token, then consume
163012cf464254804279efa84e21b4b493dde76c5f1Chris Lattner/// it (unless DontConsume is true).  Because we cannot guarantee that the
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// token will ever occur, this skips to the next token, or to some likely
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// good stopping point.  If StopAtSemi is true, skipping will stop at a ';'
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character.
167a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If SkipUntil finds the specified token, it returns true, otherwise it
169a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// returns false.
1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                       bool StopAtSemi, bool DontConsume) {
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We always want this function to skip at least one token if the first token
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // isn't T and if not at EOF.
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isFirstTokenSkipped = true;
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we found one of the tokens, stop and return true.
1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumToks; ++i) {
178000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(Toks[i])) {
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (DontConsume) {
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Noop, don't consume the token.
1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        } else {
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ConsumeAnyToken();
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return true;
1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
187a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Tok.getKind()) {
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::eof:
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ran out of tokens.
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
192a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_paren:
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested parens.
1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::r_paren, false);
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_square:
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested square brackets.
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::r_square, false);
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_brace:
2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested braces.
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::r_brace, false);
2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
208a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Since the user wasn't looking for this token (if they were, it would
2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // already be handled), this isn't balanced.  If there is a LHS token at a
2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // higher level, we will assume that this matches the unbalanced token
2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // and return it.  Otherwise, this is a spurious RHS token, which we skip.
2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_paren:
2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ParenCount && !isFirstTokenSkipped)
2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_square:
2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BracketCount && !isFirstTokenSkipped)
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_brace:
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BraceCount && !isFirstTokenSkipped)
2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
2275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
229a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::string_literal:
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::wide_string_literal:
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeStringToken();
2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::semi:
2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (StopAtSemi)
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
2375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
2385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip this token.
2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    isFirstTokenSkipped = false;
244a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump  }
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Scope manipulation
2495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EnterScope - Start a new scope.
2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::EnterScope(unsigned ScopeFlags) {
2539e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes) {
2549e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    Scope *N = ScopeCache[--NumCachedScopes];
2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    N->Init(CurScope, ScopeFlags);
2565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurScope = N;
2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurScope = new Scope(CurScope, ScopeFlags);
2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
260d6d4fcf7fd2cdb8c75e6feb5ab1a7132784e96e1Douglas Gregor  CurScope->setNumErrorsAtStart(Diags.getNumErrors());
2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExitScope - Pop a scope off the scope stack.
2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ExitScope() {
2655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurScope && "Scope imbalance!");
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26790ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // Inform the actions module that this scope is going away if there are any
26890ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // decls in it.
26990ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  if (!CurScope->decl_empty())
270b216c8861c60af9c56c900a485233255c4452df2Steve Naroff    Actions.ActOnPopScope(Tok.getLocation(), CurScope);
271a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2729e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  Scope *OldScope = CurScope;
2739e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  CurScope = OldScope->getParent();
274a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2759e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes == ScopeCacheSize)
2769e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete OldScope;
2775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
2789e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    ScopeCache[NumCachedScopes++] = OldScope;
2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// C99 6.9: External Definitions.
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerParser::~Parser() {
2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we still have scopes active, delete the scope tree.
2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  delete CurScope;
291a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Free the scope cache.
2939e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
2949e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete ScopeCache[i];
295fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar
296fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // Remove the pragma handlers we installed.
2974726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PP.RemovePragmaHandler(0, PackHandler.get());
2984726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PackHandler.reset();
2994726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PP.RemovePragmaHandler(0, UnusedHandler.get());
3004726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  UnusedHandler.reset();
3019991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman  PP.RemovePragmaHandler(0, WeakHandler.get());
3029991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman  WeakHandler.reset();
3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Initialize - Warm up the parser.
3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::Initialize() {
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Prime the lexer look-ahead.
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken();
310a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
31131e057270232c1c37602579cb6461c2704175672Chris Lattner  // Create the translation unit scope.  Install it as the current scope.
3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurScope == 0 && "A scope is already active?");
31331e057270232c1c37602579cb6461c2704175672Chris Lattner  EnterScope(Scope::DeclScope);
314b216c8861c60af9c56c900a485233255c4452df2Steve Naroff  Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
315a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
316000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(tok::eof) &&
317f72617544287acf0281c3b1a733bcb22a02e6ca4Chris Lattner      !getLang().CPlusPlus)  // Empty source file is an extension in C
3185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::ext_empty_source_file);
319a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
32034870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  // Initialization for Objective-C context sensitive keywords recognition.
321a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  // Referenced in Parser::ParseObjCTypeQualifierList.
32234870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  if (getLang().ObjC1) {
323a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
324a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
325a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
326a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
327a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
328a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
32934870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  }
330662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar
331662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar  Ident_super = &PP.getIdentifierTable().get("super");
33282287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson
33382287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  if (getLang().AltiVec) {
33482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_vector = &PP.getIdentifierTable().get("vector");
33582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_pixel = &PP.getIdentifierTable().get("pixel");
33682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  }
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// action tells us to.  This returns true if the EOF was encountered.
341682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattnerbool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
342682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  Result = DeclGroupPtrTy();
3439299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  if (Tok.is(tok::eof)) {
3449299f3fa85796613cc787a2062c9562d07c8613eChris Lattner    Actions.ActOnEndOfTranslationUnit();
3459299f3fa85796613cc787a2062c9562d07c8613eChris Lattner    return true;
3469299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  }
347a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
348bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  CXX0XAttributeList Attr;
349bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
350bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    Attr = ParseCXX0XAttributes();
351bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  Result = ParseExternalDeclaration(Attr);
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTranslationUnit:
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       translation-unit: [C99 6.9]
357a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///         external-declaration
358a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///         translation-unit external-declaration
3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseTranslationUnit() {
3608935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  Initialize();
361a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
362682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  DeclGroupPtrTy Res;
36389307ffaf8acf4d6fdffd72b607ca4fbcfdffc9dSteve Naroff  while (!ParseTopLevelDecl(Res))
3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /*parse them all*/;
3651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36606f548596beef4c0a227a45cba996497f99566c0Chris Lattner  ExitScope();
36706f548596beef4c0a227a45cba996497f99566c0Chris Lattner  assert(CurScope == 0 && "Scope imbalance!");
3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseExternalDeclaration:
37190b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner///
372c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
373c3018153a11afe91849748a93d920040a571b76cChris Lattner///         function-definition
374c3018153a11afe91849748a93d920040a571b76cChris Lattner///         declaration
375a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration
3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU]   asm-definition
377c3018153a11afe91849748a93d920040a571b76cChris Lattner/// [GNU]   __extension__ external-declaration
3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-declaration
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-alias-declaration
3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-protocol-definition
3825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-method-definition
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  @end
384c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor/// [C++]   linkage-specification
3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-definition:
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         simple-asm-expr ';'
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
388a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration:
389a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///           ';'
390a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///
39145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor/// [C++0x/GNU] 'extern' 'template' declaration
392bbd37c62e34db3f5a95c899723484a76c71d7757Sean HuntParser::DeclGroupPtrTy Parser::ParseExternalDeclaration(CXX0XAttributeList Attr) {
393682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  DeclPtrTy SingleDecl;
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Tok.getKind()) {
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::semi:
396a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor    if (!getLang().CPlusPlus0x)
397a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor      Diag(Tok, diag::ext_top_level_semi)
398849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
3991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
4015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TODO: Invoke action for top-level semicolon.
402682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
40390b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::r_brace:
40490b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    Diag(Tok, diag::err_expected_external_declaration);
40590b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    ConsumeBrace();
406682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
40790b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::eof:
40890b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    Diag(Tok, diag::err_expected_external_declaration);
409682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
410c3018153a11afe91849748a93d920040a571b76cChris Lattner  case tok::kw___extension__: {
411c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    // __extension__ silences extension warnings in the subexpression.
412c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
41339146d6497ad5e7ca8ef639221e7b3e15d07c888Chris Lattner    ConsumeToken();
414bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    return ParseExternalDeclaration(Attr);
415c3018153a11afe91849748a93d920040a571b76cChris Lattner  }
416dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  case tok::kw_asm: {
417bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    if (Attr.HasAttr)
418bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
419bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt        << Attr.Range;
420bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
421effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    OwningExprResult Result(ParseSimpleAsm());
422a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
4233f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson    ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
4243f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson                     "top-level asm block");
425dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson
426682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    if (Result.isInvalid())
427682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
428682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), move(Result));
429682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
430dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  }
4315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::at:
432682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    // @ is not a legal token unless objc is enabled, no need to check for ObjC.
433682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    /// FIXME: ParseObjCAtDirectives should return a DeclGroup for things like
434682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    /// @class foo, bar;
435682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = ParseObjCAtDirectives();
436682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::minus:
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::plus:
439682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    if (!getLang().ObjC1) {
440682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      Diag(Tok, diag::err_expected_external_declaration);
441682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      ConsumeToken();
442682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
443682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    }
444682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = ParseObjCMethodDefinition();
445682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
446791215b7a24666912c0b71175d2ca5ba082f666eDouglas Gregor  case tok::code_completion:
447b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor      Actions.CodeCompleteOrdinaryName(CurScope,
448b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor                                   ObjCImpDecl? Action::CCC_ObjCImplementation
449b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor                                              : Action::CCC_Namespace);
450791215b7a24666912c0b71175d2ca5ba082f666eDouglas Gregor    ConsumeToken();
451bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    return ParseExternalDeclaration(Attr);
452f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  case tok::kw_using:
4538f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  case tok::kw_namespace:
4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_typedef:
455adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_template:
456adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_export:    // As in 'export template'
457511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  case tok::kw_static_assert:
458bae35118fc5cea2da08567dbb9763af7f906dae2Chris Lattner    // A function definition cannot start with a these keywords.
45997144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    {
46097144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner      SourceLocation DeclEnd;
461bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      return ParseDeclaration(Declarator::FileContext, DeclEnd, Attr);
46297144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    }
46345f965581935791a018df829a14dff53c1dd8f47Douglas Gregor  case tok::kw_extern:
46445f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
46545f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      // Extern templates
46645f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation ExternLoc = ConsumeToken();
46745f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation TemplateLoc = ConsumeToken();
46845f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation DeclEnd;
46945f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      return Actions.ConvertDeclToDeclGroup(
47045f965581935791a018df829a14dff53c1dd8f47Douglas Gregor                  ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd));
47145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    }
4721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47345f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    // FIXME: Detect C++ linkage specifications here?
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47545f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    // Fall through to handle other declarations or function definitions.
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // We can't tell whether this is a function-definition or declaration yet.
479bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    return ParseDeclarationOrFunctionDefinition(Attr.AttrList);
4805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
482682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // This routine returns a DeclGroup, if the thing we parsed only contains a
483682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // single decl, convert it now.
484682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  return Actions.ConvertDeclToDeclGroup(SingleDecl);
4855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4871426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
4881426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, continues a declaration or declaration list.
4891426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregorbool Parser::isDeclarationAfterDeclarator() {
4901426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor  return Tok.is(tok::equal) ||      // int X()=  -> not a function def
4911426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::comma) ||           // int X(),  -> not a function def
4921426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::semi)  ||           // int X();  -> not a function def
4931426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
4941426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
4951426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    (getLang().CPlusPlus &&
4961426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor     Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
4971426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
4981426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
4991426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
5001426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, indicates the start of a function definition.
5011426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregorbool Parser::isStartOfFunctionDefinition() {
5025d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  if (Tok.is(tok::l_brace))   // int X() {}
5035d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner    return true;
5045d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner
5055d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  if (!getLang().CPlusPlus)
5065d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner    return isDeclarationSpecifier();   // int X(f) int f; {}
5075d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
5085d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner         Tok.is(tok::kw_try);          // X() try { ... }
5091426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
5101426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// a declaration.  We can't tell which we have until we read up to the
513c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// compound-statement in function-definition. TemplateParams, if
514c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// non-NULL, provides the template parameters when we're parsing a
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ template-declaration.
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       function-definition: [C99 6.9.1]
518a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
519a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
520a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
521a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///
5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       declaration: [C99 6.7]
523697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner///         declaration-specifiers init-declarator-list[opt] ';'
524697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner/// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OMP]   threadprivate-directive                              [TODO]
5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
527682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerParser::DeclGroupPtrTy
5283acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
5293acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian                                             AttributeList *Attr,
530bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt                                             AccessSpecifier AS) {
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Parse the common declaration-specifiers piece.
532bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  if (Attr)
533bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    DS.AddAttributes(Attr);
534bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
5350efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
536a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
5375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // declaration-specifiers init-declarator-list[opt] ';'
539000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(tok::semi)) {
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
541682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
54254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.complete(TheDecl);
543682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
545a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
546246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // ObjC2 allows prefix attributes on class interfaces and protocols.
547246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // FIXME: This still needs better diagnostics. We should only accept
548246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // attributes here, no types, etc.
549000732226610650837478cba97843d19b75f648eChris Lattner  if (getLang().ObjC2 && Tok.is(tok::at)) {
550dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation AtLoc = ConsumeToken(); // the "@"
5511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
552246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar        !Tok.isObjCAtKeyword(tok::objc_protocol)) {
553246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar      Diag(Tok, diag::err_objc_unexpected_attr);
554cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      SkipUntil(tok::semi); // FIXME: better skip?
555682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
556cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    }
557d8ac05753dc4506224d445ff98399c01da3136e5John McCall
55854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.abort();
55954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
5600de2ae28c603322f05e2d9200c7d457c8b928983Fariborz Jahanian    const char *PrevSpec = 0;
561fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
562fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
563fec54013fcd0eb72642741584ca04c1bc292bef8John McCall      Diag(AtLoc, DiagID) << PrevSpec;
5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
565682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    DeclPtrTy TheDecl;
566246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar    if (Tok.isObjCAtKeyword(tok::objc_protocol))
567682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
568682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    else
569682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
570682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
571dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
572a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
573c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // If the declspec consisted only of 'extern' and we have a string
574c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // literal following it, this must be a C++ linkage specifier like
575c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // 'extern "C"'.
5763c6f6a7a1bb9969112617a26d2333bab2f6efd65Chris Lattner  if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
577c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner      DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
578682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
5793acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian    DeclPtrTy TheDecl = ParseLinkage(DS, Declarator::FileContext);
580682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
581682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  }
582c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
583d8ac05753dc4506224d445ff98399c01da3136e5John McCall  return ParseDeclGroup(DS, Declarator::FileContext, true);
5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5863acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::DeclGroupPtrTy
5873acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::ParseDeclarationOrFunctionDefinition(AttributeList *Attr,
5883acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian                                             AccessSpecifier AS) {
5893acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian  ParsingDeclSpec DS(*this);
5903acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian  return ParseDeclarationOrFunctionDefinition(DS, Attr, AS);
5913acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian}
5923acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian
5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseFunctionDefinition - We parsed and verified that the specified
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Declarator is well formed.  If this is a K&R-style function, read the
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// parameters declaration-list, then start the compound-statement.
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
597a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///       function-definition: [C99 6.9.1]
598a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
599a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
600a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
6017ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
60223c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
60323c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         function-body
6047ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
605d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl///         decl-specifier-seq[opt] declarator function-try-block
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
60754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCallParser::DeclPtrTy Parser::ParseFunctionDefinition(ParsingDeclarator &D,
60852591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                                     const ParsedTemplateInfo &TemplateInfo) {
6095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
6105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
6115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "This isn't a function declarator!");
6125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
613a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
614a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // If this is C90 and the declspecs were completely missing, fudge in an
615a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // implicit int.  We do this here because this is the only place where
616a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // declaration-specifiers are completely optional in the grammar.
6172a327d11a07dfbdf20910cebbae38910eda111fdChris Lattner  if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) {
618a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner    const char *PrevSpec;
619fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
62031c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner    D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
62131c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner                                           D.getIdentifierLoc(),
622fec54013fcd0eb72642741584ca04c1bc292bef8John McCall                                           PrevSpec, DiagID);
623ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
624a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  }
625a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
6265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this declaration was formed with a K&R-style identifier list for the
6275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // arguments, parse declarations for all of the args next.
6285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // int foo(a,b) int a; float b; {}
6295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!FTI.hasPrototype && FTI.NumArgs != 0)
6305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseKNRParamDeclarations(D);
6315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6327ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // We should have either an opening brace or, in a C++ constructor,
6337ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // we may have a colon.
634d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (Tok.isNot(tok::l_brace) && Tok.isNot(tok::colon) &&
635d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl      Tok.isNot(tok::kw_try)) {
6365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::err_expected_fn_body);
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
6395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(tok::l_brace, true, true);
640a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we didn't find the '{', bail out.
642000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.isNot(tok::l_brace))
643b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
6445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
645a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
646b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Enter a scope for the function body.
6478935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
648a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
649b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Tell the actions module that we have entered a function definition with the
650b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // specified Declarator for the function.
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DeclPtrTy Res = TemplateInfo.TemplateParams?
65252591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor      Actions.ActOnStartOfFunctionTemplateDef(CurScope,
65352591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                              Action::MultiTemplateParamsArg(Actions,
65452591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                                          TemplateInfo.TemplateParams->data(),
65552591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                                         TemplateInfo.TemplateParams->size()),
65652591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor                                              D)
65752591bf224b2c43e2b00e265bb8599a620081925Douglas Gregor    : Actions.ActOnStartOfFunctionDef(CurScope, D);
658a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
65954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclarator context before we parse the body.
66054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.complete(Res);
66154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
66254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclSpec context, too.  This const_cast is
66354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // safe because we're always the sole owner.
66454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.getMutableDeclSpec().abort();
66554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
666d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (Tok.is(tok::kw_try))
667d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl    return ParseFunctionTryBlock(Res);
668d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
6697ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // If we have a colon, then we're probably parsing a C++
6707ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // ctor-initializer.
671d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  if (Tok.is(tok::colon)) {
6727ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    ParseConstructorInitializer(Res);
673d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall
674d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    // Recover from error.
675d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    if (!Tok.is(tok::l_brace)) {
676d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall      Actions.ActOnFinishFunctionBody(Res, Action::StmtArg(Actions));
677d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall      return Res;
678d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    }
679d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  } else
680393612e6c7727f1fee50039254d9f434364cc0b2Fariborz Jahanian    Actions.ActOnDefaultCtorInitializers(Res);
6817ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
68240e9bc84a2ab49fc33c2b1a95c6674ab2b820e9eChris Lattner  return ParseFunctionStatementBody(Res);
6835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
6865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// types for a function with a K&R-style identifier list for arguments.
6875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseKNRParamDeclarations(Declarator &D) {
6885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We know that the top-level of this declarator is a function.
6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
6905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
69104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // Enter function-declaration scope, limiting any declarators to the
69204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // function prototype scope, including parameter declarators.
6933218c4bb3b5d7250f12420de6db7ef3e3f805a75Douglas Gregor  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
69404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner
6955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read all the argument declarations.
6965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isDeclarationSpecifier()) {
6975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SourceLocation DSStart = Tok.getLocation();
698a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
6995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the common declaration-specifiers piece.
7005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    DeclSpec DS;
7015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarationSpecifiers(DS);
702a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: 'each declaration in the declaration list shall have at
7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // least one declarator'.
7055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // the declarations though.  It's trivial to ignore them, really hard to do
7075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // anything else with them.
708000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.is(tok::semi)) {
7095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DSStart, diag::err_declaration_does_not_declare_param);
7105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
7115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
7125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
713a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
7145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
7155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // than register.
7165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
7175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        DS.getStorageClassSpec() != DeclSpec::SCS_register) {
7185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getStorageClassSpecLoc(),
7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
7205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.isThreadSpecified()) {
7235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getThreadSpecLoc(),
7245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
7255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
7265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
727a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the first declarator attached to this declspec.
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
7305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarator(ParmDeclarator);
7315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Handle the full declarator list.
7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (1) {
7345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If attributes are present, parse them.
73533efe2f088ae40b5129fe4ab021912e80129e155Richard Pennington      if (Tok.is(tok::kw___attribute)) {
73633efe2f088ae40b5129fe4ab021912e80129e155Richard Pennington        SourceLocation Loc;
73733efe2f088ae40b5129fe4ab021912e80129e155Richard Pennington        AttributeList *AttrList = ParseGNUAttributes(&Loc);
73833efe2f088ae40b5129fe4ab021912e80129e155Richard Pennington        ParmDeclarator.AddAttributes(AttrList, Loc);
73933efe2f088ae40b5129fe4ab021912e80129e155Richard Pennington      }
740a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
7415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ask the actions module to compute the type for this declarator.
742b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Action::DeclPtrTy Param =
74304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner        Actions.ActOnParamDeclarator(CurScope, ParmDeclarator);
7442bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff
745a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump      if (Param &&
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // A missing identifier has already been diagnosed.
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ParmDeclarator.getIdentifier()) {
7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Scan the argument list looking for the correct param to apply this
7505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // type.
7515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0; ; ++i) {
7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // C99 6.9.1p6: those declarators shall declare only identifiers from
7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // the identifier list.
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (i == FTI.NumArgs) {
7551ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner            Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
7566898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner              << ParmDeclarator.getIdentifier();
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
759a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Reject redefinitions of parameters.
76204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner            if (FTI.ArgInfo[i].Param) {
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(ParmDeclarator.getIdentifierLoc(),
7641ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner                   diag::err_param_redefinition)
7656898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner                 << ParmDeclarator.getIdentifier();
7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            } else {
76704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner              FTI.ArgInfo[i].Param = Param;
7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
7695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
7705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
7715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
7725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we don't have a comma, it is either the end of the list (a ';') or
7755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // an error, bail out.
776000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.isNot(tok::comma))
7775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
778a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
7795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Consume the comma.
7805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
781a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
7825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Parse the next declarator.
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParmDeclarator.clear();
7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParseDeclarator(ParmDeclarator);
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
786a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
787000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.is(tok::semi)) {
7885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
7895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
7905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_parse_error);
7915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip to end of block or statement
7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi, true);
793000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(tok::semi))
7945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ConsumeToken();
7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
797a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
7985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The actions module must verify that all arguments were declared.
799a3a835149ed4b183e3b009a1f94a6123779d696bDouglas Gregor  Actions.ActOnFinishKNRParamDeclarations(CurScope, D, Tok.getLocation());
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
8045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// allowed to be a wide string, and is not subject to character translation.
8055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
8065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-string-literal:
8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         string-literal
8085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
809effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian RedlParser::OwningExprResult Parser::ParseAsmStringLiteral() {
8105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!isTokenStringLiteral()) {
8115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::err_expected_string_literal);
81261364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl    return ExprError();
8135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
814a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
81520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  OwningExprResult Res(ParseStringLiteralExpression());
816effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  if (Res.isInvalid()) return move(Res);
817a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // TODO: Diagnose: wide string literal in 'asm'
819a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
820effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  return move(Res);
8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseSimpleAsm
8245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
8255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] simple-asm-expr:
8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'asm' '(' asm-string-literal ')'
8275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
828ab197baec16bacade82325fb274cf6b992ac5d8aSebastian RedlParser::OwningExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
829000732226610650837478cba97843d19b75f648eChris Lattner  assert(Tok.is(tok::kw_asm) && "Not an asm!");
830dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  SourceLocation Loc = ConsumeToken();
831a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8327a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  if (Tok.is(tok::kw_volatile)) {
833841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    // Remove from the end of 'asm' to the end of 'volatile'.
834841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
835841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall                             PP.getLocForEndOfToken(Tok.getLocation()));
836841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall
837841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    Diag(Tok, diag::warn_file_asm_volatile)
838849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateRemoval(RemovalRange);
8397a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall    ConsumeToken();
8407a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  }
8417a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall
842000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.isNot(tok::l_paren)) {
8431ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "asm";
84461364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl    return ExprError();
8455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
846a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
847ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  Loc = ConsumeParen();
848a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
849effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  OwningExprResult Result(ParseAsmStringLiteral());
850a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
851ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Result.isInvalid()) {
852ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SkipUntil(tok::r_paren, true, true);
853ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
854ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Tok.getLocation();
855ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    ConsumeAnyToken();
856ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  } else {
857ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    Loc = MatchRHSPunctuation(tok::r_paren, Loc);
858ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
859ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Loc;
860ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
861a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
862effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  return move(Result);
8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
865eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateTypeOrScopeToken - If the current token position is on a
866eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typename (possibly qualified in C++) or a C++ scope specifier not followed
867eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
868eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// with a single annotation token representing the typename or C++ scope
869eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// respectively.
870eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This simplifies handling of C++ scope specifiers and allows efficient
871eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// backtracking without the need to re-parse and resolve nested-names and
872eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typenames.
87344802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// It will mainly be called when we expect to treat identifiers as typenames
87444802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// (if they are typenames). For example, in C we do not expect identifiers
87544802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// inside expressions to be treated as typenames so it will not be called
87644802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// for expressions in C.
87744802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// The benefit for C/ObjC is that a typename will be annotated and
878b43a50ff1b0b171ece84425b0ad83a9a31f038faSteve Naroff/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
87944802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// will not be called twice, once to check whether we have a declaration
88044802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// specifier, and another one to get the actual type inside
88144802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// ParseDeclarationSpecifiers).
882a7bc7c880f86bc180684ef032d06df51bcae7a23Chris Lattner///
8839ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// This returns true if an error occurred.
8841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
88555a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
88655a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
887495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext) {
8881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
889ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall          || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) &&
8907452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Cannot be a type or scope token!");
8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
892d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Tok.is(tok::kw_typename)) {
893d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Parse a C++ typename-specifier, e.g., "typename T::type".
894d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //
895d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //   typename-specifier:
896d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //     'typename' '::' [opt] nested-name-specifier identifier
8971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     'typename' '::' [opt] nested-name-specifier template [opt]
8981734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    //            simple-template-id
899d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    SourceLocation TypenameLoc = ConsumeToken();
900d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    CXXScopeSpec SS;
9019ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false))
9029ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
9039ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    if (!SS.isSet()) {
904d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
9059ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
906d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    }
907d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
908d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    TypeResult Ty;
909d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (Tok.is(tok::identifier)) {
910d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      // FIXME: check whether the next token is '<', first!
9111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Ty = Actions.ActOnTypenameType(TypenameLoc, SS, *Tok.getIdentifierInfo(),
912d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     Tok.getLocation());
9131734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else if (Tok.is(tok::annot_template_id)) {
9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateIdAnnotation *TemplateId
9151734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
9161734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      if (TemplateId->Kind == TNK_Function_template) {
9171734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        Diag(Tok, diag::err_typename_refers_to_non_type_template)
9181734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor          << Tok.getAnnotationRange();
9199ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
9201734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      }
921d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
92231a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor      AnnotateTemplateIdTokenAsType(0);
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(Tok.is(tok::annot_typename) &&
9241734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor             "AnnotateTemplateIdTokenAsType isn't working properly");
92531a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor      if (Tok.getAnnotationValue())
92631a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor        Ty = Actions.ActOnTypenameType(TypenameLoc, SS, SourceLocation(),
92731a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor                                       Tok.getAnnotationValue());
92831a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor      else
92931a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor        Ty = true;
9301734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else {
9311734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Diag(Tok, diag::err_expected_type_name_after_typename)
9321734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        << SS.getRange();
9339ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
9341734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
9351734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
93639d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    SourceLocation EndLoc = Tok.getLastLoc();
9371734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setKind(tok::annot_typename);
93831a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor    Tok.setAnnotationValue(Ty.isInvalid()? 0 : Ty.get());
93939d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    Tok.setAnnotationEndLoc(EndLoc);
9401734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setLocation(TypenameLoc);
9411734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    PP.AnnotateCachedTokens(Tok);
9429ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
943d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
944d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
945ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // Remembers whether the token was originally a scope annotation.
946ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
947ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall
948eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
9494bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  if (getLang().CPlusPlus)
9509ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, EnteringContext))
9519ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
952eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
953eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::identifier)) {
954608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner    // Determine whether the identifier is a type name.
9551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (TypeTy *Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
956b696ea3a0693798daeafd896d77f0b8f1fec3cc5Douglas Gregor                                         Tok.getLocation(), CurScope, &SS)) {
957608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // This is a typename. Replace the current token in-place with an
958608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // annotation type token.
959b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner      Tok.setKind(tok::annot_typename);
960608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      Tok.setAnnotationValue(Ty);
961608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      Tok.setAnnotationEndLoc(Tok.getLocation());
962608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      if (SS.isNotEmpty()) // it was a C++ qualified type name.
963608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner        Tok.setLocation(SS.getBeginLoc());
9641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
965608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // In case the tokens were cached, have Preprocessor replace
966608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them with the annotation token.
967608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      PP.AnnotateCachedTokens(Tok);
9689ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
9691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
97039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
97139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (!getLang().CPlusPlus) {
972608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // If we're in C, we can't have :: tokens at all (the lexer won't return
973608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them).  If the identifier is not a type, then it can't be scope either,
9741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // just early exit.
975608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      return false;
976eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    }
9771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
97839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // If this is a template-id, annotate with a template-id or type token.
97955f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    if (NextToken().is(tok::less)) {
9807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor      TemplateTy Template;
981014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
982014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
9831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateNameKind TNK
984014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor            = Actions.isTemplateName(CurScope, SS, TemplateName,
9851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     /*ObjectType=*/0, EnteringContext,
986ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                     Template)) {
987ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
988ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
989ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName)) {
990c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // If an unrecoverable error occurred, we need to return true here,
991c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // because the token stream is in a damaged state.  We may not return
992c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // a valid identifier.
9939ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
994c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner        }
995ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
99655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    }
997d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
99839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // The current token, which is either an identifier or a
99939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // template-id, is not part of the annotation. Fall through to
100039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // push that token back into the stream and complete the C++ scope
100139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // specifier annotation.
10021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1003eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
100439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  if (Tok.is(tok::annot_template_id)) {
10051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    TemplateIdAnnotation *TemplateId
100639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1007c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor    if (TemplateId->Kind == TNK_Type_template) {
100839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // A template-id that refers to a type was parsed into a
100939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // template-id annotation in a context where we weren't allowed
101039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // to produce a type annotation token. Update the template-id
101139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // annotation token to a type annotation token now.
101231a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor      AnnotateTemplateIdTokenAsType(&SS);
10139ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
101439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
101539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
1016d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
10176ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (SS.isEmpty())
10189ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
10191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10206ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // A C++ scope specifier that isn't followed by a typename.
10216ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // Push the current token back into the token stream (or revert it if it is
10226ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // cached) and use an annotation scope token for current token.
10236ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (PP.isBacktrackEnabled())
10246ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.RevertCachedTokens(1);
10256ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  else
10266ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.EnterToken(Tok);
10276ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setKind(tok::annot_cxxscope);
10283507369940bfb269551bfa1fec812481f60e3552Douglas Gregor  Tok.setAnnotationValue(SS.getScopeRep());
10296ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setAnnotationRange(SS.getRange());
10306ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner
1031ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // In case the tokens were cached, have Preprocessor replace them
1032ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // with the annotation token.  We don't need to do this if we've
1033ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // just reverted back to the state we were in before being called.
1034ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  if (!wasScopeAnnotation)
1035ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall    PP.AnnotateCachedTokens(Tok);
10369ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1037eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
1038eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
1039eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
104039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor/// annotates C++ scope specifiers and template-ids.  This returns
1041c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner/// true if the token was annotated or there was an error that could not be
1042c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner/// recovered from.
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
104455a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
104555a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1046495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
10474bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  assert(getLang().CPlusPlus &&
10486ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner         "Call sites of this function should be guarded by checking for C++");
10497452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
10507452c6fc567ea1799f617395d0fa4c7ed075e5d9Chris Lattner         "Cannot be a type or scope token!");
1051eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
10524bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  CXXScopeSpec SS;
10539ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, EnteringContext))
10549ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return true;
1055edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin  if (SS.isEmpty())
10569ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1057eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
10586ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // Push the current token back into the token stream (or revert it if it is
10596ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // cached) and use an annotation scope token for current token.
10606ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (PP.isBacktrackEnabled())
10616ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.RevertCachedTokens(1);
10626ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  else
10636ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner    PP.EnterToken(Tok);
10646ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setKind(tok::annot_cxxscope);
10653507369940bfb269551bfa1fec812481f60e3552Douglas Gregor  Tok.setAnnotationValue(SS.getScopeRep());
10666ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  Tok.setAnnotationRange(SS.getRange());
10676ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner
10686ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // In case the tokens were cached, have Preprocessor replace them with the
10696ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // annotation token.
10706ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  PP.AnnotateCachedTokens(Tok);
10719ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1072eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
10736c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall
10746c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// Anchor the Parser::FieldCallback vtable to this translation unit.
10756c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// We use a spurious method instead of the destructor because
10766c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// destroying FieldCallbacks can actually be slightly
10776c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// performance-sensitive.
10786c94a6d77f456f23ecd4c2061e6413786b5e6571John McCallvoid Parser::FieldCallback::_anchor() {
10796c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall}
1080