Parser.cpp revision c640058aa7f224a71ce3b1d2601d84e1b57f82d3
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"
1555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "ParsePragma.h"
1655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "RAIIObjectsForParser.h"
1755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/ASTConsumer.h"
1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/DeclTemplate.h"
19500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
2019510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
2119510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h"
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Scope.h"
230102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner#include "llvm/Support/raw_ostream.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S
2769b5e952c56f95673064ad1815a240e0fb595865Benjamin Kramernamespace {
28aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief A comment handler that passes comments found by the preprocessor
29aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// to the parser action.
30aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass ActionCommentHandler : public CommentHandler {
31aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  Sema &S;
32aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
33aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkopublic:
34aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  explicit ActionCommentHandler(Sema &S) : S(S) { }
35aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
36aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) {
37aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    S.ActOnComment(Comment);
38aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return false;
39aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
40aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko};
4169b5e952c56f95673064ad1815a240e0fb595865Benjamin Kramer} // end anonymous namespace
42aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
43b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas GregorIdentifierInfo *Parser::getSEHExceptKeyword() {
44b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor  // __except is accepted as a (contextual) keyword
454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland))
46b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor    Ident__except = PP.getIdentifierInfo("__except");
47b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor
48b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor  return Ident__except;
49b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor}
50b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor
5125893e0930c1241c3fdafc6f1b8661443bb19692Argyrios KyrtzidisParser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies)
52614f96a7cf94805c2d336639300b62dc2f54e9e0Ted Kremenek  : PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
530fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor    GreaterThanIsOperator(true), ColonIsSacred(false),
546a91d385618ea4d28236c496f540a26877c95525Erik Verbruggen    InMessageExpression(false), TemplateParameterDepth(0),
5525893e0930c1241c3fdafc6f1b8661443bb19692Argyrios Kyrtzidis    ParsingInObjCContainer(false) {
5625893e0930c1241c3fdafc6f1b8661443bb19692Argyrios Kyrtzidis  SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies;
572b970e9d70768802984f2cf1885e73643c17e0c2Chris Lattner  Tok.startToken();
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Tok.setKind(tok::eof);
5923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = 0;
609e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  NumCachedScopes = 0;
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ParenCount = BracketCount = BraceCount = 0;
62849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  CurParsedObjCImpl = 0;
63fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar
64fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // Add #pragma handlers. These are removed and destroyed in the
65fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // destructor.
669595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  AlignHandler.reset(new PragmaAlignHandler());
67cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  PP.AddPragmaHandler(AlignHandler.get());
68cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar
699595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
70aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
71aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
729595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  OptionsHandler.reset(new PragmaOptionsHandler());
739b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(OptionsHandler.get());
749b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis
759595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  PackHandler.reset(new PragmaPackHandler());
769b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(PackHandler.get());
7762c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian
789595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  MSStructHandler.reset(new PragmaMSStructHandler());
7962c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  PP.AddPragmaHandler(MSStructHandler.get());
809b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis
819595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  UnusedHandler.reset(new PragmaUnusedHandler());
829b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(UnusedHandler.get());
839b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis
849595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  WeakHandler.reset(new PragmaWeakHandler());
859b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.AddPragmaHandler(WeakHandler.get());
86321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne
879595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
885f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall  PP.AddPragmaHandler(RedefineExtnameHandler.get());
895f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall
909595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  FPContractHandler.reset(new PragmaFPContractHandler());
91321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  PP.AddPragmaHandler("STDC", FPContractHandler.get());
92f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().OpenCL) {
949595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
95f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
96f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
97f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
98f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
99c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  if (getLangOpts().OpenMP)
100c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    OpenMPHandler.reset(new PragmaOpenMPHandler());
101c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  else
102c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    OpenMPHandler.reset(new PragmaNoOpenMPHandler());
103c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  PP.AddPragmaHandler(OpenMPHandler.get());
104aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
105056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko  CommentSemaHandler.reset(new ActionCommentHandler(actions));
106056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko  PP.addCommentHandler(CommentSemaHandler.get());
107aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
108f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  PP.setCodeCompletionHandler(*this);
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1113cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
11233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return Diags.Report(Loc, DiagID);
1131ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner}
1141ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner
1153cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
1161ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  return Diag(Tok.getLocation(), DiagID);
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1194b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \brief Emits a diagnostic suggesting parentheses surrounding a
1204b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// given range.
1214b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor///
1224b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param Loc The location where we'll emit the diagnostic.
12370517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko/// \param DK The kind of diagnostic to emit.
1244b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param ParenRange Source range enclosing code that should be parenthesized.
1254b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorvoid Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
1264b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor                                SourceRange ParenRange) {
127b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
128b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
1294b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // We can't display the parentheses, so just dig the
1304b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // warning/error and return.
1314b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Loc, DK);
1324b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    return;
1334b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
1341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Diag(Loc, DK)
136849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
137849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(EndLoc, ")");
1384b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor}
1394b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
140837b1a37116cf4e64f8bb7db34982dee1fba7647John McCallstatic bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
141837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  switch (ExpectedTok) {
1424b0824229b96d024a96f3c7dd75ab70652c05c5bRichard Smith  case tok::semi:
1434b0824229b96d024a96f3c7dd75ab70652c05c5bRichard Smith    return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ;
144837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  default: return false;
145837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  }
146837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall}
147837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// input.  If so, it is consumed and false is returned.
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If the input is malformed, this emits the specified diagnostic.  Next, if
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipToTok is specified, it calls SkipUntil(SkipToTok).  Finally, true is
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// returned.
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const char *Msg, tok::TokenKind SkipToTok) {
156dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeAnyToken();
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
160a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
161837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  // Detect common single-character typos and resume.
162837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  if (IsCommonTypo(ExpectedTok, Tok)) {
163837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    SourceLocation Loc = Tok.getLocation();
164837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    Diag(Loc, DiagID)
165837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall      << Msg
166837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall      << FixItHint::CreateReplacement(SourceRange(Loc),
167837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall                                      getTokenSimpleSpelling(ExpectedTok));
168837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    ConsumeAnyToken();
169837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
170837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    // Pretend there wasn't a problem.
171837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    return false;
172837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  }
173837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
1744b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  const char *Spelling = 0;
175b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
1761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (EndLoc.isValid() &&
177b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor      (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
1784b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // Show what code to insert to fix this problem.
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(EndLoc, DiagID)
1804b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor      << Msg
181849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateInsertion(EndLoc, Spelling);
1824b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  } else
1834b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Tok, DiagID) << Msg;
1844b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (SkipToTok != tok::unknown)
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(SkipToTok);
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1909ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregorbool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
1919ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
192fb5825dca4e95fee463fdeaddb8b729294fb4d10Douglas Gregor    ConsumeToken();
1939ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    return false;
1949ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  }
1959ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
1969ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
1979ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      NextToken().is(tok::semi)) {
1989ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    Diag(Tok, diag::err_extraneous_token_before_semi)
1999ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      << PP.getSpelling(Tok)
2009ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      << FixItHint::CreateRemoval(Tok.getLocation());
2019ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeAnyToken(); // The ')' or ']'.
2029ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeToken(); // The ';'.
2039ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    return false;
2049ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  }
2059ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
2069ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  return ExpectAndConsume(tok::semi, DiagID);
2079ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor}
2089ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
209eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smithvoid Parser::ConsumeExtraSemi(ExtraSemiKind Kind, unsigned TST) {
2104b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  if (!Tok.is(tok::semi)) return;
2114b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
212eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  bool HadMultipleSemis = false;
2134b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  SourceLocation StartLoc = Tok.getLocation();
2144b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  SourceLocation EndLoc = Tok.getLocation();
2154b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  ConsumeToken();
2164b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
2174b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) {
218eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    HadMultipleSemis = true;
2194b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    EndLoc = Tok.getLocation();
2204b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    ConsumeToken();
2214b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  }
2224b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
223eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  // C++11 allows extra semicolons at namespace scope, but not in any of the
224eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  // other contexts.
225eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  if (Kind == OutsideFunction && getLangOpts().CPlusPlus) {
22680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (getLangOpts().CPlusPlus11)
227eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith      Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi)
228eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith          << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
229eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    else
230eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith      Diag(StartLoc, diag::ext_extra_semi_cxx11)
231eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith          << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
2324b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    return;
2334b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  }
2344b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
235eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis)
236eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    Diag(StartLoc, diag::ext_extra_semi)
237eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith        << Kind << DeclSpec::getSpecifierName((DeclSpec::TST)TST)
238eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith        << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
239eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  else
240eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    // A single semicolon is valid after a member function definition.
241eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def)
242eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith      << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
2434b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu}
2444b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Error recovery.
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipUntil - Read tokens until we get to the specified token, then consume
250012cf464254804279efa84e21b4b493dde76c5f1Chris Lattner/// it (unless DontConsume is true).  Because we cannot guarantee that the
2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// token will ever occur, this skips to the next token, or to some likely
2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// good stopping point.  If StopAtSemi is true, skipping will stop at a ';'
2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character.
254a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///
2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If SkipUntil finds the specified token, it returns true, otherwise it
256a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// returns false.
257eb52f86a62db523e3c993686b3ed92c55d59d53cDavid Blaikiebool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, bool StopAtSemi,
258eb52f86a62db523e3c993686b3ed92c55d59d53cDavid Blaikie                       bool DontConsume, bool StopAtCodeCompletion) {
2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We always want this function to skip at least one token if the first token
2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // isn't T and if not at EOF.
2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isFirstTokenSkipped = true;
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we found one of the tokens, stop and return true.
264eb52f86a62db523e3c993686b3ed92c55d59d53cDavid Blaikie    for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) {
265000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(Toks[i])) {
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (DontConsume) {
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Noop, don't consume the token.
2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        } else {
2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ConsumeAnyToken();
2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return true;
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
274a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Tok.getKind()) {
2765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::eof:
2775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ran out of tokens.
2785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
279dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
280dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    case tok::code_completion:
2813437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      if (!StopAtCodeCompletion)
2823437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis        ConsumeToken();
283dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      return false;
284dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_paren:
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested parens.
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
2883437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion);
2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_square:
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested square brackets.
2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
2933437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_square, false, false, StopAtCodeCompletion);
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_brace:
2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested braces.
2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
2983437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion);
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
300a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
3015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Since the user wasn't looking for this token (if they were, it would
3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // already be handled), this isn't balanced.  If there is a LHS token at a
3045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // higher level, we will assume that this matches the unbalanced token
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // and return it.  Otherwise, this is a spurious RHS token, which we skip.
3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_paren:
3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ParenCount && !isFirstTokenSkipped)
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
3105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_square:
3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BracketCount && !isFirstTokenSkipped)
3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
3165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_brace:
3175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BraceCount && !isFirstTokenSkipped)
3185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
3195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
321a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::string_literal:
3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::wide_string_literal:
3245cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf8_string_literal:
3255cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf16_string_literal:
3265cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf32_string_literal:
3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeStringToken();
3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
32955edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian
3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::semi:
3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (StopAtSemi)
3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip this token.
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    isFirstTokenSkipped = false;
340a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump  }
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Scope manipulation
3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EnterScope - Start a new scope.
3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::EnterScope(unsigned ScopeFlags) {
3499e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes) {
3509e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    Scope *N = ScopeCache[--NumCachedScopes];
35123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    N->Init(getCurScope(), ScopeFlags);
35223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CurScope = N;
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
3549c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis    Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExitScope - Pop a scope off the scope stack.
3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ExitScope() {
36023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() && "Scope imbalance!");
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
36290ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // Inform the actions module that this scope is going away if there are any
36390ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // decls in it.
36423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!getCurScope()->decl_empty())
36523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
366a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
36723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Scope *OldScope = getCurScope();
36823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = OldScope->getParent();
369a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
3709e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes == ScopeCacheSize)
3719e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete OldScope;
3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
3739e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    ScopeCache[NumCachedScopes++] = OldScope;
3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3767a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
3777a614d8380297fcd2bc23986241905d97222948cRichard Smith/// this object does nothing.
3787a614d8380297fcd2bc23986241905d97222948cRichard SmithParser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
3797a614d8380297fcd2bc23986241905d97222948cRichard Smith                                 bool ManageFlags)
3807a614d8380297fcd2bc23986241905d97222948cRichard Smith  : CurScope(ManageFlags ? Self->getCurScope() : 0) {
3817a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (CurScope) {
3827a614d8380297fcd2bc23986241905d97222948cRichard Smith    OldFlags = CurScope->getFlags();
3837a614d8380297fcd2bc23986241905d97222948cRichard Smith    CurScope->setFlags(ScopeFlags);
3847a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
3857a614d8380297fcd2bc23986241905d97222948cRichard Smith}
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3877a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Restore the flags for the current scope to what they were before this
3887a614d8380297fcd2bc23986241905d97222948cRichard Smith/// object overrode them.
3897a614d8380297fcd2bc23986241905d97222948cRichard SmithParser::ParseScopeFlags::~ParseScopeFlags() {
3907a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (CurScope)
3917a614d8380297fcd2bc23986241905d97222948cRichard Smith    CurScope->setFlags(OldFlags);
3927a614d8380297fcd2bc23986241905d97222948cRichard Smith}
3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// C99 6.9: External Definitions.
3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerParser::~Parser() {
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we still have scopes active, delete the scope tree.
40123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  delete getCurScope();
40223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = 0;
40323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Free the scope cache.
4059e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
4069e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete ScopeCache[i];
407fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar
4088387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Free LateParsedTemplatedFunction nodes.
4098387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin();
4108387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      it != LateParsedTemplateMap.end(); ++it)
4118387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    delete it->second;
4128387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
413fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // Remove the pragma handlers we installed.
414cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  PP.RemovePragmaHandler(AlignHandler.get());
415cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  AlignHandler.reset();
416aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
417aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  GCCVisibilityHandler.reset();
4189b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(OptionsHandler.get());
419861800c676004eabed5927f0552620d06c80a40aDaniel Dunbar  OptionsHandler.reset();
4209b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(PackHandler.get());
4214726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PackHandler.reset();
42262c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  PP.RemovePragmaHandler(MSStructHandler.get());
42362c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructHandler.reset();
4249b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(UnusedHandler.get());
4254726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  UnusedHandler.reset();
4269b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(WeakHandler.get());
4279991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman  WeakHandler.reset();
4285f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall  PP.RemovePragmaHandler(RedefineExtnameHandler.get());
4295f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall  RedefineExtnameHandler.reset();
430f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
4314e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().OpenCL) {
432f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
433f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    OpenCLExtensionHandler.reset();
434f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
435f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
436c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  PP.RemovePragmaHandler(OpenMPHandler.get());
437c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  OpenMPHandler.reset();
438f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
439321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  PP.RemovePragmaHandler("STDC", FPContractHandler.get());
440321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  FPContractHandler.reset();
441aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
442056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko  PP.removeCommentHandler(CommentSemaHandler.get());
443aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
444f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  PP.clearCodeCompletionHandler();
44513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer
44613bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  assert(TemplateIds.empty() && "Still alive TemplateIdAnnotations around?");
4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Initialize - Warm up the parser.
4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::Initialize() {
45231e057270232c1c37602579cb6461c2704175672Chris Lattner  // Create the translation unit scope.  Install it as the current scope.
45323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() == 0 && "A scope is already active?");
45431e057270232c1c37602579cb6461c2704175672Chris Lattner  EnterScope(Scope::DeclScope);
455c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  Actions.ActOnTranslationUnitScope(getCurScope());
456c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor
45734870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  // Initialization for Objective-C context sensitive keywords recognition.
458a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  // Referenced in Parser::ParseObjCTypeQualifierList.
4594e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC1) {
460a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
461a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
462a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
463a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
464a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
465a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
46634870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  }
467662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar
468e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  Ident_instancetype = 0;
4697eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson  Ident_final = 0;
4707eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson  Ident_override = 0;
4711f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson
472662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar  Ident_super = &PP.getIdentifierTable().get("super");
47382287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson
4744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().AltiVec) {
47582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_vector = &PP.getIdentifierTable().get("vector");
47682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_pixel = &PP.getIdentifierTable().get("pixel");
47782287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  }
4780a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4790a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_introduced = 0;
4800a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_deprecated = 0;
4810a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_obsoleted = 0;
482b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  Ident_unavailable = 0;
48328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
484b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor  Ident__except = 0;
485b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor
48628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0;
48728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0;
48828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0;
48928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
4904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if(getLangOpts().Borland) {
49128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__exception_info        = PP.getIdentifierInfo("_exception_info");
49228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___exception_info       = PP.getIdentifierInfo("__exception_info");
49328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_GetExceptionInfo       = PP.getIdentifierInfo("GetExceptionInformation");
49428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__exception_code        = PP.getIdentifierInfo("_exception_code");
49528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___exception_code       = PP.getIdentifierInfo("__exception_code");
49628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_GetExceptionCode       = PP.getIdentifierInfo("GetExceptionCode");
49728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__abnormal_termination  = PP.getIdentifierInfo("_abnormal_termination");
49828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
49928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_AbnormalTermination    = PP.getIdentifierInfo("AbnormalTermination");
50028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
50128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
50228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
50328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
50428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
50528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
50628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
50728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
50828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
50928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
51028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
511c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor
512c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor  Actions.Initialize();
513c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor
514c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor  // Prime the lexer look-ahead.
515c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor  ConsumeToken();
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
51813bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramernamespace {
51913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  /// \brief RAIIObject to destroy the contents of a SmallVector of
52013bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  /// TemplateIdAnnotation pointers and clear the vector.
52113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  class DestroyTemplateIdAnnotationsRAIIObj {
52213bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    SmallVectorImpl<TemplateIdAnnotation *> &Container;
52313bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  public:
52413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    DestroyTemplateIdAnnotationsRAIIObj(SmallVectorImpl<TemplateIdAnnotation *>
52513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer                                       &Container)
52613bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      : Container(Container) {}
52713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer
52813bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    ~DestroyTemplateIdAnnotationsRAIIObj() {
52913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      for (SmallVectorImpl<TemplateIdAnnotation *>::iterator I =
53013bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer           Container.begin(), E = Container.end();
53113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer           I != E; ++I)
53213bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer        (*I)->Destroy();
53313bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      Container.clear();
53413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    }
53513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  };
53613bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer}
53713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer
5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// action tells us to.  This returns true if the EOF was encountered.
540682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattnerbool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
54113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
542b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
543e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  // Skip over the EOF token, flagging end of previous input for incremental
544e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  // processing
545e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof))
546e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    ConsumeToken();
547e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
548b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  while (Tok.is(tok::annot_pragma_unused))
549b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis    HandlePragmaUnused();
550b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
551682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  Result = DeclGroupPtrTy();
5529299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  if (Tok.is(tok::eof)) {
5538387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    // Late template parsing can begin.
5544e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().DelayedTemplateParsing)
5558387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
556e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    if (!PP.isIncrementalProcessingEnabled())
557e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann      Actions.ActOnEndOfTranslationUnit();
558e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    //else don't tell Sema that we ended parsing: more input might come.
5598387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
5609299f3fa85796613cc787a2062c9562d07c8613eChris Lattner    return true;
5619299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  }
562a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
5630b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributesWithRange attrs(AttrFactory);
5644e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  MaybeParseCXX11Attributes(attrs);
5657f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseMicrosoftAttributes(attrs);
566e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
5677f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  Result = ParseExternalDeclaration(attrs);
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
5695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseExternalDeclaration:
57290b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner///
573c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
574c3018153a11afe91849748a93d920040a571b76cChris Lattner///         function-definition
575c3018153a11afe91849748a93d920040a571b76cChris Lattner///         declaration
5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU]   asm-definition
577c3018153a11afe91849748a93d920040a571b76cChris Lattner/// [GNU]   __extension__ external-declaration
5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-declaration
5805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-alias-declaration
5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-protocol-definition
5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-method-definition
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  @end
584c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor/// [C++]   linkage-specification
5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-definition:
5865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         simple-asm-expr ';'
5876b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith/// [C++11] empty-declaration
5886b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith/// [C++11] attribute-declaration
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
5906b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith/// [C++11] empty-declaration:
591a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///           ';'
592a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///
59345f965581935791a018df829a14dff53c1dd8f47Douglas Gregor/// [C++0x/GNU] 'extern' 'template' declaration
5947f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::DeclGroupPtrTy
5957f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
5967f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                 ParsingDeclSpec *DS) {
59713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
59836d36806f1972f7ec1d2a3f59155187278c56508Argyrios Kyrtzidis  ParenBraceBracketBalancer BalancerRAIIObj(*this);
5997d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
6007d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  if (PP.isCodeCompletionReached()) {
6017d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
6027d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
6037d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  }
6047d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
605d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *SingleDecl = 0;
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Tok.getKind()) {
607426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola  case tok::annot_pragma_vis:
608426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola    HandlePragmaVisibility();
609426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola    return DeclGroupPtrTy();
610aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman  case tok::annot_pragma_pack:
611aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman    HandlePragmaPack();
612aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman    return DeclGroupPtrTy();
6139595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_msstruct:
6149595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaMSStruct();
6159595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6169595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_align:
6179595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaAlign();
6189595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6199595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_weak:
6209595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaWeak();
6219595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6229595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_weakalias:
6239595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaWeakAlias();
6249595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6259595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_redefine_extname:
6269595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaRedefineExtname();
6279595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6289595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_fp_contract:
6299595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaFPContract();
6309595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6319595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_opencl_extension:
6329595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaOpenCLExtension();
6339595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
634c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  case tok::annot_pragma_openmp:
635c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    ParseOpenMPDeclarativeDirective();
636c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    return DeclGroupPtrTy();
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::semi:
6386b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith    // Either a C++11 empty-declaration or attribute-declaration.
639684aa73192d92850a926870be62a1787eb5b7ed9Michael Han    SingleDecl = Actions.ActOnEmptyDeclaration(getCurScope(),
640684aa73192d92850a926870be62a1787eb5b7ed9Michael Han                                               attrs.getList(),
641684aa73192d92850a926870be62a1787eb5b7ed9Michael Han                                               Tok.getLocation());
6424b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    ConsumeExtraSemi(OutsideFunction);
643684aa73192d92850a926870be62a1787eb5b7ed9Michael Han    break;
64490b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::r_brace:
645883692ebd421c40b44e2c2665e5f54dade5621bcNico Weber    Diag(Tok, diag::err_extraneous_closing_brace);
64690b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    ConsumeBrace();
647682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
64890b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::eof:
64990b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    Diag(Tok, diag::err_expected_external_declaration);
650682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
651c3018153a11afe91849748a93d920040a571b76cChris Lattner  case tok::kw___extension__: {
652c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    // __extension__ silences extension warnings in the subexpression.
653c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
65439146d6497ad5e7ca8ef639221e7b3e15d07c888Chris Lattner    ConsumeToken();
6557f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    return ParseExternalDeclaration(attrs);
656c3018153a11afe91849748a93d920040a571b76cChris Lattner  }
657dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  case tok::kw_asm: {
6587f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ProhibitAttributes(attrs);
659bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
66021e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation StartLoc = Tok.getLocation();
66121e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation EndLoc;
66221e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    ExprResult Result(ParseSimpleAsm(&EndLoc));
663a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
6643f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson    ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
6653f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson                     "top-level asm block");
666dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson
667682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    if (Result.isInvalid())
668682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
66921e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
670682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
671dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  }
6725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::at:
67395ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    return ParseObjCAtDirectives();
6745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::minus:
6755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::plus:
6764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().ObjC1) {
677682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      Diag(Tok, diag::err_expected_external_declaration);
678682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      ConsumeToken();
679682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
680682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    }
681682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = ParseObjCMethodDefinition();
682682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
683791215b7a24666912c0b71175d2ca5ba082f666eDouglas Gregor  case tok::code_completion:
68423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
685849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                             CurParsedObjCImpl? Sema::PCC_ObjCImplementation
686f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                              : Sema::PCC_Namespace);
6877d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
6887d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
689f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  case tok::kw_using:
6908f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  case tok::kw_namespace:
6915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_typedef:
692adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_template:
693adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_export:    // As in 'export template'
694511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  case tok::kw_static_assert:
695c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne  case tok::kw__Static_assert:
69626d6023cb0d343bf8fc8836f97d39709bbd4afa0Chad Rosier    // A function definition cannot start with any of these keywords.
69797144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    {
69897144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner      SourceLocation DeclEnd;
6994e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer      StmtVector Stmts;
7007f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
70197144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    }
702d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
7037306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_static:
7047306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // Parse (then ignore) 'static' prior to a template instantiation. This is
7057306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // a GCC extension that we intentionally do not support.
7064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
7077306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
7087306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        << 0;
709d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl      SourceLocation DeclEnd;
7104e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer      StmtVector Stmts;
7117f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
7127306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    }
7137306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    goto dont_know;
7147306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
7157306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_inline:
7164e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus) {
7177306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      tok::TokenKind NextKind = NextToken().getKind();
7187306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
7197306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Inline namespaces. Allowed as an extension even in C++03.
7207306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_namespace) {
7217306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
7224e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer        StmtVector Stmts;
7237f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
7247306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
7257306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
7267306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Parse (then ignore) 'inline' prior to a template instantiation. This is
7277306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // a GCC extension that we intentionally do not support.
7287306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_template) {
7297306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
7307306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor          << 1;
7317306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
7324e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer        StmtVector Stmts;
7337f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
7347306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
735d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    }
736d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
737d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
73845f965581935791a018df829a14dff53c1dd8f47Douglas Gregor  case tok::kw_extern:
7394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
74045f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      // Extern templates
74145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation ExternLoc = ConsumeToken();
74245f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation TemplateLoc = ConsumeToken();
74380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      Diag(ExternLoc, getLangOpts().CPlusPlus11 ?
7449324583ad2afd09db8c9967cd05c4fa44bac9555Richard Smith             diag::warn_cxx98_compat_extern_template :
7459324583ad2afd09db8c9967cd05c4fa44bac9555Richard Smith             diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
74645f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation DeclEnd;
74745f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      return Actions.ConvertDeclToDeclGroup(
7489241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis                  ParseExplicitInstantiation(Declarator::FileContext,
7499241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis                                             ExternLoc, TemplateLoc, DeclEnd));
75045f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    }
75145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    // FIXME: Detect C++ linkage specifications here?
752d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
7531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
754f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_exists:
755f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_not_exists:
756563a645de82231a55e221fe655b7188bf8369662Francois Pichet    ParseMicrosoftIfExistsExternalDeclaration();
757f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return DeclGroupPtrTy();
7586aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
760d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl  dont_know:
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // We can't tell whether this is a function-definition or declaration yet.
76220af49a7c5bdb6cca5f4d6586106ef1ce8579311Rafael Espindola    return ParseDeclarationOrFunctionDefinition(attrs, DS);
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
765682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // This routine returns a DeclGroup, if the thing we parsed only contains a
766682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // single decl, convert it now.
767682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  return Actions.ConvertDeclToDeclGroup(SingleDecl);
7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7701426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
7711426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, continues a declaration or declaration list.
772e4246a633b13197634225971b25df0cbdcec0c5dSean Huntbool Parser::isDeclarationAfterDeclarator() {
773e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  // Check for '= delete' or '= default'
7744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
775e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
776e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
777e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      return false;
778e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
7796c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian
7801426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor  return Tok.is(tok::equal) ||      // int X()=  -> not a function def
7811426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::comma) ||           // int X(),  -> not a function def
7821426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::semi)  ||           // int X();  -> not a function def
7831426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
7841426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
7854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    (getLangOpts().CPlusPlus &&
78639700f81c5b42e6be93be10275602915f872fc86Fariborz Jahanian     Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
7871426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
7881426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
7891426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
7901426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, indicates the start of a function definition.
791004659a56916f2f81ede507c12516c146d6c0df3Chris Lattnerbool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
792075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
7935d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  if (Tok.is(tok::l_brace))   // int X() {}
7945d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner    return true;
7955d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner
796004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  // Handle K&R C argument lists: int X(f) int f; {}
7974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CPlusPlus &&
798075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      Declarator.getFunctionTypeInfo().isKNRPrototype())
799004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner    return isDeclarationSpecifier();
800e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
8014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
802e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
803e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
804e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
805004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner
8065d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
8075d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner         Tok.is(tok::kw_try);          // X() try { ... }
8081426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
8091426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
8105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
8115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// a declaration.  We can't tell which we have until we read up to the
812c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// compound-statement in function-definition. TemplateParams, if
813c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// non-NULL, provides the template parameters when we're parsing a
8141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ template-declaration.
8155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       function-definition: [C99 6.9.1]
817a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
818a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
819a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
820a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///
8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       declaration: [C99 6.7]
822697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner///         declaration-specifiers init-declarator-list[opt] ';'
823697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner/// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
8245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OMP]   threadprivate-directive                              [TODO]
8255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
826682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerParser::DeclGroupPtrTy
8272edf0a2520313cde900799b1eb9bd11c9c776afeSean HuntParser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs,
8282edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                       ParsingDeclSpec &DS,
8292edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                       AccessSpecifier AS) {
8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Parse the common declaration-specifiers piece.
8310efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
832a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
8345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // declaration-specifiers init-declarator-list[opt] ';'
835000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(tok::semi)) {
8362edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ProhibitAttributes(attrs);
8375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
838d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
83954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.complete(TheDecl);
840682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
8415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
842a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8432edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  DS.takeAttributesFrom(attrs);
8442edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
845246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // ObjC2 allows prefix attributes on class interfaces and protocols.
846246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // FIXME: This still needs better diagnostics. We should only accept
847246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // attributes here, no types, etc.
8484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC2 && Tok.is(tok::at)) {
849dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation AtLoc = ConsumeToken(); // the "@"
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
851246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar        !Tok.isObjCAtKeyword(tok::objc_protocol)) {
852246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar      Diag(Tok, diag::err_objc_unexpected_attr);
853cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      SkipUntil(tok::semi); // FIXME: better skip?
854682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
855cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    }
856d8ac05753dc4506224d445ff98399c01da3136e5John McCall
85754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.abort();
85854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
8590de2ae28c603322f05e2d9200c7d457c8b928983Fariborz Jahanian    const char *PrevSpec = 0;
860fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
861fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
862fec54013fcd0eb72642741584ca04c1bc292bef8John McCall      Diag(AtLoc, DiagID) << PrevSpec;
8631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
864246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar    if (Tok.isObjCAtKeyword(tok::objc_protocol))
865bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor      return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
866bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor
867bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return Actions.ConvertDeclToDeclGroup(
868bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor            ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
869dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
870a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
871c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // If the declspec consisted only of 'extern' and we have a string
872c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // literal following it, this must be a C++ linkage specifier like
873c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // 'extern "C"'.
8744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Tok.is(tok::string_literal) && getLangOpts().CPlusPlus &&
875c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner      DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
876682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
877d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
878682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
879682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  }
880c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
881d8ac05753dc4506224d445ff98399c01da3136e5John McCall  return ParseDeclGroup(DS, Declarator::FileContext, true);
8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8843acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::DeclGroupPtrTy
8852edf0a2520313cde900799b1eb9bd11c9c776afeSean HuntParser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs,
8862edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                             ParsingDeclSpec *DS,
8873acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian                                             AccessSpecifier AS) {
8882edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  if (DS) {
8892edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    return ParseDeclOrFunctionDefInternal(attrs, *DS, AS);
8902edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  } else {
8912edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ParsingDeclSpec PDS(*this);
8922edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // Must temporarily exit the objective-c container scope for
8932edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // parsing c constructs and re-enter objc container scope
8942edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // afterwards.
8952edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ObjCDeclContextSwitch ObjCDC(*this);
8962edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
8972edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    return ParseDeclOrFunctionDefInternal(attrs, PDS, AS);
8982edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  }
8993acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian}
9003acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian
9015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseFunctionDefinition - We parsed and verified that the specified
9025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Declarator is well formed.  If this is a K&R-style function, read the
9035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// parameters declaration-list, then start the compound-statement.
9045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
905a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///       function-definition: [C99 6.9.1]
906a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
907a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
908a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
9097ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
91023c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
91123c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         function-body
9127ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
913d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl///         decl-specifier-seq[opt] declarator function-try-block
9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
915d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
916c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins                                      const ParsedTemplateInfo &TemplateInfo,
917c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins                                      LateParsedAttrList *LateParsedAttrs) {
91828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // Poison the SEH identifiers so they are flagged as illegal in function bodies
91928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
920075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
921a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
922a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // If this is C90 and the declspecs were completely missing, fudge in an
923a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // implicit int.  We do this here because this is the only place where
924a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // declaration-specifiers are completely optional in the grammar.
9254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
926a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner    const char *PrevSpec;
927fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
92831c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner    D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
92931c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner                                           D.getIdentifierLoc(),
930fec54013fcd0eb72642741584ca04c1bc292bef8John McCall                                           PrevSpec, DiagID);
931ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
932a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  }
933a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
9345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this declaration was formed with a K&R-style identifier list for the
9355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // arguments, parse declarations for all of the args next.
9365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // int foo(a,b) int a; float b; {}
937004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  if (FTI.isKNRPrototype())
9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseKNRParamDeclarations(D);
9395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9407ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // We should have either an opening brace or, in a C++ constructor,
9417ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // we may have a colon.
942758afbcc86ef15f8d433f5f87db1495e50effeb3Douglas Gregor  if (Tok.isNot(tok::l_brace) &&
9434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      (!getLangOpts().CPlusPlus ||
944cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt       (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
945cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        Tok.isNot(tok::equal)))) {
9465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::err_expected_fn_body);
9475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
9495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(tok::l_brace, true, true);
950a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
9515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we didn't find the '{', bail out.
952000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.isNot(tok::l_brace))
953d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
9545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
955a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
956c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // Check to make sure that any normal attributes are allowed to be on
957c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // a definition.  Late parsed attributes are checked at the end.
958c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  if (Tok.isNot(tok::equal)) {
959c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    AttributeList *DtorAttrs = D.getAttributes();
960c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    while (DtorAttrs) {
961cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (!IsThreadSafetyAttribute(DtorAttrs->getName()->getName()) &&
962cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          !DtorAttrs->isCXX11Attribute()) {
963c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins        Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition)
964c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins          << DtorAttrs->getName()->getName();
965c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins      }
966c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins      DtorAttrs = DtorAttrs->getNext();
967c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    }
968c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  }
969c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins
9708387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // In delayed template parsing mode, for function template we consume the
9718387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // tokens and store them for late parsing at the end of the translation unit.
9724e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().DelayedTemplateParsing &&
9730963017dcbc32176c79a251c3ab23bc35ac784e5Douglas Gregor      Tok.isNot(tok::equal) &&
9748387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      TemplateInfo.Kind == ParsedTemplateInfo::Template) {
9755354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
9768387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9778387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
9788387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Scope *ParentScope = getCurScope()->getParent();
9798387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
98045fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor    D.setFunctionDefinitionKind(FDK_Definition);
9818387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Decl *DP = Actions.HandleDeclarator(ParentScope, D,
9823fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                        TemplateParameterLists);
9838387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.complete(DP);
9848387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.getMutableDeclSpec().abort();
9858387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9868387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (DP) {
987e1fca502e7f1349e9b4520a4ca9a02413bcf2b14Francois Pichet      LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(DP);
9888387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9898387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      FunctionDecl *FnD = 0;
9908387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
9918387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = FunTmpl->getTemplatedDecl();
9928387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      else
9938387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = cast<FunctionDecl>(DP);
994d4a0caf78e7c18e7aca65fbfd799a6c024ff51fbFrancois Pichet      Actions.CheckForFunctionRedefinition(FnD);
9958387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9968387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateParsedTemplateMap[FnD] = LPT;
9978387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.MarkAsLateParsedTemplate(FnD);
9988387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(LPT->Toks);
9998387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    } else {
10008387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      CachedTokens Toks;
10018387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(Toks);
10028387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    }
10038387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return DP;
10048387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
10052eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian  else if (CurParsedObjCImpl &&
10069e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian           !TemplateInfo.TemplateParams &&
10079e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian           (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
10089e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian            Tok.is(tok::colon)) &&
1009be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      Actions.CurContext->isTranslationUnit()) {
1010be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1011be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    Scope *ParentScope = getCurScope()->getParent();
1012be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian
1013be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.setFunctionDefinitionKind(FDK_Definition);
1014be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
10155354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                              MultiTemplateParamsArg());
1016be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.complete(FuncDecl);
1017be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.getMutableDeclSpec().abort();
1018be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    if (FuncDecl) {
1019be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      // Consume the tokens and store them for later parsing.
1020be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      StashAwayMethodOrFunctionBodyTokens(FuncDecl);
1021be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      CurParsedObjCImpl->HasCFunction = true;
1022be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      return FuncDecl;
1023be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    }
1024be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian  }
1025be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian
1026b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Enter a scope for the function body.
10278935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1028a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1029b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Tell the actions module that we have entered a function definition with the
1030b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // specified Declarator for the function.
1031d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Res = TemplateInfo.TemplateParams?
103223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
10335354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                              *TemplateInfo.TemplateParams, D)
103423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
1035a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
103654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclarator context before we parse the body.
103754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.complete(Res);
103854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
103954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclSpec context, too.  This const_cast is
104054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // safe because we're always the sole owner.
104154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.getMutableDeclSpec().abort();
104254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
1043cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  if (Tok.is(tok::equal)) {
10444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
1045cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ConsumeToken();
1046cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1047cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    Actions.ActOnFinishFunctionBody(Res, 0, false);
1048cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1049cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    bool Delete = false;
1050cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    SourceLocation KWLoc;
1051cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Tok.is(tok::kw_delete)) {
105280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      Diag(Tok, getLangOpts().CPlusPlus11 ?
10537fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith           diag::warn_cxx98_compat_deleted_function :
1054d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith           diag::ext_deleted_function);
1055cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1056cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      KWLoc = ConsumeToken();
1057cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Actions.SetDeclDeleted(Res, KWLoc);
1058cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Delete = true;
1059cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else if (Tok.is(tok::kw_default)) {
106080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      Diag(Tok, getLangOpts().CPlusPlus11 ?
10617fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith           diag::warn_cxx98_compat_defaulted_function :
1062d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith           diag::ext_defaulted_function);
1063cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1064cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      KWLoc = ConsumeToken();
1065cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Actions.SetDeclDefaulted(Res, KWLoc);
1066cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else {
1067cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      llvm_unreachable("function definition after = not 'delete' or 'default'");
1068cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
1069cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1070cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Tok.is(tok::comma)) {
1071cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
1072cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        << Delete;
1073cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      SkipUntil(tok::semi);
1074cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else {
1075cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1076cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                       Delete ? "delete" : "default", tok::semi);
1077cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
1078cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1079cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    return Res;
1080cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  }
1081cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1082d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (Tok.is(tok::kw_try))
1083c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor    return ParseFunctionTryBlock(Res, BodyScope);
1084d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
10857ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // If we have a colon, then we're probably parsing a C++
10867ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // ctor-initializer.
1087d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  if (Tok.is(tok::colon)) {
10887ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    ParseConstructorInitializer(Res);
1089d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall
1090d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    // Recover from error.
1091d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    if (!Tok.is(tok::l_brace)) {
1092c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor      BodyScope.Exit();
10939ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.ActOnFinishFunctionBody(Res, 0);
1094d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall      return Res;
1095d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    }
1096d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  } else
1097393612e6c7727f1fee50039254d9f434364cc0b2Fariborz Jahanian    Actions.ActOnDefaultCtorInitializers(Res);
10987ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
1099c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // Late attributes are parsed in the same scope as the function body.
1100c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  if (LateParsedAttrs)
1101c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
1102c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins
1103c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor  return ParseFunctionStatementBody(Res, BodyScope);
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// types for a function with a K&R-style identifier list for arguments.
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseKNRParamDeclarations(Declarator &D) {
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We know that the top-level of this declarator is a function.
1110075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
111204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // Enter function-declaration scope, limiting any declarators to the
111304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // function prototype scope, including parameter declarators.
11143a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
11153a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith                            Scope::FunctionDeclarationScope | Scope::DeclScope);
111604421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read all the argument declarations.
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isDeclarationSpecifier()) {
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SourceLocation DSStart = Tok.getLocation();
1120a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the common declaration-specifiers piece.
11220b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarationSpecifiers(DS);
1124a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: 'each declaration in the declaration list shall have at
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // least one declarator'.
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // the declarations though.  It's trivial to ignore them, really hard to do
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // anything else with them.
1130000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.is(tok::semi)) {
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DSStart, diag::err_declaration_does_not_declare_param);
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1135a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // than register.
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        DS.getStorageClassSpec() != DeclSpec::SCS_register) {
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getStorageClassSpecLoc(),
11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.isThreadSpecified()) {
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getThreadSpecLoc(),
11465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1149a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the first declarator attached to this declspec.
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
11525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarator(ParmDeclarator);
11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Handle the full declarator list.
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (1) {
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If attributes are present, parse them.
11577f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      MaybeParseGNUAttributes(ParmDeclarator);
1158a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ask the actions module to compute the type for this declarator.
1160d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *Param =
116123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
11622bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff
1163a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump      if (Param &&
11645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // A missing identifier has already been diagnosed.
11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ParmDeclarator.getIdentifier()) {
11665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Scan the argument list looking for the correct param to apply this
11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // type.
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0; ; ++i) {
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // C99 6.9.1p6: those declarators shall declare only identifiers from
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // the identifier list.
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (i == FTI.NumArgs) {
11731ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner            Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
11746898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner              << ParmDeclarator.getIdentifier();
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
1177a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Reject redefinitions of parameters.
118004421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner            if (FTI.ArgInfo[i].Param) {
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(ParmDeclarator.getIdentifierLoc(),
11821ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner                   diag::err_param_redefinition)
11836898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner                 << ParmDeclarator.getIdentifier();
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            } else {
118504421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner              FTI.ArgInfo[i].Param = Param;
11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
11875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we don't have a comma, it is either the end of the list (a ';') or
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // an error, bail out.
1194000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.isNot(tok::comma))
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
1196a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11977984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith      ParmDeclarator.clear();
11987984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith
11995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Consume the comma.
12007984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith      ParmDeclarator.setCommaLoc(ConsumeToken());
1201a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Parse the next declarator.
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParseDeclarator(ParmDeclarator);
12045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1205a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12068bb21d32e9ccc9d9c221506dff26acafa8724ccaChris Lattner    if (ExpectAndConsumeSemi(diag::err_expected_semi_declaration)) {
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip to end of block or statement
12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi, true);
1209000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(tok::semi))
12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ConsumeToken();
12115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1213a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The actions module must verify that all arguments were declared.
121523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
12165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// allowed to be a wide string, and is not subject to character translation.
12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-string-literal:
12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         string-literal
12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
122560d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseAsmStringLiteral() {
12267f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek  switch (Tok.getKind()) {
12277f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    case tok::string_literal:
12287f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      break;
122999831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf8_string_literal:
123099831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf16_string_literal:
123199831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf32_string_literal:
12327f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    case tok::wide_string_literal: {
12337f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      SourceLocation L = Tok.getLocation();
12347f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      Diag(Tok, diag::err_asm_operand_wide_string_literal)
123599831e4677a7e2e051af636221694d60ba31fcdbRichard Smith        << (Tok.getKind() == tok::wide_string_literal)
12367f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek        << SourceRange(L, L);
12377f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      return ExprError();
12387f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    }
12397f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    default:
124097f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs      Diag(Tok, diag::err_expected_string_literal)
124197f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs        << /*Source='in...'*/0 << "'asm'";
12427f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      return ExprError();
12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1244a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
124599831e4677a7e2e051af636221694d60ba31fcdbRichard Smith  return ParseStringLiteralExpression();
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseSimpleAsm
12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
12505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] simple-asm-expr:
12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'asm' '(' asm-string-literal ')'
12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
125360d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
1254000732226610650837478cba97843d19b75f648eChris Lattner  assert(Tok.is(tok::kw_asm) && "Not an asm!");
1255dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  SourceLocation Loc = ConsumeToken();
1256a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12577a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  if (Tok.is(tok::kw_volatile)) {
1258841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    // Remove from the end of 'asm' to the end of 'volatile'.
1259841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1260841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall                             PP.getLocForEndOfToken(Tok.getLocation()));
1261841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall
1262841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    Diag(Tok, diag::warn_file_asm_volatile)
1263849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateRemoval(RemovalRange);
12647a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall    ConsumeToken();
12657a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  }
12667a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall
12674a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
12684a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
12691ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "asm";
127061364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl    return ExprError();
12715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1272a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
127360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(ParseAsmStringLiteral());
1274a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1275ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Result.isInvalid()) {
1276ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SkipUntil(tok::r_paren, true, true);
1277ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1278ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Tok.getLocation();
1279ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    ConsumeAnyToken();
1280ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  } else {
12814a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    // Close the paren and get the location of the end bracket
12824a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
1283ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
12844a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      *EndLoc = T.getCloseLocation();
1285ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
1286a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12873fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
12885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
129025a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// \brief Get the TemplateIdAnnotation from the token and put it in the
129125a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// cleanup pool so that it gets destroyed when parsing the current top level
129225a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// declaration is finished.
129325a767651d14db87aa03dd5fe3e011d877dd4100Argyrios KyrtzidisTemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
129425a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  assert(tok.is(tok::annot_template_id) && "Expected template-id token");
129525a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  TemplateIdAnnotation *
129625a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
129725a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  return Id;
129825a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis}
129925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis
13000576681bac125be07f77f66b02a3dba2c3a24557Richard Smithvoid Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {
13010576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Push the current token back into the token stream (or revert it if it is
13020576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // cached) and use an annotation scope token for current token.
13030576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (PP.isBacktrackEnabled())
13040576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.RevertCachedTokens(1);
13050576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  else
13060576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.EnterToken(Tok);
13070576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setKind(tok::annot_cxxscope);
13080576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
13090576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setAnnotationRange(SS.getRange());
13100576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13110576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // In case the tokens were cached, have Preprocessor replace them
13120576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // with the annotation token.  We don't need to do this if we've
13130576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // just reverted back to a prior state.
13140576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (IsNewAnnotation)
13150576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
13160576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
13170576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13180576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \brief Attempt to classify the name at the current token position. This may
13190576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// form a type, scope or primary expression annotation, or replace the token
13200576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// with a typo-corrected keyword. This is only appropriate when the current
13210576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// name must refer to an entity which has already been declared.
13220576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///
13230576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \param IsAddressOfOperand Must be \c true if the name is preceded by an '&'
13240576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///        and might possibly have a dependent nested name specifier.
13250576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \param CCC Indicates how to perform typo-correction for this name. If NULL,
13260576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///        no typo correction will be performed.
13270576681bac125be07f77f66b02a3dba2c3a24557Richard SmithParser::AnnotatedNameKind
13280576681bac125be07f77f66b02a3dba2c3a24557Richard SmithParser::TryAnnotateName(bool IsAddressOfOperand,
13290576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                        CorrectionCandidateCallback *CCC) {
13300576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));
13310576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13320576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  const bool EnteringContext = false;
13330576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
13340576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13350576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  CXXScopeSpec SS;
13360576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (getLangOpts().CPlusPlus &&
13370576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
13380576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Error;
13390576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13400576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
13410576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
13420576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                  !WasScopeAnnotation))
13430576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
13440576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Unresolved;
13450576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
13460576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13470576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  IdentifierInfo *Name = Tok.getIdentifierInfo();
13480576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  SourceLocation NameLoc = Tok.getLocation();
13490576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13500576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // FIXME: Move the tentative declaration logic into ClassifyName so we can
13510576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // typo-correct to tentatively-declared identifiers.
13520576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (isTentativelyDeclared(Name)) {
13530576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // Identifier has been tentatively declared, and thus cannot be resolved as
13540576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // an expression. Fall back to annotating it as a type.
13550576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
13560576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                  !WasScopeAnnotation))
13570576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
13580576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl;
13590576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
13600576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13610576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Token Next = NextToken();
13620576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13630576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Look up and classify the identifier. We don't perform any typo-correction
13640576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // after a scope specifier, because in general we can't recover from typos
13650576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // there (eg, after correcting 'A::tempalte B<X>::C', we would need to jump
13660576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // back into scope specifier parsing).
13670576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Sema::NameClassification Classification
13680576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next,
13690576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                           IsAddressOfOperand, SS.isEmpty() ? CCC : 0);
13700576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13710576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  switch (Classification.getKind()) {
13720576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Error:
13730576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Error;
13740576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13750576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Keyword:
13760576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // The identifier was typo-corrected to a keyword.
13770576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setIdentifierInfo(Name);
13780576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(Name->getTokenID());
13790576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.TypoCorrectToken(Tok);
13800576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
13810576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      AnnotateScopeToken(SS, !WasScopeAnnotation);
13820576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // We've "annotated" this as a keyword.
13830576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
13840576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13850576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Unknown:
13860576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // It's not something we know about. Leave it unannotated.
13870576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    break;
13880576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13890576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Type:
13900576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(tok::annot_typename);
13910576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    setTypeAnnotation(Tok, Classification.getType());
13920576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setAnnotationEndLoc(NameLoc);
13930576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
13940576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      Tok.setLocation(SS.getBeginLoc());
13950576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
13960576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
13970576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13980576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Expression:
13990576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(tok::annot_primary_expr);
14000576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    setExprAnnotation(Tok, Classification.getExpression());
14010576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setAnnotationEndLoc(NameLoc);
14020576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
14030576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      Tok.setLocation(SS.getBeginLoc());
14040576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
14050576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
14060576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
14070576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_TypeTemplate:
14080576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (Next.isNot(tok::less)) {
14090576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      // This may be a type template being used as a template template argument.
14100576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      if (SS.isNotEmpty())
14110576681bac125be07f77f66b02a3dba2c3a24557Richard Smith        AnnotateScopeToken(SS, !WasScopeAnnotation);
14120576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_TemplateName;
14130576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    }
14140576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // Fall through.
14150576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_FunctionTemplate: {
14160576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // We have a type or function template followed by '<'.
14170576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    ConsumeToken();
14180576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    UnqualifiedId Id;
14190576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Id.setIdentifier(Name, NameLoc);
14200576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (AnnotateTemplateIdToken(
14210576681bac125be07f77f66b02a3dba2c3a24557Richard Smith            TemplateTy::make(Classification.getTemplateName()),
14220576681bac125be07f77f66b02a3dba2c3a24557Richard Smith            Classification.getTemplateNameKind(), SS, SourceLocation(), Id))
14230576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
14240576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
14250576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
14260576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
14270576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_NestedNameSpecifier:
14280576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    llvm_unreachable("already parsed nested name specifier");
14290576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
14300576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
14310576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Unable to classify the name, but maybe we can annotate a scope specifier.
14320576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (SS.isNotEmpty())
14330576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    AnnotateScopeToken(SS, !WasScopeAnnotation);
14340576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  return ANK_Unresolved;
14350576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
14360576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
1437eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateTypeOrScopeToken - If the current token position is on a
1438eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typename (possibly qualified in C++) or a C++ scope specifier not followed
1439eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1440eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// with a single annotation token representing the typename or C++ scope
1441eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// respectively.
1442eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This simplifies handling of C++ scope specifiers and allows efficient
1443eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// backtracking without the need to re-parse and resolve nested-names and
1444eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typenames.
144544802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// It will mainly be called when we expect to treat identifiers as typenames
144644802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// (if they are typenames). For example, in C we do not expect identifiers
144744802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// inside expressions to be treated as typenames so it will not be called
144844802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// for expressions in C.
144944802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// The benefit for C/ObjC is that a typename will be annotated and
1450b43a50ff1b0b171ece84425b0ad83a9a31f038faSteve Naroff/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
145144802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// will not be called twice, once to check whether we have a declaration
145244802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// specifier, and another one to get the actual type inside
145344802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// ParseDeclarationSpecifiers).
1454a7bc7c880f86bc180684ef032d06df51bcae7a23Chris Lattner///
14559ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// This returns true if an error occurred.
14561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
145755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
145855a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1459fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrainbool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
14601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
146142d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie          || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)
146223756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          || Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id))
146323756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          && "Cannot be a type or scope token!");
14641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1465d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Tok.is(tok::kw_typename)) {
1466d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Parse a C++ typename-specifier, e.g., "typename T::type".
1467d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //
1468d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //   typename-specifier:
1469d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //     'typename' '::' [opt] nested-name-specifier identifier
14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     'typename' '::' [opt] nested-name-specifier template [opt]
14711734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    //            simple-template-id
1472d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    SourceLocation TypenameLoc = ConsumeToken();
1473d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    CXXScopeSpec SS;
1474efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(),
1475efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor                                       /*EnteringContext=*/false,
14764147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                       0, /*IsTypename*/true))
14779ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
14789ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    if (!SS.isSet()) {
1479b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet      if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
1480b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet          Tok.is(tok::annot_decltype)) {
148123756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith        // Attempt to recover by skipping the invalid 'typename'
1482b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet        if (Tok.is(tok::annot_decltype) ||
1483b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet            (!TryAnnotateTypeOrScopeToken(EnteringContext, NeedType) &&
1484b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet            Tok.isAnnotation())) {
148523756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          unsigned DiagID = diag::err_expected_qualified_after_typename;
148623756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          // MS compatibility: MSVC permits using known types with typename.
148723756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          // e.g. "typedef typename T* pointer_type"
148823756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          if (getLangOpts().MicrosoftExt)
148923756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith            DiagID = diag::warn_expected_qualified_after_typename;
149023756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          Diag(Tok.getLocation(), DiagID);
149123756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          return false;
149223756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith        }
149323756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith      }
149423756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith
149523756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith      Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
14969ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1497d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    }
1498d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1499d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    TypeResult Ty;
1500d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (Tok.is(tok::identifier)) {
1501d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      // FIXME: check whether the next token is '<', first!
150223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
15031a15dae8be2b28e02b6639aa92b832465c5be420Douglas Gregor                                     *Tok.getIdentifierInfo(),
1504d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     Tok.getLocation());
15051734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else if (Tok.is(tok::annot_template_id)) {
150625a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
15071734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      if (TemplateId->Kind == TNK_Function_template) {
15081734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        Diag(Tok, diag::err_typename_refers_to_non_type_template)
15091734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor          << Tok.getAnnotationRange();
15109ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
15111734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      }
1512d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
15135354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer      ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1514a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                         TemplateId->NumArgs);
151566581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara
1516a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
151766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                                     TemplateId->TemplateKWLoc,
1518a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->Template,
1519a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->TemplateNameLoc,
1520a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->LAngleLoc,
152166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                                     TemplateArgsPtr,
1522a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->RAngleLoc);
15231734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else {
15241734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Diag(Tok, diag::err_expected_type_name_after_typename)
15251734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        << SS.getRange();
15269ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
15271734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
15281734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
152939d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    SourceLocation EndLoc = Tok.getLastLoc();
15301734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setKind(tok::annot_typename);
1531b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
153239d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    Tok.setAnnotationEndLoc(EndLoc);
15331734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setLocation(TypenameLoc);
15341734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    PP.AnnotateCachedTokens(Tok);
15359ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1536d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1537d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1538ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // Remembers whether the token was originally a scope annotation.
15390576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1540ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall
1541eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
15424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
1543b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
15449ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1545eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
15460576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  return TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, NeedType,
15470576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                   SS, !WasScopeAnnotation);
15480576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
15490576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
15500576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \brief Try to annotate a type or scope token, having already parsed an
15510576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// optional scope specifier. \p IsNewScope should be \c true unless the scope
15520576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// specifier was extracted from an existing tok::annot_cxxscope annotation.
15530576681bac125be07f77f66b02a3dba2c3a24557Richard Smithbool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(bool EnteringContext,
15540576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       bool NeedType,
15550576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       CXXScopeSpec &SS,
15560576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       bool IsNewScope) {
1557eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::identifier)) {
1558fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain    IdentifierInfo *CorrectedII = 0;
1559608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner    // Determine whether the identifier is a type name.
1560b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
1561b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            Tok.getLocation(), getCurScope(),
15621e52dfc648ce0b25ef57ae29ef1b4337d80011efFariborz Jahanian                                            &SS, false,
15639e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            NextToken().is(tok::period),
15649e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            ParsedType(),
1565fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*IsCtorOrDtorName=*/false,
1566fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain                                            /*NonTrivialTypeSourceInfo*/true,
1567fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain                                            NeedType ? &CorrectedII : NULL)) {
1568fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain      // A FixIt was applied as a result of typo correction
1569fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain      if (CorrectedII)
1570fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain        Tok.setIdentifierInfo(CorrectedII);
1571608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // This is a typename. Replace the current token in-place with an
1572608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // annotation type token.
1573b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner      Tok.setKind(tok::annot_typename);
1574b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      setTypeAnnotation(Tok, Ty);
1575608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      Tok.setAnnotationEndLoc(Tok.getLocation());
1576608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      if (SS.isNotEmpty()) // it was a C++ qualified type name.
1577608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner        Tok.setLocation(SS.getBeginLoc());
15781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1579608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // In case the tokens were cached, have Preprocessor replace
1580608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them with the annotation token.
1581608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      PP.AnnotateCachedTokens(Tok);
15829ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
15831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
158439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
15854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus) {
1586608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // If we're in C, we can't have :: tokens at all (the lexer won't return
1587608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them).  If the identifier is not a type, then it can't be scope either,
15881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // just early exit.
1589608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      return false;
1590eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    }
15911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
159239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // If this is a template-id, annotate with a template-id or type token.
159355f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    if (NextToken().is(tok::less)) {
15947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor      TemplateTy Template;
1595014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
1596014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
15971fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
15981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateNameKind TNK
15997c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara          = Actions.isTemplateName(getCurScope(), SS,
16007c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   /*hasTemplateKeyword=*/false, TemplateName,
1601b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   /*ObjectType=*/ ParsedType(),
1602b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   EnteringContext,
16037c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   Template, MemberOfUnknownSpecialization)) {
1604ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
1605ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
1606e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1607e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName)) {
1608c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // If an unrecoverable error occurred, we need to return true here,
1609c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // because the token stream is in a damaged state.  We may not return
1610c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // a valid identifier.
16119ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
1612c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner        }
1613ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
161455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    }
1615d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
161639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // The current token, which is either an identifier or a
161739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // template-id, is not part of the annotation. Fall through to
161839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // push that token back into the stream and complete the C++ scope
161939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // specifier annotation.
16201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1621eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
162239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  if (Tok.is(tok::annot_template_id)) {
162325a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1624c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor    if (TemplateId->Kind == TNK_Type_template) {
162539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // A template-id that refers to a type was parsed into a
162639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // template-id annotation in a context where we weren't allowed
162739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // to produce a type annotation token. Update the template-id
162839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // annotation token to a type annotation token now.
1629059101f922de6eb765601459925f4c8914420b23Douglas Gregor      AnnotateTemplateIdTokenAsType();
16309ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
163139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
163239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
1633d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
16346ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (SS.isEmpty())
16359ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
16361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16376ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // A C++ scope specifier that isn't followed by a typename.
16380576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  AnnotateScopeToken(SS, IsNewScope);
16399ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1640eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
1641eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
1642eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
164339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor/// annotates C++ scope specifiers and template-ids.  This returns
164483a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith/// true if there was an error that could not be recovered from.
16451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
164655a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
164755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1648495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
16494e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus &&
16506ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner         "Call sites of this function should be guarded by checking for C++");
16513b887354b1b667c97d070ddc67b5354353c4c07bDouglas Gregor  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
165242d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie          (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
165342d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie         Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
1654eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
16554bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  CXXScopeSpec SS;
1656b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
16579ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return true;
1658edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin  if (SS.isEmpty())
16599ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1660eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
16610576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  AnnotateScopeToken(SS, true);
16629ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1663eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
16646c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall
1665fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieubool Parser::isTokenEqualOrEqualTypo() {
1666fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  tok::TokenKind Kind = Tok.getKind();
1667fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  switch (Kind) {
1668fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  default:
1669d6c7c67313634b317a0d63c32be0511a121bb33dRichard Trieu    return false;
1670fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::ampequal:            // &=
1671fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::starequal:           // *=
1672fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::plusequal:           // +=
1673fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::minusequal:          // -=
1674fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::exclaimequal:        // !=
1675fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::slashequal:          // /=
1676fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::percentequal:        // %=
1677fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::lessequal:           // <=
1678fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::lesslessequal:       // <<=
1679fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::greaterequal:        // >=
1680fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::greatergreaterequal: // >>=
1681fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::caretequal:          // ^=
1682fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::pipeequal:           // |=
1683fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::equalequal:          // ==
1684fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu    Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
1685fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu      << getTokenSimpleSpelling(Kind)
1686fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu      << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
1687fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::equal:
1688fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu    return true;
1689fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  }
1690a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis}
1691a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
16927d100872341f233c81e1d7b72b40457e62c36862Argyrios KyrtzidisSourceLocation Parser::handleUnexpectedCodeCompletionToken() {
16937d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  assert(Tok.is(tok::code_completion));
16947d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  PrevTokLocation = Tok.getLocation();
16957d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
169623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
1697dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::FnScope) {
1698f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
16997d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
17007d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return PrevTokLocation;
1701dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1702dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1703dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::ClassScope) {
1704f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
17057d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
17067d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return PrevTokLocation;
1707dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1708dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  }
1709dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1710f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
17117d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  cutOffParsing();
17127d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  return PrevTokLocation;
1713dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor}
1714dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
17156c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// Anchor the Parser::FieldCallback vtable to this translation unit.
17166c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// We use a spurious method instead of the destructor because
17176c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// destroying FieldCallbacks can actually be slightly
17186c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// performance-sensitive.
17196c94a6d77f456f23ecd4c2061e6413786b5e6571John McCallvoid Parser::FieldCallback::_anchor() {
17206c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall}
1721f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1722f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor// Code-completion pass-through functions
1723f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1724f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteDirective(bool InConditional) {
1725f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorDirective(InConditional);
1726f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
1727f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1728f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteInConditionalExclusion() {
1729f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
1730f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
17311fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor
17321fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregorvoid Parser::CodeCompleteMacroName(bool IsDefinition) {
1733f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1734f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1735f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1736f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompletePreprocessorExpression() {
1737f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorExpression();
1738f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1739f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1740f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1741f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       MacroInfo *MacroInfo,
1742f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       unsigned ArgumentIndex) {
1743f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1744f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                                ArgumentIndex);
17451fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor}
174655817afdf9d453a443262a733f6caf6692dca118Douglas Gregor
174755817afdf9d453a443262a733f6caf6692dca118Douglas Gregorvoid Parser::CodeCompleteNaturalLanguage() {
174855817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  Actions.CodeCompleteNaturalLanguage();
174955817afdf9d453a443262a733f6caf6692dca118Douglas Gregor}
1750f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17513896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregorbool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
1752f986038beed360c031de8654cfba43a5d3184605Francois Pichet  assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
1753f986038beed360c031de8654cfba43a5d3184605Francois Pichet         "Expected '__if_exists' or '__if_not_exists'");
17543896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Result.IsIfExists = Tok.is(tok::kw___if_exists);
17553896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Result.KeywordLoc = ConsumeToken();
1756f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17574a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
17584a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
17593896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Diag(Tok, diag::err_expected_lparen_after)
17603896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor      << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
1761f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1762f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1763f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1764f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Parse nested-name-specifier.
1765efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(),
1766efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor                                 /*EnteringContext=*/false);
1767f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1768f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check nested-name specifier.
17693896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (Result.SS.isInvalid()) {
17703896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    T.skipToEnd();
1771f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1772f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1773f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1774e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  // Parse the unqualified-id.
1775e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
1776e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(),
1777e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         TemplateKWLoc, Result.Name)) {
17783896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    T.skipToEnd();
1779f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1780f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1781f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17823896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (T.consumeClose())
1783f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
17843896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
1785f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check if the symbol exists.
178665019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
178765019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor                                               Result.IsIfExists, Result.SS,
17883896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor                                               Result.Name)) {
17893896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_Exists:
17903896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
17913896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
1792f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17933896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_DoesNotExist:
17943896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
17953896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
17963896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
17973896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_Dependent:
17983896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = IEB_Dependent;
17993896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
180065019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor
180165019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  case Sema::IER_Error:
180265019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor    return true;
18033896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  }
1804f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1805f986038beed360c031de8654cfba43a5d3184605Francois Pichet  return false;
1806f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
1807f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1808563a645de82231a55e221fe655b7188bf8369662Francois Pichetvoid Parser::ParseMicrosoftIfExistsExternalDeclaration() {
18093896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  IfExistsCondition Result;
1810f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (ParseMicrosoftIfExistsCondition(Result))
1811f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1812f986038beed360c031de8654cfba43a5d3184605Francois Pichet
18133896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  BalancedDelimiterTracker Braces(*this, tok::l_brace);
18143896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (Braces.consumeOpen()) {
1815f986038beed360c031de8654cfba43a5d3184605Francois Pichet    Diag(Tok, diag::err_expected_lbrace);
1816f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1817f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1818f986038beed360c031de8654cfba43a5d3184605Francois Pichet
18193896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  switch (Result.Behavior) {
18203896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Parse:
18213896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    // Parse declarations below.
18223896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
18233896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
18243896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Dependent:
18253896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    llvm_unreachable("Cannot have a dependent external declaration");
18263896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
18273896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Skip:
18283896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Braces.skipToEnd();
1829f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1830f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1831f986038beed360c031de8654cfba43a5d3184605Francois Pichet
18323896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  // Parse the declarations.
18333896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1834f986038beed360c031de8654cfba43a5d3184605Francois Pichet    ParsedAttributesWithRange attrs(AttrFactory);
18354e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    MaybeParseCXX11Attributes(attrs);
1836f986038beed360c031de8654cfba43a5d3184605Francois Pichet    MaybeParseMicrosoftAttributes(attrs);
1837f986038beed360c031de8654cfba43a5d3184605Francois Pichet    DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
1838f986038beed360c031de8654cfba43a5d3184605Francois Pichet    if (Result && !getCurScope()->getParent())
1839f986038beed360c031de8654cfba43a5d3184605Francois Pichet      Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
18403896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  }
18413896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Braces.consumeClose();
1842f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
18436aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
18445948ae1021122164b22f74353bb7fe325a64f616Douglas GregorParser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) {
18451b257afbae854c6817f26b7d61c4fed8ff7aebadDouglas Gregor  assert(Tok.isObjCAtKeyword(tok::objc_import) &&
184665030af6526748ce11534e92f0ccefc44091ba13Douglas Gregor         "Improper start to module import");
18476aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  SourceLocation ImportLoc = ConsumeToken();
18486aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
1849cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
18503d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18513d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  // Parse the module path.
18523d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  do {
18533d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    if (!Tok.is(tok::identifier)) {
1854c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor      if (Tok.is(tok::code_completion)) {
1855c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        Actions.CodeCompleteModuleImport(ImportLoc, Path);
1856c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        ConsumeCodeCompletionToken();
1857c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        SkipUntil(tok::semi);
1858c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        return DeclGroupPtrTy();
1859c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor      }
1860c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor
18613d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      Diag(Tok, diag::err_module_expected_ident);
18623d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      SkipUntil(tok::semi);
18633d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      return DeclGroupPtrTy();
18643d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    }
18653d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18663d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    // Record this part of the module path.
18673d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
18683d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    ConsumeToken();
18693d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18703d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    if (Tok.is(tok::period)) {
18713d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      ConsumeToken();
18723d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      continue;
18733d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    }
18743d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18753d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    break;
18763d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  } while (true);
18776aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
18785948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor  DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path);
18796aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  ExpectAndConsumeSemi(diag::err_module_expected_semi);
18806aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  if (Import.isInvalid())
18816aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    return DeclGroupPtrTy();
18826aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
18836aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  return Actions.ConvertDeclToDeclGroup(Import.get());
18846aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor}
18854a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1886c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::diagnoseOverflow() {
18879e738cc9d4b4655c44dadeb22f3a314daf43b995Richard Smith  P.Diag(P.Tok, diag::err_bracket_depth_exceeded)
18889e738cc9d4b4655c44dadeb22f3a314daf43b995Richard Smith    << P.getLangOpts().BracketDepth;
18899e738cc9d4b4655c44dadeb22f3a314daf43b995Richard Smith  P.Diag(P.Tok, diag::note_bracket_depth);
1890d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.SkipUntil(tok::eof);
1891d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  return true;
18924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
18934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1894c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
18954a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            const char *Msg,
18964a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            tok::TokenKind SkipToToc ) {
18974a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  LOpen = P.Tok.getLocation();
1898d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc))
1899d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor    return true;
1900d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
1901d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  if (getDepth() < MaxDepth)
1902d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor    return false;
1903d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
1904d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  return diagnoseOverflow();
19054a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
19064a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1907c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::diagnoseMissingClose() {
1908d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
1909d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
1910d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  const char *LHSName = "unknown";
1911b031eab1c07fa2c5bd74c7e92f7c938bf3304729David Blaikie  diag::kind DID;
1912d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  switch (Close) {
1913b031eab1c07fa2c5bd74c7e92f7c938bf3304729David Blaikie  default: llvm_unreachable("Unexpected balanced token");
1914d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
1915d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
1916d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
19174a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  }
1918d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(P.Tok, DID);
1919d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(LOpen, diag::note_matching) << LHSName;
1920b578aee665aad5ed1a46a26217c730fdfbfc8c2eDavid Blaikie  if (P.SkipUntil(Close, /*StopAtSemi*/ true, /*DontConsume*/ true))
1921b578aee665aad5ed1a46a26217c730fdfbfc8c2eDavid Blaikie    LClose = P.ConsumeAnyToken();
19224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return true;
19234a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
19243896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
1925c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorvoid BalancedDelimiterTracker::skipToEnd() {
19263896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  P.SkipUntil(Close, false);
19273896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor}
1928