Parser.cpp revision 1b257afbae854c6817f26b7d61c4fed8ff7aebad
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  }
99aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
100056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko  CommentSemaHandler.reset(new ActionCommentHandler(actions));
101056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko  PP.addCommentHandler(CommentSemaHandler.get());
102aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
103f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  PP.setCodeCompletionHandler(*this);
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1063cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
10733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return Diags.Report(Loc, DiagID);
1081ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner}
1091ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner
1103cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
1111ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner  return Diag(Tok.getLocation(), DiagID);
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1144b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \brief Emits a diagnostic suggesting parentheses surrounding a
1154b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// given range.
1164b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor///
1174b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param Loc The location where we'll emit the diagnostic.
11870517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko/// \param DK The kind of diagnostic to emit.
1194b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param ParenRange Source range enclosing code that should be parenthesized.
1204b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorvoid Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
1214b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor                                SourceRange ParenRange) {
122b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
123b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
1244b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // We can't display the parentheses, so just dig the
1254b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // warning/error and return.
1264b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Loc, DK);
1274b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    return;
1284b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  }
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Diag(Loc, DK)
131849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
132849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(EndLoc, ")");
1334b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor}
1344b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
135837b1a37116cf4e64f8bb7db34982dee1fba7647John McCallstatic bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
136837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  switch (ExpectedTok) {
1374b0824229b96d024a96f3c7dd75ab70652c05c5bRichard Smith  case tok::semi:
1384b0824229b96d024a96f3c7dd75ab70652c05c5bRichard Smith    return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ;
139837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  default: return false;
140837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  }
141837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall}
142837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// input.  If so, it is consumed and false is returned.
1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If the input is malformed, this emits the specified diagnostic.  Next, if
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipToTok is specified, it calls SkipUntil(SkipToTok).  Finally, true is
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// returned.
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const char *Msg, tok::TokenKind SkipToTok) {
151dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeAnyToken();
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
155a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
156837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  // Detect common single-character typos and resume.
157837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  if (IsCommonTypo(ExpectedTok, Tok)) {
158837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    SourceLocation Loc = Tok.getLocation();
159837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    Diag(Loc, DiagID)
160837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall      << Msg
161837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall      << FixItHint::CreateReplacement(SourceRange(Loc),
162837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall                                      getTokenSimpleSpelling(ExpectedTok));
163837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    ConsumeAnyToken();
164837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
165837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    // Pretend there wasn't a problem.
166837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall    return false;
167837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall  }
168837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall
1694b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  const char *Spelling = 0;
170b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (EndLoc.isValid() &&
172b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor      (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
1734b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    // Show what code to insert to fix this problem.
1741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(EndLoc, DiagID)
1754b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor      << Msg
176849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateInsertion(EndLoc, Spelling);
1774b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor  } else
1784b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor    Diag(Tok, DiagID) << Msg;
1794b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (SkipToTok != tok::unknown)
1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(SkipToTok);
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1859ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregorbool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
1869ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
187fb5825dca4e95fee463fdeaddb8b729294fb4d10Douglas Gregor    ConsumeToken();
1889ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    return false;
1899ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  }
1909ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
1919ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
1929ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      NextToken().is(tok::semi)) {
1939ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    Diag(Tok, diag::err_extraneous_token_before_semi)
1949ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      << PP.getSpelling(Tok)
1959ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor      << FixItHint::CreateRemoval(Tok.getLocation());
1969ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeAnyToken(); // The ')' or ']'.
1979ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    ConsumeToken(); // The ';'.
1989ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor    return false;
1999ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  }
2009ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
2019ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  return ExpectAndConsume(tok::semi, DiagID);
2029ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor}
2039ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor
204eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smithvoid Parser::ConsumeExtraSemi(ExtraSemiKind Kind, unsigned TST) {
2054b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  if (!Tok.is(tok::semi)) return;
2064b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
207eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  bool HadMultipleSemis = false;
2084b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  SourceLocation StartLoc = Tok.getLocation();
2094b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  SourceLocation EndLoc = Tok.getLocation();
2104b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  ConsumeToken();
2114b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
2124b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) {
213eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    HadMultipleSemis = true;
2144b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    EndLoc = Tok.getLocation();
2154b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    ConsumeToken();
2164b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  }
2174b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
218eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  // C++11 allows extra semicolons at namespace scope, but not in any of the
219eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  // other contexts.
220eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  if (Kind == OutsideFunction && getLangOpts().CPlusPlus) {
221eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    if (getLangOpts().CPlusPlus0x)
222eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith      Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi)
223eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith          << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
224eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    else
225eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith      Diag(StartLoc, diag::ext_extra_semi_cxx11)
226eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith          << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
2274b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    return;
2284b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu  }
2294b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
230eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis)
231eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    Diag(StartLoc, diag::ext_extra_semi)
232eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith        << Kind << DeclSpec::getSpecifierName((DeclSpec::TST)TST)
233eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith        << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
234eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith  else
235eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    // A single semicolon is valid after a member function definition.
236eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith    Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def)
237eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith      << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
2384b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu}
2394b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu
2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Error recovery.
2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipUntil - Read tokens until we get to the specified token, then consume
245012cf464254804279efa84e21b4b493dde76c5f1Chris Lattner/// it (unless DontConsume is true).  Because we cannot guarantee that the
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// token will ever occur, this skips to the next token, or to some likely
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// good stopping point.  If StopAtSemi is true, skipping will stop at a ';'
2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character.
249a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///
2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If SkipUntil finds the specified token, it returns true, otherwise it
251a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// returns false.
252eb52f86a62db523e3c993686b3ed92c55d59d53cDavid Blaikiebool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, bool StopAtSemi,
253eb52f86a62db523e3c993686b3ed92c55d59d53cDavid Blaikie                       bool DontConsume, bool StopAtCodeCompletion) {
2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We always want this function to skip at least one token if the first token
2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // isn't T and if not at EOF.
2565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isFirstTokenSkipped = true;
2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we found one of the tokens, stop and return true.
259eb52f86a62db523e3c993686b3ed92c55d59d53cDavid Blaikie    for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) {
260000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(Toks[i])) {
2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (DontConsume) {
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Noop, don't consume the token.
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        } else {
2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ConsumeAnyToken();
2655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return true;
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
269a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Tok.getKind()) {
2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::eof:
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ran out of tokens.
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
274dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
275dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    case tok::code_completion:
2763437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      if (!StopAtCodeCompletion)
2773437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis        ConsumeToken();
278dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      return false;
279dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_paren:
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested parens.
2825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
2833437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion);
2845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_square:
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested square brackets.
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
2883437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_square, false, false, StopAtCodeCompletion);
2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::l_brace:
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Recursively skip properly-nested braces.
2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
2933437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis      SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion);
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
295a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Since the user wasn't looking for this token (if they were, it would
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // already be handled), this isn't balanced.  If there is a LHS token at a
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // higher level, we will assume that this matches the unbalanced token
3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // and return it.  Otherwise, this is a spurious RHS token, which we skip.
3015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_paren:
3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ParenCount && !isFirstTokenSkipped)
3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
3045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeParen();
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_square:
3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BracketCount && !isFirstTokenSkipped)
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBracket();
3105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::r_brace:
3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (BraceCount && !isFirstTokenSkipped)
3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;  // Matches something.
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeBrace();
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
316a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
3175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::string_literal:
3185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::wide_string_literal:
3195cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf8_string_literal:
3205cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf16_string_literal:
3215cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    case tok::utf32_string_literal:
3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeStringToken();
3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
32455edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian
3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case tok::semi:
3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (StopAtSemi)
3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip this token.
3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    isFirstTokenSkipped = false;
335a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump  }
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Scope manipulation
3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EnterScope - Start a new scope.
3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::EnterScope(unsigned ScopeFlags) {
3449e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes) {
3459e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    Scope *N = ScopeCache[--NumCachedScopes];
34623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    N->Init(getCurScope(), ScopeFlags);
34723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CurScope = N;
3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
3499c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis    Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
3505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExitScope - Pop a scope off the scope stack.
3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ExitScope() {
35523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() && "Scope imbalance!");
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
35790ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // Inform the actions module that this scope is going away if there are any
35890ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner  // decls in it.
35923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!getCurScope()->decl_empty())
36023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
361a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
36223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Scope *OldScope = getCurScope();
36323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = OldScope->getParent();
364a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
3659e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  if (NumCachedScopes == ScopeCacheSize)
3669e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete OldScope;
3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
3689e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    ScopeCache[NumCachedScopes++] = OldScope;
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3717a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
3727a614d8380297fcd2bc23986241905d97222948cRichard Smith/// this object does nothing.
3737a614d8380297fcd2bc23986241905d97222948cRichard SmithParser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
3747a614d8380297fcd2bc23986241905d97222948cRichard Smith                                 bool ManageFlags)
3757a614d8380297fcd2bc23986241905d97222948cRichard Smith  : CurScope(ManageFlags ? Self->getCurScope() : 0) {
3767a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (CurScope) {
3777a614d8380297fcd2bc23986241905d97222948cRichard Smith    OldFlags = CurScope->getFlags();
3787a614d8380297fcd2bc23986241905d97222948cRichard Smith    CurScope->setFlags(ScopeFlags);
3797a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
3807a614d8380297fcd2bc23986241905d97222948cRichard Smith}
3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3827a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Restore the flags for the current scope to what they were before this
3837a614d8380297fcd2bc23986241905d97222948cRichard Smith/// object overrode them.
3847a614d8380297fcd2bc23986241905d97222948cRichard SmithParser::ParseScopeFlags::~ParseScopeFlags() {
3857a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (CurScope)
3867a614d8380297fcd2bc23986241905d97222948cRichard Smith    CurScope->setFlags(OldFlags);
3877a614d8380297fcd2bc23986241905d97222948cRichard Smith}
3885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// C99 6.9: External Definitions.
3925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerParser::~Parser() {
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we still have scopes active, delete the scope tree.
39623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  delete getCurScope();
39723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.CurScope = 0;
39823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Free the scope cache.
4009e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner  for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
4019e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner    delete ScopeCache[i];
402fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar
4038387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Free LateParsedTemplatedFunction nodes.
4048387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin();
4058387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      it != LateParsedTemplateMap.end(); ++it)
4068387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    delete it->second;
4078387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
408fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar  // Remove the pragma handlers we installed.
409cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  PP.RemovePragmaHandler(AlignHandler.get());
410cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar  AlignHandler.reset();
411aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
412aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  GCCVisibilityHandler.reset();
4139b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(OptionsHandler.get());
414861800c676004eabed5927f0552620d06c80a40aDaniel Dunbar  OptionsHandler.reset();
4159b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(PackHandler.get());
4164726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  PackHandler.reset();
41762c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  PP.RemovePragmaHandler(MSStructHandler.get());
41862c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructHandler.reset();
4199b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(UnusedHandler.get());
4204726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  UnusedHandler.reset();
4219b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  PP.RemovePragmaHandler(WeakHandler.get());
4229991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman  WeakHandler.reset();
4235f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall  PP.RemovePragmaHandler(RedefineExtnameHandler.get());
4245f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall  RedefineExtnameHandler.reset();
425f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
4264e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().OpenCL) {
427f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
428f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    OpenCLExtensionHandler.reset();
429f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
430f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
431f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
432321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  PP.RemovePragmaHandler("STDC", FPContractHandler.get());
433321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  FPContractHandler.reset();
434aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
435056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko  PP.removeCommentHandler(CommentSemaHandler.get());
436aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
437f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  PP.clearCodeCompletionHandler();
43813bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer
43913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  assert(TemplateIds.empty() && "Still alive TemplateIdAnnotations around?");
4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Initialize - Warm up the parser.
4435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::Initialize() {
44531e057270232c1c37602579cb6461c2704175672Chris Lattner  // Create the translation unit scope.  Install it as the current scope.
44623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() == 0 && "A scope is already active?");
44731e057270232c1c37602579cb6461c2704175672Chris Lattner  EnterScope(Scope::DeclScope);
448c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor  Actions.ActOnTranslationUnitScope(getCurScope());
449c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor
45034870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  // Initialization for Objective-C context sensitive keywords recognition.
451a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  // Referenced in Parser::ParseObjCTypeQualifierList.
4524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC1) {
453a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
454a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
455a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
456a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
457a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
458a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
45934870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner  }
460662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar
461e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  Ident_instancetype = 0;
4627eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson  Ident_final = 0;
4637eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson  Ident_override = 0;
4641f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson
465662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar  Ident_super = &PP.getIdentifierTable().get("super");
46682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson
4674e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().AltiVec) {
46882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_vector = &PP.getIdentifierTable().get("vector");
46982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Ident_pixel = &PP.getIdentifierTable().get("pixel");
47082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  }
4710a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4720a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_introduced = 0;
4730a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_deprecated = 0;
4740a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  Ident_obsoleted = 0;
475b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  Ident_unavailable = 0;
47628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
477b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor  Ident__except = 0;
478b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor
47928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0;
48028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0;
48128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0;
48228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
4834e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if(getLangOpts().Borland) {
48428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__exception_info        = PP.getIdentifierInfo("_exception_info");
48528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___exception_info       = PP.getIdentifierInfo("__exception_info");
48628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_GetExceptionInfo       = PP.getIdentifierInfo("GetExceptionInformation");
48728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__exception_code        = PP.getIdentifierInfo("_exception_code");
48828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___exception_code       = PP.getIdentifierInfo("__exception_code");
48928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_GetExceptionCode       = PP.getIdentifierInfo("GetExceptionCode");
49028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident__abnormal_termination  = PP.getIdentifierInfo("_abnormal_termination");
49128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
49228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    Ident_AbnormalTermination    = PP.getIdentifierInfo("AbnormalTermination");
49328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
49428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
49528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
49628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
49728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
49828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
49928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
50028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
50128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
50228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
50328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
504c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor
505c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor  Actions.Initialize();
506c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor
507c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor  // Prime the lexer look-ahead.
508c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor  ConsumeToken();
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
51113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramernamespace {
51213bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  /// \brief RAIIObject to destroy the contents of a SmallVector of
51313bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  /// TemplateIdAnnotation pointers and clear the vector.
51413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  class DestroyTemplateIdAnnotationsRAIIObj {
51513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    SmallVectorImpl<TemplateIdAnnotation *> &Container;
51613bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  public:
51713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    DestroyTemplateIdAnnotationsRAIIObj(SmallVectorImpl<TemplateIdAnnotation *>
51813bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer                                       &Container)
51913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      : Container(Container) {}
52013bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer
52113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    ~DestroyTemplateIdAnnotationsRAIIObj() {
52213bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      for (SmallVectorImpl<TemplateIdAnnotation *>::iterator I =
52313bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer           Container.begin(), E = Container.end();
52413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer           I != E; ++I)
52513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer        (*I)->Destroy();
52613bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      Container.clear();
52713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    }
52813bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  };
52913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer}
53013bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// action tells us to.  This returns true if the EOF was encountered.
533682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattnerbool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
53413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
535b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
536e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  // Skip over the EOF token, flagging end of previous input for incremental
537e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  // processing
538e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof))
539e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    ConsumeToken();
540e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
541b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  while (Tok.is(tok::annot_pragma_unused))
542b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis    HandlePragmaUnused();
543b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
544682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  Result = DeclGroupPtrTy();
5459299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  if (Tok.is(tok::eof)) {
5468387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    // Late template parsing can begin.
5474e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().DelayedTemplateParsing)
5488387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
549e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    if (!PP.isIncrementalProcessingEnabled())
550e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann      Actions.ActOnEndOfTranslationUnit();
551e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    //else don't tell Sema that we ended parsing: more input might come.
5528387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
5539299f3fa85796613cc787a2062c9562d07c8613eChris Lattner    return true;
5549299f3fa85796613cc787a2062c9562d07c8613eChris Lattner  }
555a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
5560b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributesWithRange attrs(AttrFactory);
5577f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseCXX0XAttributes(attrs);
5587f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseMicrosoftAttributes(attrs);
559e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
5607f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  Result = ParseExternalDeclaration(attrs);
5615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTranslationUnit:
5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       translation-unit: [C99 6.9]
566a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///         external-declaration
567a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump///         translation-unit external-declaration
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseTranslationUnit() {
5698935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  Initialize();
570a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
571682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  DeclGroupPtrTy Res;
57289307ffaf8acf4d6fdffd72b607ca4fbcfdffc9dSteve Naroff  while (!ParseTopLevelDecl(Res))
5735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /*parse them all*/;
5741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57506f548596beef4c0a227a45cba996497f99566c0Chris Lattner  ExitScope();
57623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  assert(getCurScope() == 0 && "Scope imbalance!");
5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseExternalDeclaration:
58090b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner///
581c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
582c3018153a11afe91849748a93d920040a571b76cChris Lattner///         function-definition
583c3018153a11afe91849748a93d920040a571b76cChris Lattner///         declaration
584a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration
5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU]   asm-definition
586c3018153a11afe91849748a93d920040a571b76cChris Lattner/// [GNU]   __extension__ external-declaration
5875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
5885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-declaration
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-alias-declaration
5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-protocol-definition
5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-method-definition
5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  @end
593c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor/// [C++]   linkage-specification
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-definition:
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         simple-asm-expr ';'
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
597a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration:
598a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///           ';'
599a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///
60045f965581935791a018df829a14dff53c1dd8f47Douglas Gregor/// [C++0x/GNU] 'extern' 'template' declaration
6017f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::DeclGroupPtrTy
6027f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
6037f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                 ParsingDeclSpec *DS) {
60413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
60536d36806f1972f7ec1d2a3f59155187278c56508Argyrios Kyrtzidis  ParenBraceBracketBalancer BalancerRAIIObj(*this);
6067d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
6077d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  if (PP.isCodeCompletionReached()) {
6087d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
6097d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
6107d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  }
6117d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
612d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *SingleDecl = 0;
6135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Tok.getKind()) {
614426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola  case tok::annot_pragma_vis:
615426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola    HandlePragmaVisibility();
616426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola    return DeclGroupPtrTy();
617aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman  case tok::annot_pragma_pack:
618aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman    HandlePragmaPack();
619aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman    return DeclGroupPtrTy();
6209595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_msstruct:
6219595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaMSStruct();
6229595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6239595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_align:
6249595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaAlign();
6259595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6269595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_weak:
6279595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaWeak();
6289595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6299595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_weakalias:
6309595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaWeakAlias();
6319595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6329595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_redefine_extname:
6339595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaRedefineExtname();
6349595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6359595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_fp_contract:
6369595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaFPContract();
6379595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6389595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_opencl_extension:
6399595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaOpenCLExtension();
6409595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::semi:
6424b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    ConsumeExtraSemi(OutsideFunction);
6435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TODO: Invoke action for top-level semicolon.
644682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
64590b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::r_brace:
646883692ebd421c40b44e2c2665e5f54dade5621bcNico Weber    Diag(Tok, diag::err_extraneous_closing_brace);
64790b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    ConsumeBrace();
648682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
64990b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::eof:
65090b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    Diag(Tok, diag::err_expected_external_declaration);
651682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
652c3018153a11afe91849748a93d920040a571b76cChris Lattner  case tok::kw___extension__: {
653c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    // __extension__ silences extension warnings in the subexpression.
654c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
65539146d6497ad5e7ca8ef639221e7b3e15d07c888Chris Lattner    ConsumeToken();
6567f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    return ParseExternalDeclaration(attrs);
657c3018153a11afe91849748a93d920040a571b76cChris Lattner  }
658dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  case tok::kw_asm: {
6597f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ProhibitAttributes(attrs);
660bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
66121e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation StartLoc = Tok.getLocation();
66221e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation EndLoc;
66321e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    ExprResult Result(ParseSimpleAsm(&EndLoc));
664a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
6653f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson    ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
6663f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson                     "top-level asm block");
667dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson
668682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    if (Result.isInvalid())
669682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
67021e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
671682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
672dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  }
6735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::at:
67495ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    return ParseObjCAtDirectives();
6755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::minus:
6765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::plus:
6774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().ObjC1) {
678682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      Diag(Tok, diag::err_expected_external_declaration);
679682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      ConsumeToken();
680682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
681682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    }
682682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = ParseObjCMethodDefinition();
683682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
684791215b7a24666912c0b71175d2ca5ba082f666eDouglas Gregor  case tok::code_completion:
68523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
686849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                             CurParsedObjCImpl? Sema::PCC_ObjCImplementation
687f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                              : Sema::PCC_Namespace);
6887d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
6897d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
690f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  case tok::kw_using:
6918f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  case tok::kw_namespace:
6925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_typedef:
693adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_template:
694adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_export:    // As in 'export template'
695511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  case tok::kw_static_assert:
696c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne  case tok::kw__Static_assert:
69726d6023cb0d343bf8fc8836f97d39709bbd4afa0Chad Rosier    // A function definition cannot start with any of these keywords.
69897144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    {
69997144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner      SourceLocation DeclEnd;
7004e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer      StmtVector Stmts;
7017f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
70297144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    }
703d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
7047306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_static:
7057306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // Parse (then ignore) 'static' prior to a template instantiation. This is
7067306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // a GCC extension that we intentionally do not support.
7074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
7087306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
7097306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        << 0;
710d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl      SourceLocation DeclEnd;
7114e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer      StmtVector Stmts;
7127f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
7137306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    }
7147306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    goto dont_know;
7157306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
7167306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_inline:
7174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus) {
7187306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      tok::TokenKind NextKind = NextToken().getKind();
7197306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
7207306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Inline namespaces. Allowed as an extension even in C++03.
7217306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_namespace) {
7227306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
7234e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer        StmtVector Stmts;
7247f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
7257306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
7267306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
7277306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Parse (then ignore) 'inline' prior to a template instantiation. This is
7287306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // a GCC extension that we intentionally do not support.
7297306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_template) {
7307306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
7317306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor          << 1;
7327306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
7334e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer        StmtVector Stmts;
7347f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
7357306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
736d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    }
737d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
738d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
73945f965581935791a018df829a14dff53c1dd8f47Douglas Gregor  case tok::kw_extern:
7404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
74145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      // Extern templates
74245f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation ExternLoc = ConsumeToken();
74345f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation TemplateLoc = ConsumeToken();
7444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      Diag(ExternLoc, getLangOpts().CPlusPlus0x ?
7459324583ad2afd09db8c9967cd05c4fa44bac9555Richard Smith             diag::warn_cxx98_compat_extern_template :
7469324583ad2afd09db8c9967cd05c4fa44bac9555Richard Smith             diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
74745f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation DeclEnd;
74845f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      return Actions.ConvertDeclToDeclGroup(
7499241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis                  ParseExplicitInstantiation(Declarator::FileContext,
7509241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis                                             ExternLoc, TemplateLoc, DeclEnd));
75145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    }
75245f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    // FIXME: Detect C++ linkage specifications here?
753d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
755f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_exists:
756f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_not_exists:
757563a645de82231a55e221fe655b7188bf8369662Francois Pichet    ParseMicrosoftIfExistsExternalDeclaration();
758f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return DeclGroupPtrTy();
7596aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
761d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl  dont_know:
7625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // We can't tell whether this is a function-definition or declaration yet.
7637f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (DS) {
7642edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt      return ParseDeclarationOrFunctionDefinition(attrs, DS);
7657f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    } else {
7667f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclarationOrFunctionDefinition(attrs);
7677f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    }
7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
770682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // This routine returns a DeclGroup, if the thing we parsed only contains a
771682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // single decl, convert it now.
772682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  return Actions.ConvertDeclToDeclGroup(SingleDecl);
7735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7751426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
7761426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, continues a declaration or declaration list.
777e4246a633b13197634225971b25df0cbdcec0c5dSean Huntbool Parser::isDeclarationAfterDeclarator() {
778e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  // Check for '= delete' or '= default'
7794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
780e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
781e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
782e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      return false;
783e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
7846c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian
7851426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor  return Tok.is(tok::equal) ||      // int X()=  -> not a function def
7861426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::comma) ||           // int X(),  -> not a function def
7871426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::semi)  ||           // int X();  -> not a function def
7881426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
7891426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
7904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    (getLangOpts().CPlusPlus &&
79139700f81c5b42e6be93be10275602915f872fc86Fariborz Jahanian     Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
7921426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
7931426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
7941426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
7951426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, indicates the start of a function definition.
796004659a56916f2f81ede507c12516c146d6c0df3Chris Lattnerbool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
797075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
7985d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  if (Tok.is(tok::l_brace))   // int X() {}
7995d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner    return true;
8005d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner
801004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  // Handle K&R C argument lists: int X(f) int f; {}
8024e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CPlusPlus &&
803075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      Declarator.getFunctionTypeInfo().isKNRPrototype())
804004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner    return isDeclarationSpecifier();
805e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
8064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
807e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
808e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
809e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
810004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner
8115d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
8125d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner         Tok.is(tok::kw_try);          // X() try { ... }
8131426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
8141426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
8155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// a declaration.  We can't tell which we have until we read up to the
817c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// compound-statement in function-definition. TemplateParams, if
818c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// non-NULL, provides the template parameters when we're parsing a
8191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ template-declaration.
8205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       function-definition: [C99 6.9.1]
822a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
823a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
824a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
825a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///
8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       declaration: [C99 6.7]
827697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner///         declaration-specifiers init-declarator-list[opt] ';'
828697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner/// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OMP]   threadprivate-directive                              [TODO]
8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
831682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerParser::DeclGroupPtrTy
8322edf0a2520313cde900799b1eb9bd11c9c776afeSean HuntParser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs,
8332edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                       ParsingDeclSpec &DS,
8342edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                       AccessSpecifier AS) {
8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Parse the common declaration-specifiers piece.
8360efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
837a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
8395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // declaration-specifiers init-declarator-list[opt] ';'
840000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(tok::semi)) {
8412edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ProhibitAttributes(attrs);
8425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
843d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
84454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.complete(TheDecl);
845682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
847a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8482edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  DS.takeAttributesFrom(attrs);
8492edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
850246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // ObjC2 allows prefix attributes on class interfaces and protocols.
851246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // FIXME: This still needs better diagnostics. We should only accept
852246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // attributes here, no types, etc.
8534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC2 && Tok.is(tok::at)) {
854dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation AtLoc = ConsumeToken(); // the "@"
8551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
856246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar        !Tok.isObjCAtKeyword(tok::objc_protocol)) {
857246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar      Diag(Tok, diag::err_objc_unexpected_attr);
858cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      SkipUntil(tok::semi); // FIXME: better skip?
859682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
860cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    }
861d8ac05753dc4506224d445ff98399c01da3136e5John McCall
86254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.abort();
86354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
8640de2ae28c603322f05e2d9200c7d457c8b928983Fariborz Jahanian    const char *PrevSpec = 0;
865fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
866fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
867fec54013fcd0eb72642741584ca04c1bc292bef8John McCall      Diag(AtLoc, DiagID) << PrevSpec;
8681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
869246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar    if (Tok.isObjCAtKeyword(tok::objc_protocol))
870bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor      return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
871bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor
872bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return Actions.ConvertDeclToDeclGroup(
873bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor            ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
874dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
875a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
876c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // If the declspec consisted only of 'extern' and we have a string
877c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // literal following it, this must be a C++ linkage specifier like
878c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // 'extern "C"'.
8794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Tok.is(tok::string_literal) && getLangOpts().CPlusPlus &&
880c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner      DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
881682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
882d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
883682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
884682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  }
885c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
886d8ac05753dc4506224d445ff98399c01da3136e5John McCall  return ParseDeclGroup(DS, Declarator::FileContext, true);
8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8893acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::DeclGroupPtrTy
8902edf0a2520313cde900799b1eb9bd11c9c776afeSean HuntParser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs,
8912edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                             ParsingDeclSpec *DS,
8923acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian                                             AccessSpecifier AS) {
8932edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  if (DS) {
8942edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    return ParseDeclOrFunctionDefInternal(attrs, *DS, AS);
8952edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  } else {
8962edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ParsingDeclSpec PDS(*this);
8972edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // Must temporarily exit the objective-c container scope for
8982edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // parsing c constructs and re-enter objc container scope
8992edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // afterwards.
9002edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ObjCDeclContextSwitch ObjCDC(*this);
9012edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
9022edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    return ParseDeclOrFunctionDefInternal(attrs, PDS, AS);
9032edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  }
9043acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian}
9053acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian
9065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseFunctionDefinition - We parsed and verified that the specified
9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Declarator is well formed.  If this is a K&R-style function, read the
9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// parameters declaration-list, then start the compound-statement.
9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
910a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///       function-definition: [C99 6.9.1]
911a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
912a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
913a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
9147ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
91523c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
91623c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         function-body
9177ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
918d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl///         decl-specifier-seq[opt] declarator function-try-block
9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
920d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
921c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins                                      const ParsedTemplateInfo &TemplateInfo,
922c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins                                      LateParsedAttrList *LateParsedAttrs) {
92328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // Poison the SEH identifiers so they are flagged as illegal in function bodies
92428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
925075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
926a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
927a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // If this is C90 and the declspecs were completely missing, fudge in an
928a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // implicit int.  We do this here because this is the only place where
929a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // declaration-specifiers are completely optional in the grammar.
9304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
931a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner    const char *PrevSpec;
932fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
93331c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner    D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
93431c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner                                           D.getIdentifierLoc(),
935fec54013fcd0eb72642741584ca04c1bc292bef8John McCall                                           PrevSpec, DiagID);
936ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
937a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  }
938a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
9395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this declaration was formed with a K&R-style identifier list for the
9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // arguments, parse declarations for all of the args next.
9415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // int foo(a,b) int a; float b; {}
942004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  if (FTI.isKNRPrototype())
9435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseKNRParamDeclarations(D);
9445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9457ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // We should have either an opening brace or, in a C++ constructor,
9467ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // we may have a colon.
947758afbcc86ef15f8d433f5f87db1495e50effeb3Douglas Gregor  if (Tok.isNot(tok::l_brace) &&
9484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      (!getLangOpts().CPlusPlus ||
949cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt       (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
950cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        Tok.isNot(tok::equal)))) {
9515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::err_expected_fn_body);
9525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
9545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(tok::l_brace, true, true);
955a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we didn't find the '{', bail out.
957000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.isNot(tok::l_brace))
958d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
9595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
960a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
961c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // Check to make sure that any normal attributes are allowed to be on
962c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // a definition.  Late parsed attributes are checked at the end.
963c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  if (Tok.isNot(tok::equal)) {
964c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    AttributeList *DtorAttrs = D.getAttributes();
965c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    while (DtorAttrs) {
966c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins      if (!IsThreadSafetyAttribute(DtorAttrs->getName()->getName())) {
967c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins        Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition)
968c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins          << DtorAttrs->getName()->getName();
969c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins      }
970c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins      DtorAttrs = DtorAttrs->getNext();
971c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    }
972c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  }
973c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins
9748387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // In delayed template parsing mode, for function template we consume the
9758387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // tokens and store them for late parsing at the end of the translation unit.
9764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().DelayedTemplateParsing &&
9770963017dcbc32176c79a251c3ab23bc35ac784e5Douglas Gregor      Tok.isNot(tok::equal) &&
9788387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      TemplateInfo.Kind == ParsedTemplateInfo::Template) {
9795354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
9808387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9818387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
9828387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Scope *ParentScope = getCurScope()->getParent();
9838387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
98445fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor    D.setFunctionDefinitionKind(FDK_Definition);
9858387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Decl *DP = Actions.HandleDeclarator(ParentScope, D,
9863fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                        TemplateParameterLists);
9878387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.complete(DP);
9888387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.getMutableDeclSpec().abort();
9898387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9908387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (DP) {
991e1fca502e7f1349e9b4520a4ca9a02413bcf2b14Francois Pichet      LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(DP);
9928387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9938387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      FunctionDecl *FnD = 0;
9948387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
9958387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = FunTmpl->getTemplatedDecl();
9968387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      else
9978387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = cast<FunctionDecl>(DP);
998d4a0caf78e7c18e7aca65fbfd799a6c024ff51fbFrancois Pichet      Actions.CheckForFunctionRedefinition(FnD);
9998387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
10008387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateParsedTemplateMap[FnD] = LPT;
10018387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.MarkAsLateParsedTemplate(FnD);
10028387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(LPT->Toks);
10038387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    } else {
10048387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      CachedTokens Toks;
10058387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(Toks);
10068387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    }
10078387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return DP;
10088387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
10092eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian  else if (CurParsedObjCImpl &&
10109e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian           !TemplateInfo.TemplateParams &&
10119e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian           (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
10129e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian            Tok.is(tok::colon)) &&
1013be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      Actions.CurContext->isTranslationUnit()) {
1014be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1015be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    Scope *ParentScope = getCurScope()->getParent();
1016be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian
1017be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.setFunctionDefinitionKind(FDK_Definition);
1018be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
10195354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                              MultiTemplateParamsArg());
1020be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.complete(FuncDecl);
1021be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.getMutableDeclSpec().abort();
1022be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    if (FuncDecl) {
1023be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      // Consume the tokens and store them for later parsing.
1024be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      StashAwayMethodOrFunctionBodyTokens(FuncDecl);
1025be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      CurParsedObjCImpl->HasCFunction = true;
1026be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      return FuncDecl;
1027be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    }
1028be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian  }
1029be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian
1030b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Enter a scope for the function body.
10318935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1032a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1033b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Tell the actions module that we have entered a function definition with the
1034b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // specified Declarator for the function.
1035d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Res = TemplateInfo.TemplateParams?
103623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
10375354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                              *TemplateInfo.TemplateParams, D)
103823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
1039a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
104054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclarator context before we parse the body.
104154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.complete(Res);
104254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
104354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclSpec context, too.  This const_cast is
104454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // safe because we're always the sole owner.
104554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.getMutableDeclSpec().abort();
104654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
1047cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  if (Tok.is(tok::equal)) {
10484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
1049cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ConsumeToken();
1050cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1051cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    Actions.ActOnFinishFunctionBody(Res, 0, false);
1052cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1053cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    bool Delete = false;
1054cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    SourceLocation KWLoc;
1055cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Tok.is(tok::kw_delete)) {
10564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      Diag(Tok, getLangOpts().CPlusPlus0x ?
10577fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith           diag::warn_cxx98_compat_deleted_function :
1058d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith           diag::ext_deleted_function);
1059cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1060cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      KWLoc = ConsumeToken();
1061cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Actions.SetDeclDeleted(Res, KWLoc);
1062cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Delete = true;
1063cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else if (Tok.is(tok::kw_default)) {
10644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      Diag(Tok, getLangOpts().CPlusPlus0x ?
10657fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith           diag::warn_cxx98_compat_defaulted_function :
1066d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith           diag::ext_defaulted_function);
1067cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1068cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      KWLoc = ConsumeToken();
1069cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Actions.SetDeclDefaulted(Res, KWLoc);
1070cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else {
1071cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      llvm_unreachable("function definition after = not 'delete' or 'default'");
1072cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
1073cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1074cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Tok.is(tok::comma)) {
1075cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
1076cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        << Delete;
1077cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      SkipUntil(tok::semi);
1078cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else {
1079cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1080cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                       Delete ? "delete" : "default", tok::semi);
1081cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
1082cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1083cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    return Res;
1084cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  }
1085cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1086d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (Tok.is(tok::kw_try))
1087c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor    return ParseFunctionTryBlock(Res, BodyScope);
1088d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
10897ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // If we have a colon, then we're probably parsing a C++
10907ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // ctor-initializer.
1091d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  if (Tok.is(tok::colon)) {
10927ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    ParseConstructorInitializer(Res);
1093d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall
1094d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    // Recover from error.
1095d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    if (!Tok.is(tok::l_brace)) {
1096c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor      BodyScope.Exit();
10979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.ActOnFinishFunctionBody(Res, 0);
1098d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall      return Res;
1099d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    }
1100d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  } else
1101393612e6c7727f1fee50039254d9f434364cc0b2Fariborz Jahanian    Actions.ActOnDefaultCtorInitializers(Res);
11027ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
1103c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // Late attributes are parsed in the same scope as the function body.
1104c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  if (LateParsedAttrs)
1105c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
1106c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins
1107c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor  return ParseFunctionStatementBody(Res, BodyScope);
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// types for a function with a K&R-style identifier list for arguments.
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseKNRParamDeclarations(Declarator &D) {
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We know that the top-level of this declarator is a function.
1114075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
111604421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // Enter function-declaration scope, limiting any declarators to the
111704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // function prototype scope, including parameter declarators.
11183218c4bb3b5d7250f12420de6db7ef3e3f805a75Douglas Gregor  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
111904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read all the argument declarations.
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isDeclarationSpecifier()) {
11225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SourceLocation DSStart = Tok.getLocation();
1123a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the common declaration-specifiers piece.
11250b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarationSpecifiers(DS);
1127a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: 'each declaration in the declaration list shall have at
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // least one declarator'.
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // the declarations though.  It's trivial to ignore them, really hard to do
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // anything else with them.
1133000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.is(tok::semi)) {
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DSStart, diag::err_declaration_does_not_declare_param);
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1138a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // than register.
11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        DS.getStorageClassSpec() != DeclSpec::SCS_register) {
11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getStorageClassSpecLoc(),
11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
11465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.isThreadSpecified()) {
11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getThreadSpecLoc(),
11495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
11505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1152a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the first declarator attached to this declspec.
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarator(ParmDeclarator);
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Handle the full declarator list.
11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (1) {
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If attributes are present, parse them.
11607f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      MaybeParseGNUAttributes(ParmDeclarator);
1161a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ask the actions module to compute the type for this declarator.
1163d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *Param =
116423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
11652bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff
1166a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump      if (Param &&
11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // A missing identifier has already been diagnosed.
11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ParmDeclarator.getIdentifier()) {
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Scan the argument list looking for the correct param to apply this
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // type.
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0; ; ++i) {
11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // C99 6.9.1p6: those declarators shall declare only identifiers from
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // the identifier list.
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (i == FTI.NumArgs) {
11761ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner            Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
11776898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner              << ParmDeclarator.getIdentifier();
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
1180a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Reject redefinitions of parameters.
118304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner            if (FTI.ArgInfo[i].Param) {
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(ParmDeclarator.getIdentifierLoc(),
11851ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner                   diag::err_param_redefinition)
11866898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner                 << ParmDeclarator.getIdentifier();
11875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            } else {
118804421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner              FTI.ArgInfo[i].Param = Param;
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we don't have a comma, it is either the end of the list (a ';') or
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // an error, bail out.
1197000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.isNot(tok::comma))
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
1199a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12007984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith      ParmDeclarator.clear();
12017984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith
12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Consume the comma.
12037984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith      ParmDeclarator.setCommaLoc(ConsumeToken());
1204a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Parse the next declarator.
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParseDeclarator(ParmDeclarator);
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1208a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12098bb21d32e9ccc9d9c221506dff26acafa8724ccaChris Lattner    if (ExpectAndConsumeSemi(diag::err_expected_semi_declaration)) {
12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip to end of block or statement
12115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi, true);
1212000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(tok::semi))
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ConsumeToken();
12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
12155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1216a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The actions module must verify that all arguments were declared.
121823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
12195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// allowed to be a wide string, and is not subject to character translation.
12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-string-literal:
12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         string-literal
12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
122860d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseAsmStringLiteral() {
12297f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek  switch (Tok.getKind()) {
12307f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    case tok::string_literal:
12317f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      break;
123299831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf8_string_literal:
123399831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf16_string_literal:
123499831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf32_string_literal:
12357f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    case tok::wide_string_literal: {
12367f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      SourceLocation L = Tok.getLocation();
12377f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      Diag(Tok, diag::err_asm_operand_wide_string_literal)
123899831e4677a7e2e051af636221694d60ba31fcdbRichard Smith        << (Tok.getKind() == tok::wide_string_literal)
12397f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek        << SourceRange(L, L);
12407f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      return ExprError();
12417f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    }
12427f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    default:
124397f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs      Diag(Tok, diag::err_expected_string_literal)
124497f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs        << /*Source='in...'*/0 << "'asm'";
12457f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      return ExprError();
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1247a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
124899831e4677a7e2e051af636221694d60ba31fcdbRichard Smith  return ParseStringLiteralExpression();
12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseSimpleAsm
12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
12535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] simple-asm-expr:
12545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'asm' '(' asm-string-literal ')'
12555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
125660d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
1257000732226610650837478cba97843d19b75f648eChris Lattner  assert(Tok.is(tok::kw_asm) && "Not an asm!");
1258dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  SourceLocation Loc = ConsumeToken();
1259a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12607a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  if (Tok.is(tok::kw_volatile)) {
1261841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    // Remove from the end of 'asm' to the end of 'volatile'.
1262841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1263841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall                             PP.getLocForEndOfToken(Tok.getLocation()));
1264841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall
1265841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    Diag(Tok, diag::warn_file_asm_volatile)
1266849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateRemoval(RemovalRange);
12677a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall    ConsumeToken();
12687a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  }
12697a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall
12704a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
12714a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
12721ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "asm";
127361364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl    return ExprError();
12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1275a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
127660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(ParseAsmStringLiteral());
1277a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1278ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Result.isInvalid()) {
1279ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SkipUntil(tok::r_paren, true, true);
1280ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1281ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Tok.getLocation();
1282ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    ConsumeAnyToken();
1283ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  } else {
12844a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    // Close the paren and get the location of the end bracket
12854a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
1286ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
12874a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      *EndLoc = T.getCloseLocation();
1288ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
1289a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12903fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
12915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
129325a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// \brief Get the TemplateIdAnnotation from the token and put it in the
129425a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// cleanup pool so that it gets destroyed when parsing the current top level
129525a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// declaration is finished.
129625a767651d14db87aa03dd5fe3e011d877dd4100Argyrios KyrtzidisTemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
129725a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  assert(tok.is(tok::annot_template_id) && "Expected template-id token");
129825a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  TemplateIdAnnotation *
129925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
130025a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  return Id;
130125a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis}
130225a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis
13030576681bac125be07f77f66b02a3dba2c3a24557Richard Smithvoid Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {
13040576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Push the current token back into the token stream (or revert it if it is
13050576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // cached) and use an annotation scope token for current token.
13060576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (PP.isBacktrackEnabled())
13070576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.RevertCachedTokens(1);
13080576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  else
13090576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.EnterToken(Tok);
13100576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setKind(tok::annot_cxxscope);
13110576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
13120576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setAnnotationRange(SS.getRange());
13130576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13140576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // In case the tokens were cached, have Preprocessor replace them
13150576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // with the annotation token.  We don't need to do this if we've
13160576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // just reverted back to a prior state.
13170576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (IsNewAnnotation)
13180576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
13190576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
13200576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13210576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \brief Attempt to classify the name at the current token position. This may
13220576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// form a type, scope or primary expression annotation, or replace the token
13230576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// with a typo-corrected keyword. This is only appropriate when the current
13240576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// name must refer to an entity which has already been declared.
13250576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///
13260576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \param IsAddressOfOperand Must be \c true if the name is preceded by an '&'
13270576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///        and might possibly have a dependent nested name specifier.
13280576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \param CCC Indicates how to perform typo-correction for this name. If NULL,
13290576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///        no typo correction will be performed.
13300576681bac125be07f77f66b02a3dba2c3a24557Richard SmithParser::AnnotatedNameKind
13310576681bac125be07f77f66b02a3dba2c3a24557Richard SmithParser::TryAnnotateName(bool IsAddressOfOperand,
13320576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                        CorrectionCandidateCallback *CCC) {
13330576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));
13340576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13350576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  const bool EnteringContext = false;
13360576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
13370576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13380576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  CXXScopeSpec SS;
13390576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (getLangOpts().CPlusPlus &&
13400576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
13410576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Error;
13420576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13430576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
13440576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
13450576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                  !WasScopeAnnotation))
13460576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
13470576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Unresolved;
13480576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
13490576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13500576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  IdentifierInfo *Name = Tok.getIdentifierInfo();
13510576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  SourceLocation NameLoc = Tok.getLocation();
13520576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13530576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // FIXME: Move the tentative declaration logic into ClassifyName so we can
13540576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // typo-correct to tentatively-declared identifiers.
13550576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (isTentativelyDeclared(Name)) {
13560576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // Identifier has been tentatively declared, and thus cannot be resolved as
13570576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // an expression. Fall back to annotating it as a type.
13580576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
13590576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                  !WasScopeAnnotation))
13600576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
13610576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl;
13620576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
13630576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13640576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Token Next = NextToken();
13650576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13660576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Look up and classify the identifier. We don't perform any typo-correction
13670576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // after a scope specifier, because in general we can't recover from typos
13680576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // there (eg, after correcting 'A::tempalte B<X>::C', we would need to jump
13690576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // back into scope specifier parsing).
13700576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Sema::NameClassification Classification
13710576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next,
13720576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                           IsAddressOfOperand, SS.isEmpty() ? CCC : 0);
13730576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13740576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  switch (Classification.getKind()) {
13750576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Error:
13760576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Error;
13770576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13780576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Keyword:
13790576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // The identifier was typo-corrected to a keyword.
13800576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setIdentifierInfo(Name);
13810576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(Name->getTokenID());
13820576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.TypoCorrectToken(Tok);
13830576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
13840576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      AnnotateScopeToken(SS, !WasScopeAnnotation);
13850576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // We've "annotated" this as a keyword.
13860576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
13870576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13880576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Unknown:
13890576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // It's not something we know about. Leave it unannotated.
13900576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    break;
13910576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13920576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Type:
13930576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(tok::annot_typename);
13940576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    setTypeAnnotation(Tok, Classification.getType());
13950576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setAnnotationEndLoc(NameLoc);
13960576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
13970576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      Tok.setLocation(SS.getBeginLoc());
13980576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
13990576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
14000576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
14010576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Expression:
14020576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(tok::annot_primary_expr);
14030576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    setExprAnnotation(Tok, Classification.getExpression());
14040576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setAnnotationEndLoc(NameLoc);
14050576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
14060576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      Tok.setLocation(SS.getBeginLoc());
14070576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
14080576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
14090576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
14100576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_TypeTemplate:
14110576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (Next.isNot(tok::less)) {
14120576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      // This may be a type template being used as a template template argument.
14130576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      if (SS.isNotEmpty())
14140576681bac125be07f77f66b02a3dba2c3a24557Richard Smith        AnnotateScopeToken(SS, !WasScopeAnnotation);
14150576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_TemplateName;
14160576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    }
14170576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // Fall through.
14180576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_FunctionTemplate: {
14190576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // We have a type or function template followed by '<'.
14200576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    ConsumeToken();
14210576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    UnqualifiedId Id;
14220576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Id.setIdentifier(Name, NameLoc);
14230576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (AnnotateTemplateIdToken(
14240576681bac125be07f77f66b02a3dba2c3a24557Richard Smith            TemplateTy::make(Classification.getTemplateName()),
14250576681bac125be07f77f66b02a3dba2c3a24557Richard Smith            Classification.getTemplateNameKind(), SS, SourceLocation(), Id))
14260576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
14270576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
14280576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
14290576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
14300576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_NestedNameSpecifier:
14310576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    llvm_unreachable("already parsed nested name specifier");
14320576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
14330576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
14340576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Unable to classify the name, but maybe we can annotate a scope specifier.
14350576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (SS.isNotEmpty())
14360576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    AnnotateScopeToken(SS, !WasScopeAnnotation);
14370576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  return ANK_Unresolved;
14380576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
14390576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
1440eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateTypeOrScopeToken - If the current token position is on a
1441eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typename (possibly qualified in C++) or a C++ scope specifier not followed
1442eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1443eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// with a single annotation token representing the typename or C++ scope
1444eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// respectively.
1445eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This simplifies handling of C++ scope specifiers and allows efficient
1446eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// backtracking without the need to re-parse and resolve nested-names and
1447eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typenames.
144844802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// It will mainly be called when we expect to treat identifiers as typenames
144944802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// (if they are typenames). For example, in C we do not expect identifiers
145044802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// inside expressions to be treated as typenames so it will not be called
145144802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// for expressions in C.
145244802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// The benefit for C/ObjC is that a typename will be annotated and
1453b43a50ff1b0b171ece84425b0ad83a9a31f038faSteve Naroff/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
145444802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// will not be called twice, once to check whether we have a declaration
145544802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// specifier, and another one to get the actual type inside
145644802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// ParseDeclarationSpecifiers).
1457a7bc7c880f86bc180684ef032d06df51bcae7a23Chris Lattner///
14589ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// This returns true if an error occurred.
14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
146055a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
146155a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1462fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrainbool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
14631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
146442d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie          || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)
146523756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          || Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id))
146623756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          && "Cannot be a type or scope token!");
14671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1468d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Tok.is(tok::kw_typename)) {
1469d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Parse a C++ typename-specifier, e.g., "typename T::type".
1470d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //
1471d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //   typename-specifier:
1472d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //     'typename' '::' [opt] nested-name-specifier identifier
14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     'typename' '::' [opt] nested-name-specifier template [opt]
14741734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    //            simple-template-id
1475d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    SourceLocation TypenameLoc = ConsumeToken();
1476d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    CXXScopeSpec SS;
1477efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(),
1478efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor                                       /*EnteringContext=*/false,
14794147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                       0, /*IsTypename*/true))
14809ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
14819ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    if (!SS.isSet()) {
1482b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet      if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
1483b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet          Tok.is(tok::annot_decltype)) {
148423756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith        // Attempt to recover by skipping the invalid 'typename'
1485b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet        if (Tok.is(tok::annot_decltype) ||
1486b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet            (!TryAnnotateTypeOrScopeToken(EnteringContext, NeedType) &&
1487b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet            Tok.isAnnotation())) {
148823756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          unsigned DiagID = diag::err_expected_qualified_after_typename;
148923756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          // MS compatibility: MSVC permits using known types with typename.
149023756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          // e.g. "typedef typename T* pointer_type"
149123756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          if (getLangOpts().MicrosoftExt)
149223756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith            DiagID = diag::warn_expected_qualified_after_typename;
149323756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          Diag(Tok.getLocation(), DiagID);
149423756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          return false;
149523756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith        }
149623756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith      }
149723756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith
149823756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith      Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
14999ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1500d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    }
1501d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1502d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    TypeResult Ty;
1503d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (Tok.is(tok::identifier)) {
1504d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      // FIXME: check whether the next token is '<', first!
150523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
15061a15dae8be2b28e02b6639aa92b832465c5be420Douglas Gregor                                     *Tok.getIdentifierInfo(),
1507d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     Tok.getLocation());
15081734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else if (Tok.is(tok::annot_template_id)) {
150925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
15101734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      if (TemplateId->Kind == TNK_Function_template) {
15111734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        Diag(Tok, diag::err_typename_refers_to_non_type_template)
15121734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor          << Tok.getAnnotationRange();
15139ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
15141734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      }
1515d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
15165354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer      ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1517a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                         TemplateId->NumArgs);
151866581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara
1519a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
152066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                                     TemplateId->TemplateKWLoc,
1521a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->Template,
1522a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->TemplateNameLoc,
1523a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->LAngleLoc,
152466581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                                     TemplateArgsPtr,
1525a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->RAngleLoc);
15261734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else {
15271734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Diag(Tok, diag::err_expected_type_name_after_typename)
15281734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        << SS.getRange();
15299ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
15301734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
15311734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
153239d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    SourceLocation EndLoc = Tok.getLastLoc();
15331734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setKind(tok::annot_typename);
1534b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
153539d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    Tok.setAnnotationEndLoc(EndLoc);
15361734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setLocation(TypenameLoc);
15371734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    PP.AnnotateCachedTokens(Tok);
15389ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1539d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1540d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1541ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // Remembers whether the token was originally a scope annotation.
15420576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1543ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall
1544eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
15454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
1546b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
15479ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1548eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
15490576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  return TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, NeedType,
15500576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                   SS, !WasScopeAnnotation);
15510576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
15520576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
15530576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \brief Try to annotate a type or scope token, having already parsed an
15540576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// optional scope specifier. \p IsNewScope should be \c true unless the scope
15550576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// specifier was extracted from an existing tok::annot_cxxscope annotation.
15560576681bac125be07f77f66b02a3dba2c3a24557Richard Smithbool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(bool EnteringContext,
15570576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       bool NeedType,
15580576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       CXXScopeSpec &SS,
15590576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       bool IsNewScope) {
1560eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::identifier)) {
1561fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain    IdentifierInfo *CorrectedII = 0;
1562608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner    // Determine whether the identifier is a type name.
1563b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
1564b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            Tok.getLocation(), getCurScope(),
15651e52dfc648ce0b25ef57ae29ef1b4337d80011efFariborz Jahanian                                            &SS, false,
15669e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            NextToken().is(tok::period),
15679e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            ParsedType(),
1568fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*IsCtorOrDtorName=*/false,
1569fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain                                            /*NonTrivialTypeSourceInfo*/true,
1570fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain                                            NeedType ? &CorrectedII : NULL)) {
1571fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain      // A FixIt was applied as a result of typo correction
1572fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain      if (CorrectedII)
1573fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain        Tok.setIdentifierInfo(CorrectedII);
1574608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // This is a typename. Replace the current token in-place with an
1575608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // annotation type token.
1576b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner      Tok.setKind(tok::annot_typename);
1577b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      setTypeAnnotation(Tok, Ty);
1578608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      Tok.setAnnotationEndLoc(Tok.getLocation());
1579608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      if (SS.isNotEmpty()) // it was a C++ qualified type name.
1580608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner        Tok.setLocation(SS.getBeginLoc());
15811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1582608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // In case the tokens were cached, have Preprocessor replace
1583608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them with the annotation token.
1584608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      PP.AnnotateCachedTokens(Tok);
15859ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
15861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
158739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
15884e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus) {
1589608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // If we're in C, we can't have :: tokens at all (the lexer won't return
1590608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them).  If the identifier is not a type, then it can't be scope either,
15911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // just early exit.
1592608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      return false;
1593eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    }
15941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
159539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // If this is a template-id, annotate with a template-id or type token.
159655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    if (NextToken().is(tok::less)) {
15977532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor      TemplateTy Template;
1598014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
1599014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
16001fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
16011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateNameKind TNK
16027c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara          = Actions.isTemplateName(getCurScope(), SS,
16037c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   /*hasTemplateKeyword=*/false, TemplateName,
1604b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   /*ObjectType=*/ ParsedType(),
1605b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   EnteringContext,
16067c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   Template, MemberOfUnknownSpecialization)) {
1607ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
1608ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
1609e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1610e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName)) {
1611c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // If an unrecoverable error occurred, we need to return true here,
1612c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // because the token stream is in a damaged state.  We may not return
1613c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // a valid identifier.
16149ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
1615c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner        }
1616ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
161755f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    }
1618d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
161939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // The current token, which is either an identifier or a
162039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // template-id, is not part of the annotation. Fall through to
162139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // push that token back into the stream and complete the C++ scope
162239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // specifier annotation.
16231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1624eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
162539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  if (Tok.is(tok::annot_template_id)) {
162625a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1627c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor    if (TemplateId->Kind == TNK_Type_template) {
162839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // A template-id that refers to a type was parsed into a
162939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // template-id annotation in a context where we weren't allowed
163039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // to produce a type annotation token. Update the template-id
163139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // annotation token to a type annotation token now.
1632059101f922de6eb765601459925f4c8914420b23Douglas Gregor      AnnotateTemplateIdTokenAsType();
16339ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
163439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
163539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
1636d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
16376ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (SS.isEmpty())
16389ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
16391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16406ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // A C++ scope specifier that isn't followed by a typename.
16410576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  AnnotateScopeToken(SS, IsNewScope);
16429ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1643eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
1644eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
1645eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
164639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor/// annotates C++ scope specifiers and template-ids.  This returns
164783a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith/// true if there was an error that could not be recovered from.
16481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
164955a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
165055a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1651495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
16524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus &&
16536ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner         "Call sites of this function should be guarded by checking for C++");
16543b887354b1b667c97d070ddc67b5354353c4c07bDouglas Gregor  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
165542d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie          (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
165642d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie         Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
1657eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
16584bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  CXXScopeSpec SS;
1659b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
16609ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return true;
1661edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin  if (SS.isEmpty())
16629ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1663eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
16640576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  AnnotateScopeToken(SS, true);
16659ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1666eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
16676c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall
1668fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieubool Parser::isTokenEqualOrEqualTypo() {
1669fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  tok::TokenKind Kind = Tok.getKind();
1670fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  switch (Kind) {
1671fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  default:
1672d6c7c67313634b317a0d63c32be0511a121bb33dRichard Trieu    return false;
1673fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::ampequal:            // &=
1674fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::starequal:           // *=
1675fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::plusequal:           // +=
1676fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::minusequal:          // -=
1677fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::exclaimequal:        // !=
1678fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::slashequal:          // /=
1679fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::percentequal:        // %=
1680fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::lessequal:           // <=
1681fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::lesslessequal:       // <<=
1682fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::greaterequal:        // >=
1683fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::greatergreaterequal: // >>=
1684fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::caretequal:          // ^=
1685fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::pipeequal:           // |=
1686fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::equalequal:          // ==
1687fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu    Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
1688fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu      << getTokenSimpleSpelling(Kind)
1689fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu      << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
1690fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::equal:
1691fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu    return true;
1692fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  }
1693a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis}
1694a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
16957d100872341f233c81e1d7b72b40457e62c36862Argyrios KyrtzidisSourceLocation Parser::handleUnexpectedCodeCompletionToken() {
16967d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  assert(Tok.is(tok::code_completion));
16977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  PrevTokLocation = Tok.getLocation();
16987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
169923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
1700dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::FnScope) {
1701f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
17027d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
17037d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return PrevTokLocation;
1704dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1705dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1706dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::ClassScope) {
1707f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
17087d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
17097d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return PrevTokLocation;
1710dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1711dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  }
1712dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1713f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
17147d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  cutOffParsing();
17157d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  return PrevTokLocation;
1716dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor}
1717dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
17186c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// Anchor the Parser::FieldCallback vtable to this translation unit.
17196c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// We use a spurious method instead of the destructor because
17206c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// destroying FieldCallbacks can actually be slightly
17216c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// performance-sensitive.
17226c94a6d77f456f23ecd4c2061e6413786b5e6571John McCallvoid Parser::FieldCallback::_anchor() {
17236c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall}
1724f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1725f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor// Code-completion pass-through functions
1726f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1727f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteDirective(bool InConditional) {
1728f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorDirective(InConditional);
1729f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
1730f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1731f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteInConditionalExclusion() {
1732f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
1733f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
17341fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor
17351fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregorvoid Parser::CodeCompleteMacroName(bool IsDefinition) {
1736f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1737f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1738f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1739f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompletePreprocessorExpression() {
1740f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorExpression();
1741f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1742f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1743f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1744f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       MacroInfo *MacroInfo,
1745f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       unsigned ArgumentIndex) {
1746f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1747f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                                ArgumentIndex);
17481fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor}
174955817afdf9d453a443262a733f6caf6692dca118Douglas Gregor
175055817afdf9d453a443262a733f6caf6692dca118Douglas Gregorvoid Parser::CodeCompleteNaturalLanguage() {
175155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  Actions.CodeCompleteNaturalLanguage();
175255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor}
1753f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17543896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregorbool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
1755f986038beed360c031de8654cfba43a5d3184605Francois Pichet  assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
1756f986038beed360c031de8654cfba43a5d3184605Francois Pichet         "Expected '__if_exists' or '__if_not_exists'");
17573896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Result.IsIfExists = Tok.is(tok::kw___if_exists);
17583896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Result.KeywordLoc = ConsumeToken();
1759f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17604a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
17614a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
17623896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Diag(Tok, diag::err_expected_lparen_after)
17633896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor      << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
1764f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1765f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1766f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1767f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Parse nested-name-specifier.
1768efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(),
1769efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor                                 /*EnteringContext=*/false);
1770f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1771f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check nested-name specifier.
17723896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (Result.SS.isInvalid()) {
17733896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    T.skipToEnd();
1774f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1775f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1776f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1777e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  // Parse the unqualified-id.
1778e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
1779e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(),
1780e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         TemplateKWLoc, Result.Name)) {
17813896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    T.skipToEnd();
1782f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1783f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1784f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17853896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (T.consumeClose())
1786f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
17873896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
1788f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check if the symbol exists.
178965019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
179065019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor                                               Result.IsIfExists, Result.SS,
17913896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor                                               Result.Name)) {
17923896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_Exists:
17933896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
17943896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
1795f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17963896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_DoesNotExist:
17973896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
17983896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
17993896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
18003896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_Dependent:
18013896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = IEB_Dependent;
18023896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
180365019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor
180465019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  case Sema::IER_Error:
180565019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor    return true;
18063896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  }
1807f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1808f986038beed360c031de8654cfba43a5d3184605Francois Pichet  return false;
1809f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
1810f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1811563a645de82231a55e221fe655b7188bf8369662Francois Pichetvoid Parser::ParseMicrosoftIfExistsExternalDeclaration() {
18123896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  IfExistsCondition Result;
1813f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (ParseMicrosoftIfExistsCondition(Result))
1814f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1815f986038beed360c031de8654cfba43a5d3184605Francois Pichet
18163896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  BalancedDelimiterTracker Braces(*this, tok::l_brace);
18173896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (Braces.consumeOpen()) {
1818f986038beed360c031de8654cfba43a5d3184605Francois Pichet    Diag(Tok, diag::err_expected_lbrace);
1819f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1820f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1821f986038beed360c031de8654cfba43a5d3184605Francois Pichet
18223896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  switch (Result.Behavior) {
18233896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Parse:
18243896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    // Parse declarations below.
18253896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
18263896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
18273896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Dependent:
18283896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    llvm_unreachable("Cannot have a dependent external declaration");
18293896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
18303896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Skip:
18313896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Braces.skipToEnd();
1832f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1833f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1834f986038beed360c031de8654cfba43a5d3184605Francois Pichet
18353896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  // Parse the declarations.
18363896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1837f986038beed360c031de8654cfba43a5d3184605Francois Pichet    ParsedAttributesWithRange attrs(AttrFactory);
1838f986038beed360c031de8654cfba43a5d3184605Francois Pichet    MaybeParseCXX0XAttributes(attrs);
1839f986038beed360c031de8654cfba43a5d3184605Francois Pichet    MaybeParseMicrosoftAttributes(attrs);
1840f986038beed360c031de8654cfba43a5d3184605Francois Pichet    DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
1841f986038beed360c031de8654cfba43a5d3184605Francois Pichet    if (Result && !getCurScope()->getParent())
1842f986038beed360c031de8654cfba43a5d3184605Francois Pichet      Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
18433896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  }
18443896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Braces.consumeClose();
1845f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
18466aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
18475948ae1021122164b22f74353bb7fe325a64f616Douglas GregorParser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) {
18481b257afbae854c6817f26b7d61c4fed8ff7aebadDouglas Gregor  assert(Tok.isObjCAtKeyword(tok::objc_import) &&
184965030af6526748ce11534e92f0ccefc44091ba13Douglas Gregor         "Improper start to module import");
18506aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  SourceLocation ImportLoc = ConsumeToken();
18516aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
18523d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
18533d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18543d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  // Parse the module path.
18553d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  do {
18563d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    if (!Tok.is(tok::identifier)) {
1857c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor      if (Tok.is(tok::code_completion)) {
1858c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        Actions.CodeCompleteModuleImport(ImportLoc, Path);
1859c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        ConsumeCodeCompletionToken();
1860c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        SkipUntil(tok::semi);
1861c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        return DeclGroupPtrTy();
1862c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor      }
1863c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor
18643d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      Diag(Tok, diag::err_module_expected_ident);
18653d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      SkipUntil(tok::semi);
18663d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      return DeclGroupPtrTy();
18673d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    }
18683d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18693d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    // Record this part of the module path.
18703d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
18713d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    ConsumeToken();
18723d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18733d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    if (Tok.is(tok::period)) {
18743d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      ConsumeToken();
18753d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      continue;
18763d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    }
18773d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18783d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    break;
18793d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  } while (true);
18806aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
18815948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor  DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path);
18826aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  ExpectAndConsumeSemi(diag::err_module_expected_semi);
18836aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  if (Import.isInvalid())
18846aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    return DeclGroupPtrTy();
18856aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
18866aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  return Actions.ConvertDeclToDeclGroup(Import.get());
18876aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor}
18884a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1889c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::diagnoseOverflow() {
1890d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
1891d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.SkipUntil(tok::eof);
1892d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  return true;
18934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
18944a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1895c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
18964a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            const char *Msg,
18974a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            tok::TokenKind SkipToToc ) {
18984a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  LOpen = P.Tok.getLocation();
1899d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc))
1900d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor    return true;
1901d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
1902d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  if (getDepth() < MaxDepth)
1903d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor    return false;
1904d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
1905d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  return diagnoseOverflow();
19064a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
19074a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1908c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::diagnoseMissingClose() {
1909d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
1910d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
1911d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  const char *LHSName = "unknown";
1912b031eab1c07fa2c5bd74c7e92f7c938bf3304729David Blaikie  diag::kind DID;
1913d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  switch (Close) {
1914b031eab1c07fa2c5bd74c7e92f7c938bf3304729David Blaikie  default: llvm_unreachable("Unexpected balanced token");
1915d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
1916d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
1917d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
19184a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  }
1919d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(P.Tok, DID);
1920d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(LOpen, diag::note_matching) << LHSName;
1921b578aee665aad5ed1a46a26217c730fdfbfc8c2eDavid Blaikie  if (P.SkipUntil(Close, /*StopAtSemi*/ true, /*DontConsume*/ true))
1922b578aee665aad5ed1a46a26217c730fdfbfc8c2eDavid Blaikie    LClose = P.ConsumeAnyToken();
19234a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return true;
19244a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
19253896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
1926c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorvoid BalancedDelimiterTracker::skipToEnd() {
19273896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  P.SkipUntil(Close, false);
19283896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor}
1929