Parser.cpp revision 3a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5
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) {
22180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (getLangOpts().CPlusPlus11)
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);
5574e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  MaybeParseCXX11Attributes(attrs);
5587f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  MaybeParseMicrosoftAttributes(attrs);
559e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
5607f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  Result = ParseExternalDeclaration(attrs);
5615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseExternalDeclaration:
56590b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner///
566c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
567c3018153a11afe91849748a93d920040a571b76cChris Lattner///         function-definition
568c3018153a11afe91849748a93d920040a571b76cChris Lattner///         declaration
569a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration
5705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU]   asm-definition
571c3018153a11afe91849748a93d920040a571b76cChris Lattner/// [GNU]   __extension__ external-declaration
5725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
5735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-declaration
5745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-alias-declaration
5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-protocol-definition
5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-method-definition
5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  @end
578c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor/// [C++]   linkage-specification
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-definition:
5805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         simple-asm-expr ';'
5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
582a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// [C++0x] empty-declaration:
583a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///           ';'
584a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor///
58545f965581935791a018df829a14dff53c1dd8f47Douglas Gregor/// [C++0x/GNU] 'extern' 'template' declaration
5867f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::DeclGroupPtrTy
5877f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
5887f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                 ParsingDeclSpec *DS) {
58913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
59036d36806f1972f7ec1d2a3f59155187278c56508Argyrios Kyrtzidis  ParenBraceBracketBalancer BalancerRAIIObj(*this);
5917d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
5927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  if (PP.isCodeCompletionReached()) {
5937d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
5947d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
5957d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  }
5967d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
597d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *SingleDecl = 0;
5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Tok.getKind()) {
599426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola  case tok::annot_pragma_vis:
600426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola    HandlePragmaVisibility();
601426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola    return DeclGroupPtrTy();
602aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman  case tok::annot_pragma_pack:
603aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman    HandlePragmaPack();
604aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman    return DeclGroupPtrTy();
6059595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_msstruct:
6069595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaMSStruct();
6079595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6089595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_align:
6099595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaAlign();
6109595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6119595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_weak:
6129595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaWeak();
6139595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6149595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_weakalias:
6159595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaWeakAlias();
6169595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6179595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_redefine_extname:
6189595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaRedefineExtname();
6199595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6209595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_fp_contract:
6219595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaFPContract();
6229595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6239595c7e2533c836537dc300e75d059c29feb7094Eli Friedman  case tok::annot_pragma_opencl_extension:
6249595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    HandlePragmaOpenCLExtension();
6259595c7e2533c836537dc300e75d059c29feb7094Eli Friedman    return DeclGroupPtrTy();
6265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::semi:
6274b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu    ConsumeExtraSemi(OutsideFunction);
6285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TODO: Invoke action for top-level semicolon.
629682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
63090b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::r_brace:
631883692ebd421c40b44e2c2665e5f54dade5621bcNico Weber    Diag(Tok, diag::err_extraneous_closing_brace);
63290b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    ConsumeBrace();
633682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
63490b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner  case tok::eof:
63590b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner    Diag(Tok, diag::err_expected_external_declaration);
636682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return DeclGroupPtrTy();
637c3018153a11afe91849748a93d920040a571b76cChris Lattner  case tok::kw___extension__: {
638c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    // __extension__ silences extension warnings in the subexpression.
639c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
64039146d6497ad5e7ca8ef639221e7b3e15d07c888Chris Lattner    ConsumeToken();
6417f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    return ParseExternalDeclaration(attrs);
642c3018153a11afe91849748a93d920040a571b76cChris Lattner  }
643dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  case tok::kw_asm: {
6447f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ProhibitAttributes(attrs);
645bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
64621e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation StartLoc = Tok.getLocation();
64721e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SourceLocation EndLoc;
64821e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    ExprResult Result(ParseSimpleAsm(&EndLoc));
649a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
6503f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson    ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
6513f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson                     "top-level asm block");
652dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson
653682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    if (Result.isInvalid())
654682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
65521e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara    SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
656682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
657dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  }
6585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::at:
65995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    return ParseObjCAtDirectives();
6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::minus:
6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::plus:
6624e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().ObjC1) {
663682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      Diag(Tok, diag::err_expected_external_declaration);
664682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      ConsumeToken();
665682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
666682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    }
667682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    SingleDecl = ParseObjCMethodDefinition();
668682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    break;
669791215b7a24666912c0b71175d2ca5ba082f666eDouglas Gregor  case tok::code_completion:
67023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
671849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                             CurParsedObjCImpl? Sema::PCC_ObjCImplementation
672f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                              : Sema::PCC_Namespace);
6737d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
6747d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
675f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor  case tok::kw_using:
6768f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner  case tok::kw_namespace:
6775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case tok::kw_typedef:
678adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_template:
679adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor  case tok::kw_export:    // As in 'export template'
680511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson  case tok::kw_static_assert:
681c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne  case tok::kw__Static_assert:
68226d6023cb0d343bf8fc8836f97d39709bbd4afa0Chad Rosier    // A function definition cannot start with any of these keywords.
68397144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    {
68497144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner      SourceLocation DeclEnd;
6854e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer      StmtVector Stmts;
6867f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
68797144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner    }
688d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
6897306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_static:
6907306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // Parse (then ignore) 'static' prior to a template instantiation. This is
6917306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    // a GCC extension that we intentionally do not support.
6924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
6937306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
6947306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        << 0;
695d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl      SourceLocation DeclEnd;
6964e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer      StmtVector Stmts;
6977f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
6987306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    }
6997306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor    goto dont_know;
7007306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
7017306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor  case tok::kw_inline:
7024e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus) {
7037306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      tok::TokenKind NextKind = NextToken().getKind();
7047306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
7057306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Inline namespaces. Allowed as an extension even in C++03.
7067306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_namespace) {
7077306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
7084e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer        StmtVector Stmts;
7097f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
7107306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
7117306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
7127306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // Parse (then ignore) 'inline' prior to a template instantiation. This is
7137306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      // a GCC extension that we intentionally do not support.
7147306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      if (NextKind == tok::kw_template) {
7157306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
7167306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor          << 1;
7177306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor        SourceLocation DeclEnd;
7184e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer        StmtVector Stmts;
7197f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
7207306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor      }
721d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    }
722d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
723d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl
72445f965581935791a018df829a14dff53c1dd8f47Douglas Gregor  case tok::kw_extern:
7254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
72645f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      // Extern templates
72745f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation ExternLoc = ConsumeToken();
72845f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation TemplateLoc = ConsumeToken();
72980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      Diag(ExternLoc, getLangOpts().CPlusPlus11 ?
7309324583ad2afd09db8c9967cd05c4fa44bac9555Richard Smith             diag::warn_cxx98_compat_extern_template :
7319324583ad2afd09db8c9967cd05c4fa44bac9555Richard Smith             diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
73245f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      SourceLocation DeclEnd;
73345f965581935791a018df829a14dff53c1dd8f47Douglas Gregor      return Actions.ConvertDeclToDeclGroup(
7349241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis                  ParseExplicitInstantiation(Declarator::FileContext,
7359241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis                                             ExternLoc, TemplateLoc, DeclEnd));
73645f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    }
73745f965581935791a018df829a14dff53c1dd8f47Douglas Gregor    // FIXME: Detect C++ linkage specifications here?
738d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl    goto dont_know;
7391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
740f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_exists:
741f986038beed360c031de8654cfba43a5d3184605Francois Pichet  case tok::kw___if_not_exists:
742563a645de82231a55e221fe655b7188bf8369662Francois Pichet    ParseMicrosoftIfExistsExternalDeclaration();
743f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return DeclGroupPtrTy();
7446aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
7455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
746d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl  dont_know:
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // We can't tell whether this is a function-definition or declaration yet.
74820af49a7c5bdb6cca5f4d6586106ef1ce8579311Rafael Espindola    return ParseDeclarationOrFunctionDefinition(attrs, DS);
7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
751682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // This routine returns a DeclGroup, if the thing we parsed only contains a
752682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // single decl, convert it now.
753682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  return Actions.ConvertDeclToDeclGroup(SingleDecl);
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7561426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
7571426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, continues a declaration or declaration list.
758e4246a633b13197634225971b25df0cbdcec0c5dSean Huntbool Parser::isDeclarationAfterDeclarator() {
759e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  // Check for '= delete' or '= default'
7604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
761e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
762e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
763e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt      return false;
764e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
7656c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian
7661426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor  return Tok.is(tok::equal) ||      // int X()=  -> not a function def
7671426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::comma) ||           // int X(),  -> not a function def
7681426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::semi)  ||           // int X();  -> not a function def
7691426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
7701426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor    Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
7714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    (getLangOpts().CPlusPlus &&
77239700f81c5b42e6be93be10275602915f872fc86Fariborz Jahanian     Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
7731426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
7741426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
7751426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a
7761426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, indicates the start of a function definition.
777004659a56916f2f81ede507c12516c146d6c0df3Chris Lattnerbool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
778075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
7795d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  if (Tok.is(tok::l_brace))   // int X() {}
7805d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner    return true;
7815d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner
782004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  // Handle K&R C argument lists: int X(f) int f; {}
7834e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CPlusPlus &&
784075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      Declarator.getFunctionTypeInfo().isKNRPrototype())
785004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner    return isDeclarationSpecifier();
786e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt
7874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
788e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    const Token &KW = NextToken();
789e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt    return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
790e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt  }
791004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner
7925d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner  return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
7935d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner         Tok.is(tok::kw_try);          // X() try { ... }
7941426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor}
7951426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor
7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
7975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// a declaration.  We can't tell which we have until we read up to the
798c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// compound-statement in function-definition. TemplateParams, if
799c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// non-NULL, provides the template parameters when we're parsing a
8001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ template-declaration.
8015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       function-definition: [C99 6.9.1]
803a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
804a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
805a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
806a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///
8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       declaration: [C99 6.7]
808697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner///         declaration-specifiers init-declarator-list[opt] ';'
809697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner/// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
8105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OMP]   threadprivate-directive                              [TODO]
8115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
812682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerParser::DeclGroupPtrTy
8132edf0a2520313cde900799b1eb9bd11c9c776afeSean HuntParser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs,
8142edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                       ParsingDeclSpec &DS,
8152edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                       AccessSpecifier AS) {
8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Parse the common declaration-specifiers piece.
8170efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
818a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
8205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // declaration-specifiers init-declarator-list[opt] ';'
821000732226610650837478cba97843d19b75f648eChris Lattner  if (Tok.is(tok::semi)) {
8222edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ProhibitAttributes(attrs);
8235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
824d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
82554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.complete(TheDecl);
826682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
8275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
828a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
8292edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  DS.takeAttributesFrom(attrs);
8302edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
831246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // ObjC2 allows prefix attributes on class interfaces and protocols.
832246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // FIXME: This still needs better diagnostics. We should only accept
833246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar  // attributes here, no types, etc.
8344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC2 && Tok.is(tok::at)) {
835dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation AtLoc = ConsumeToken(); // the "@"
8361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
837246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar        !Tok.isObjCAtKeyword(tok::objc_protocol)) {
838246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar      Diag(Tok, diag::err_objc_unexpected_attr);
839cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      SkipUntil(tok::semi); // FIXME: better skip?
840682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      return DeclGroupPtrTy();
841cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    }
842d8ac05753dc4506224d445ff98399c01da3136e5John McCall
84354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DS.abort();
84454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
8450de2ae28c603322f05e2d9200c7d457c8b928983Fariborz Jahanian    const char *PrevSpec = 0;
846fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
847fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
848fec54013fcd0eb72642741584ca04c1bc292bef8John McCall      Diag(AtLoc, DiagID) << PrevSpec;
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
850246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar    if (Tok.isObjCAtKeyword(tok::objc_protocol))
851bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor      return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
852bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor
853bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return Actions.ConvertDeclToDeclGroup(
854bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor            ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
855dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
856a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
857c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // If the declspec consisted only of 'extern' and we have a string
858c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // literal following it, this must be a C++ linkage specifier like
859c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  // 'extern "C"'.
8604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Tok.is(tok::string_literal) && getLangOpts().CPlusPlus &&
861c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner      DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
862682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
863d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
864682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    return Actions.ConvertDeclToDeclGroup(TheDecl);
865682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  }
866c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
867d8ac05753dc4506224d445ff98399c01da3136e5John McCall  return ParseDeclGroup(DS, Declarator::FileContext, true);
8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8703acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::DeclGroupPtrTy
8712edf0a2520313cde900799b1eb9bd11c9c776afeSean HuntParser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs,
8722edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt                                             ParsingDeclSpec *DS,
8733acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian                                             AccessSpecifier AS) {
8742edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  if (DS) {
8752edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    return ParseDeclOrFunctionDefInternal(attrs, *DS, AS);
8762edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  } else {
8772edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ParsingDeclSpec PDS(*this);
8782edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // Must temporarily exit the objective-c container scope for
8792edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // parsing c constructs and re-enter objc container scope
8802edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    // afterwards.
8812edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    ObjCDeclContextSwitch ObjCDC(*this);
8822edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
8832edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    return ParseDeclOrFunctionDefInternal(attrs, PDS, AS);
8842edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  }
8853acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian}
8863acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian
8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseFunctionDefinition - We parsed and verified that the specified
8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Declarator is well formed.  If this is a K&R-style function, read the
8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// parameters declaration-list, then start the compound-statement.
8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
891a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///       function-definition: [C99 6.9.1]
892a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner///         decl-specs      declarator declaration-list[opt] compound-statement
893a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result
894a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
8957ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
89623c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
89723c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner///         function-body
8987ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4]
899d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl///         decl-specifier-seq[opt] declarator function-try-block
9005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
901d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
902c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins                                      const ParsedTemplateInfo &TemplateInfo,
903c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins                                      LateParsedAttrList *LateParsedAttrs) {
90428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // Poison the SEH identifiers so they are flagged as illegal in function bodies
90528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
906075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
907a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
908a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // If this is C90 and the declspecs were completely missing, fudge in an
909a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // implicit int.  We do this here because this is the only place where
910a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  // declaration-specifiers are completely optional in the grammar.
9114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
912a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner    const char *PrevSpec;
913fec54013fcd0eb72642741584ca04c1bc292bef8John McCall    unsigned DiagID;
91431c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner    D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
91531c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner                                           D.getIdentifierLoc(),
916fec54013fcd0eb72642741584ca04c1bc292bef8John McCall                                           PrevSpec, DiagID);
917ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
918a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner  }
919a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this declaration was formed with a K&R-style identifier list for the
9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // arguments, parse declarations for all of the args next.
9225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // int foo(a,b) int a; float b; {}
923004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner  if (FTI.isKNRPrototype())
9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseKNRParamDeclarations(D);
9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9267ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // We should have either an opening brace or, in a C++ constructor,
9277ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // we may have a colon.
928758afbcc86ef15f8d433f5f87db1495e50effeb3Douglas Gregor  if (Tok.isNot(tok::l_brace) &&
9294e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      (!getLangOpts().CPlusPlus ||
930cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt       (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
931cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        Tok.isNot(tok::equal)))) {
9325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok, diag::err_expected_fn_body);
9335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
9355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipUntil(tok::l_brace, true, true);
936a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
9375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we didn't find the '{', bail out.
938000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.isNot(tok::l_brace))
939d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
941a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
942c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // Check to make sure that any normal attributes are allowed to be on
943c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // a definition.  Late parsed attributes are checked at the end.
944c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  if (Tok.isNot(tok::equal)) {
945c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    AttributeList *DtorAttrs = D.getAttributes();
946c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    while (DtorAttrs) {
947cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (!IsThreadSafetyAttribute(DtorAttrs->getName()->getName()) &&
948cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          !DtorAttrs->isCXX11Attribute()) {
949c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins        Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition)
950c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins          << DtorAttrs->getName()->getName();
951c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins      }
952c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins      DtorAttrs = DtorAttrs->getNext();
953c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    }
954c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  }
955c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins
9568387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // In delayed template parsing mode, for function template we consume the
9578387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // tokens and store them for late parsing at the end of the translation unit.
9584e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().DelayedTemplateParsing &&
9590963017dcbc32176c79a251c3ab23bc35ac784e5Douglas Gregor      Tok.isNot(tok::equal) &&
9608387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      TemplateInfo.Kind == ParsedTemplateInfo::Template) {
9615354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
9628387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9638387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
9648387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Scope *ParentScope = getCurScope()->getParent();
9658387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
96645fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor    D.setFunctionDefinitionKind(FDK_Definition);
9678387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Decl *DP = Actions.HandleDeclarator(ParentScope, D,
9683fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                        TemplateParameterLists);
9698387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.complete(DP);
9708387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    D.getMutableDeclSpec().abort();
9718387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9728387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (DP) {
973e1fca502e7f1349e9b4520a4ca9a02413bcf2b14Francois Pichet      LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(DP);
9748387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9758387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      FunctionDecl *FnD = 0;
9768387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
9778387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = FunTmpl->getTemplatedDecl();
9788387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      else
9798387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet        FnD = cast<FunctionDecl>(DP);
980d4a0caf78e7c18e7aca65fbfd799a6c024ff51fbFrancois Pichet      Actions.CheckForFunctionRedefinition(FnD);
9818387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
9828387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateParsedTemplateMap[FnD] = LPT;
9838387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Actions.MarkAsLateParsedTemplate(FnD);
9848387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(LPT->Toks);
9858387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    } else {
9868387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      CachedTokens Toks;
9878387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LexTemplateFunctionForLateParsing(Toks);
9888387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    }
9898387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return DP;
9908387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
9912eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian  else if (CurParsedObjCImpl &&
9929e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian           !TemplateInfo.TemplateParams &&
9939e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian           (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
9949e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian            Tok.is(tok::colon)) &&
995be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      Actions.CurContext->isTranslationUnit()) {
996be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
997be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    Scope *ParentScope = getCurScope()->getParent();
998be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian
999be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.setFunctionDefinitionKind(FDK_Definition);
1000be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
10015354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                              MultiTemplateParamsArg());
1002be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.complete(FuncDecl);
1003be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    D.getMutableDeclSpec().abort();
1004be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    if (FuncDecl) {
1005be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      // Consume the tokens and store them for later parsing.
1006be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      StashAwayMethodOrFunctionBodyTokens(FuncDecl);
1007be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      CurParsedObjCImpl->HasCFunction = true;
1008be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian      return FuncDecl;
1009be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian    }
1010be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian  }
1011be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian
1012b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Enter a scope for the function body.
10138935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1014a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1015b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // Tell the actions module that we have entered a function definition with the
1016b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner  // specified Declarator for the function.
1017d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Res = TemplateInfo.TemplateParams?
101823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
10195354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                              *TemplateInfo.TemplateParams, D)
102023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
1021a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
102254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclarator context before we parse the body.
102354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.complete(Res);
102454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
102554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // Break out of the ParsingDeclSpec context, too.  This const_cast is
102654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  // safe because we're always the sole owner.
102754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  D.getMutableDeclSpec().abort();
102854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
1029cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  if (Tok.is(tok::equal)) {
10304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
1031cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ConsumeToken();
1032cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1033cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    Actions.ActOnFinishFunctionBody(Res, 0, false);
1034cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1035cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    bool Delete = false;
1036cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    SourceLocation KWLoc;
1037cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Tok.is(tok::kw_delete)) {
103880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      Diag(Tok, getLangOpts().CPlusPlus11 ?
10397fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith           diag::warn_cxx98_compat_deleted_function :
1040d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith           diag::ext_deleted_function);
1041cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1042cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      KWLoc = ConsumeToken();
1043cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Actions.SetDeclDeleted(Res, KWLoc);
1044cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Delete = true;
1045cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else if (Tok.is(tok::kw_default)) {
104680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      Diag(Tok, getLangOpts().CPlusPlus11 ?
10477fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith           diag::warn_cxx98_compat_defaulted_function :
1048d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith           diag::ext_defaulted_function);
1049cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1050cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      KWLoc = ConsumeToken();
1051cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Actions.SetDeclDefaulted(Res, KWLoc);
1052cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else {
1053cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      llvm_unreachable("function definition after = not 'delete' or 'default'");
1054cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
1055cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1056cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Tok.is(tok::comma)) {
1057cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
1058cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt        << Delete;
1059cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      SkipUntil(tok::semi);
1060cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    } else {
1061cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1062cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                       Delete ? "delete" : "default", tok::semi);
1063cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
1064cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1065cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    return Res;
1066cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  }
1067cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
1068d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (Tok.is(tok::kw_try))
1069c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor    return ParseFunctionTryBlock(Res, BodyScope);
1070d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
10717ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // If we have a colon, then we're probably parsing a C++
10727ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor  // ctor-initializer.
1073d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  if (Tok.is(tok::colon)) {
10747ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor    ParseConstructorInitializer(Res);
1075d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall
1076d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    // Recover from error.
1077d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    if (!Tok.is(tok::l_brace)) {
1078c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor      BodyScope.Exit();
10799ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.ActOnFinishFunctionBody(Res, 0);
1080d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall      return Res;
1081d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall    }
1082d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall  } else
1083393612e6c7727f1fee50039254d9f434364cc0b2Fariborz Jahanian    Actions.ActOnDefaultCtorInitializers(Res);
10847ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor
1085c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  // Late attributes are parsed in the same scope as the function body.
1086c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins  if (LateParsedAttrs)
1087c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins    ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
1088c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins
1089c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor  return ParseFunctionStatementBody(Res, BodyScope);
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// types for a function with a K&R-style identifier list for arguments.
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseKNRParamDeclarations(Declarator &D) {
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // We know that the top-level of this declarator is a function.
1096075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
109804421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // Enter function-declaration scope, limiting any declarators to the
109904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  // function prototype scope, including parameter declarators.
11003a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
11013a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith                            Scope::FunctionDeclarationScope | Scope::DeclScope);
110204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read all the argument declarations.
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isDeclarationSpecifier()) {
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SourceLocation DSStart = Tok.getLocation();
1106a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the common declaration-specifiers piece.
11080b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarationSpecifiers(DS);
1110a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: 'each declaration in the declaration list shall have at
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // least one declarator'.
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // the declarations though.  It's trivial to ignore them, really hard to do
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // anything else with them.
1116000732226610650837478cba97843d19b75f648eChris Lattner    if (Tok.is(tok::semi)) {
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DSStart, diag::err_declaration_does_not_declare_param);
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ConsumeToken();
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1121a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // than register.
11245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        DS.getStorageClassSpec() != DeclSpec::SCS_register) {
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getStorageClassSpecLoc(),
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.isThreadSpecified()) {
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(DS.getThreadSpecLoc(),
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           diag::err_invalid_storage_class_in_func_decl);
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      DS.ClearStorageClassSpecs();
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1135a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Parse the first declarator attached to this declspec.
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParseDeclarator(ParmDeclarator);
11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Handle the full declarator list.
11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (1) {
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If attributes are present, parse them.
11437f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      MaybeParseGNUAttributes(ParmDeclarator);
1144a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Ask the actions module to compute the type for this declarator.
1146d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *Param =
114723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
11482bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff
1149a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump      if (Param &&
11505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // A missing identifier has already been diagnosed.
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ParmDeclarator.getIdentifier()) {
11525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Scan the argument list looking for the correct param to apply this
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // type.
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0; ; ++i) {
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // C99 6.9.1p6: those declarators shall declare only identifiers from
11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // the identifier list.
11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (i == FTI.NumArgs) {
11591ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner            Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
11606898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner              << ParmDeclarator.getIdentifier();
11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
1163a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Reject redefinitions of parameters.
116604421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner            if (FTI.ArgInfo[i].Param) {
11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(ParmDeclarator.getIdentifierLoc(),
11681ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner                   diag::err_param_redefinition)
11696898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner                 << ParmDeclarator.getIdentifier();
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            } else {
117104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner              FTI.ArgInfo[i].Param = Param;
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            break;
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we don't have a comma, it is either the end of the list (a ';') or
11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // an error, bail out.
1180000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.isNot(tok::comma))
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
1182a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11837984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith      ParmDeclarator.clear();
11847984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Consume the comma.
11867984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith      ParmDeclarator.setCommaLoc(ConsumeToken());
1187a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Parse the next declarator.
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParseDeclarator(ParmDeclarator);
11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1191a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
11928bb21d32e9ccc9d9c221506dff26acafa8724ccaChris Lattner    if (ExpectAndConsumeSemi(diag::err_expected_semi_declaration)) {
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip to end of block or statement
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi, true);
1195000732226610650837478cba97843d19b75f648eChris Lattner      if (Tok.is(tok::semi))
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ConsumeToken();
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1199a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The actions module must verify that all arguments were declared.
120123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// allowed to be a wide string, and is not subject to character translation.
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-string-literal:
12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         string-literal
12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
121160d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseAsmStringLiteral() {
12127f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek  switch (Tok.getKind()) {
12137f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    case tok::string_literal:
12147f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      break;
121599831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf8_string_literal:
121699831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf16_string_literal:
121799831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    case tok::utf32_string_literal:
12187f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    case tok::wide_string_literal: {
12197f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      SourceLocation L = Tok.getLocation();
12207f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      Diag(Tok, diag::err_asm_operand_wide_string_literal)
122199831e4677a7e2e051af636221694d60ba31fcdbRichard Smith        << (Tok.getKind() == tok::wide_string_literal)
12227f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek        << SourceRange(L, L);
12237f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      return ExprError();
12247f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    }
12257f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek    default:
122697f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs      Diag(Tok, diag::err_expected_string_literal)
122797f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs        << /*Source='in...'*/0 << "'asm'";
12287f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek      return ExprError();
12295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1230a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
123199831e4677a7e2e051af636221694d60ba31fcdbRichard Smith  return ParseStringLiteralExpression();
12325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseSimpleAsm
12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] simple-asm-expr:
12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///         'asm' '(' asm-string-literal ')'
12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
123960d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
1240000732226610650837478cba97843d19b75f648eChris Lattner  assert(Tok.is(tok::kw_asm) && "Not an asm!");
1241dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson  SourceLocation Loc = ConsumeToken();
1242a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12437a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  if (Tok.is(tok::kw_volatile)) {
1244841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    // Remove from the end of 'asm' to the end of 'volatile'.
1245841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1246841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall                             PP.getLocForEndOfToken(Tok.getLocation()));
1247841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall
1248841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall    Diag(Tok, diag::warn_file_asm_volatile)
1249849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateRemoval(RemovalRange);
12507a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall    ConsumeToken();
12517a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall  }
12527a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall
12534a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
12544a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
12551ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "asm";
125661364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl    return ExprError();
12575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1258a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
125960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result(ParseAsmStringLiteral());
1260a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
1261ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  if (Result.isInvalid()) {
1262ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    SkipUntil(tok::r_paren, true, true);
1263ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
1264ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl      *EndLoc = Tok.getLocation();
1265ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    ConsumeAnyToken();
1266ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  } else {
12674a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    // Close the paren and get the location of the end bracket
12684a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
1269ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl    if (EndLoc)
12704a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      *EndLoc = T.getCloseLocation();
1271ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl  }
1272a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump
12733fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
127625a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// \brief Get the TemplateIdAnnotation from the token and put it in the
127725a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// cleanup pool so that it gets destroyed when parsing the current top level
127825a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// declaration is finished.
127925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios KyrtzidisTemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
128025a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  assert(tok.is(tok::annot_template_id) && "Expected template-id token");
128125a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  TemplateIdAnnotation *
128225a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
128325a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis  return Id;
128425a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis}
128525a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis
12860576681bac125be07f77f66b02a3dba2c3a24557Richard Smithvoid Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {
12870576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Push the current token back into the token stream (or revert it if it is
12880576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // cached) and use an annotation scope token for current token.
12890576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (PP.isBacktrackEnabled())
12900576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.RevertCachedTokens(1);
12910576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  else
12920576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.EnterToken(Tok);
12930576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setKind(tok::annot_cxxscope);
12940576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
12950576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Tok.setAnnotationRange(SS.getRange());
12960576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
12970576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // In case the tokens were cached, have Preprocessor replace them
12980576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // with the annotation token.  We don't need to do this if we've
12990576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // just reverted back to a prior state.
13000576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (IsNewAnnotation)
13010576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
13020576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
13030576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13040576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \brief Attempt to classify the name at the current token position. This may
13050576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// form a type, scope or primary expression annotation, or replace the token
13060576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// with a typo-corrected keyword. This is only appropriate when the current
13070576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// name must refer to an entity which has already been declared.
13080576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///
13090576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \param IsAddressOfOperand Must be \c true if the name is preceded by an '&'
13100576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///        and might possibly have a dependent nested name specifier.
13110576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \param CCC Indicates how to perform typo-correction for this name. If NULL,
13120576681bac125be07f77f66b02a3dba2c3a24557Richard Smith///        no typo correction will be performed.
13130576681bac125be07f77f66b02a3dba2c3a24557Richard SmithParser::AnnotatedNameKind
13140576681bac125be07f77f66b02a3dba2c3a24557Richard SmithParser::TryAnnotateName(bool IsAddressOfOperand,
13150576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                        CorrectionCandidateCallback *CCC) {
13160576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));
13170576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13180576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  const bool EnteringContext = false;
13190576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
13200576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13210576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  CXXScopeSpec SS;
13220576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (getLangOpts().CPlusPlus &&
13230576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
13240576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Error;
13250576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13260576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
13270576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
13280576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                  !WasScopeAnnotation))
13290576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
13300576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Unresolved;
13310576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
13320576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13330576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  IdentifierInfo *Name = Tok.getIdentifierInfo();
13340576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  SourceLocation NameLoc = Tok.getLocation();
13350576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13360576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // FIXME: Move the tentative declaration logic into ClassifyName so we can
13370576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // typo-correct to tentatively-declared identifiers.
13380576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (isTentativelyDeclared(Name)) {
13390576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // Identifier has been tentatively declared, and thus cannot be resolved as
13400576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // an expression. Fall back to annotating it as a type.
13410576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
13420576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                  !WasScopeAnnotation))
13430576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
13440576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl;
13450576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
13460576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13470576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Token Next = NextToken();
13480576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13490576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Look up and classify the identifier. We don't perform any typo-correction
13500576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // after a scope specifier, because in general we can't recover from typos
13510576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // there (eg, after correcting 'A::tempalte B<X>::C', we would need to jump
13520576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // back into scope specifier parsing).
13530576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  Sema::NameClassification Classification
13540576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next,
13550576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                           IsAddressOfOperand, SS.isEmpty() ? CCC : 0);
13560576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13570576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  switch (Classification.getKind()) {
13580576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Error:
13590576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Error;
13600576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13610576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Keyword:
13620576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // The identifier was typo-corrected to a keyword.
13630576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setIdentifierInfo(Name);
13640576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(Name->getTokenID());
13650576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.TypoCorrectToken(Tok);
13660576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
13670576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      AnnotateScopeToken(SS, !WasScopeAnnotation);
13680576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // We've "annotated" this as a keyword.
13690576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
13700576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13710576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Unknown:
13720576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // It's not something we know about. Leave it unannotated.
13730576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    break;
13740576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13750576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Type:
13760576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(tok::annot_typename);
13770576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    setTypeAnnotation(Tok, Classification.getType());
13780576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setAnnotationEndLoc(NameLoc);
13790576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
13800576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      Tok.setLocation(SS.getBeginLoc());
13810576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
13820576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
13830576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13840576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_Expression:
13850576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setKind(tok::annot_primary_expr);
13860576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    setExprAnnotation(Tok, Classification.getExpression());
13870576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Tok.setAnnotationEndLoc(NameLoc);
13880576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (SS.isNotEmpty())
13890576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      Tok.setLocation(SS.getBeginLoc());
13900576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    PP.AnnotateCachedTokens(Tok);
13910576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
13920576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
13930576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_TypeTemplate:
13940576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (Next.isNot(tok::less)) {
13950576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      // This may be a type template being used as a template template argument.
13960576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      if (SS.isNotEmpty())
13970576681bac125be07f77f66b02a3dba2c3a24557Richard Smith        AnnotateScopeToken(SS, !WasScopeAnnotation);
13980576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_TemplateName;
13990576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    }
14000576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // Fall through.
14010576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_FunctionTemplate: {
14020576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    // We have a type or function template followed by '<'.
14030576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    ConsumeToken();
14040576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    UnqualifiedId Id;
14050576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    Id.setIdentifier(Name, NameLoc);
14060576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (AnnotateTemplateIdToken(
14070576681bac125be07f77f66b02a3dba2c3a24557Richard Smith            TemplateTy::make(Classification.getTemplateName()),
14080576681bac125be07f77f66b02a3dba2c3a24557Richard Smith            Classification.getTemplateNameKind(), SS, SourceLocation(), Id))
14090576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      return ANK_Error;
14100576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    return ANK_Success;
14110576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
14120576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
14130576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  case Sema::NC_NestedNameSpecifier:
14140576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    llvm_unreachable("already parsed nested name specifier");
14150576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
14160576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
14170576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  // Unable to classify the name, but maybe we can annotate a scope specifier.
14180576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  if (SS.isNotEmpty())
14190576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    AnnotateScopeToken(SS, !WasScopeAnnotation);
14200576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  return ANK_Unresolved;
14210576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
14220576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
1423eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateTypeOrScopeToken - If the current token position is on a
1424eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typename (possibly qualified in C++) or a C++ scope specifier not followed
1425eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1426eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// with a single annotation token representing the typename or C++ scope
1427eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// respectively.
1428eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This simplifies handling of C++ scope specifiers and allows efficient
1429eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// backtracking without the need to re-parse and resolve nested-names and
1430eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typenames.
143144802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// It will mainly be called when we expect to treat identifiers as typenames
143244802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// (if they are typenames). For example, in C we do not expect identifiers
143344802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// inside expressions to be treated as typenames so it will not be called
143444802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// for expressions in C.
143544802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// The benefit for C/ObjC is that a typename will be annotated and
1436b43a50ff1b0b171ece84425b0ad83a9a31f038faSteve Naroff/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
143744802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// will not be called twice, once to check whether we have a declaration
143844802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// specifier, and another one to get the actual type inside
143944802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// ParseDeclarationSpecifiers).
1440a7bc7c880f86bc180684ef032d06df51bcae7a23Chris Lattner///
14419ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// This returns true if an error occurred.
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
144355a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
144455a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1445fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrainbool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
14461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
144742d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie          || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)
144823756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          || Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id))
144923756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          && "Cannot be a type or scope token!");
14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1451d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Tok.is(tok::kw_typename)) {
1452d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Parse a C++ typename-specifier, e.g., "typename T::type".
1453d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //
1454d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //   typename-specifier:
1455d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    //     'typename' '::' [opt] nested-name-specifier identifier
14561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     'typename' '::' [opt] nested-name-specifier template [opt]
14571734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    //            simple-template-id
1458d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    SourceLocation TypenameLoc = ConsumeToken();
1459d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    CXXScopeSpec SS;
1460efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(),
1461efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor                                       /*EnteringContext=*/false,
14624147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet                                       0, /*IsTypename*/true))
14639ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
14649ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    if (!SS.isSet()) {
1465b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet      if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
1466b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet          Tok.is(tok::annot_decltype)) {
146723756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith        // Attempt to recover by skipping the invalid 'typename'
1468b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet        if (Tok.is(tok::annot_decltype) ||
1469b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet            (!TryAnnotateTypeOrScopeToken(EnteringContext, NeedType) &&
1470b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet            Tok.isAnnotation())) {
147123756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          unsigned DiagID = diag::err_expected_qualified_after_typename;
147223756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          // MS compatibility: MSVC permits using known types with typename.
147323756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          // e.g. "typedef typename T* pointer_type"
147423756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          if (getLangOpts().MicrosoftExt)
147523756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith            DiagID = diag::warn_expected_qualified_after_typename;
147623756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          Diag(Tok.getLocation(), DiagID);
147723756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith          return false;
147823756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith        }
147923756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith      }
148023756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith
148123756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith      Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
14829ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1483d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    }
1484d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1485d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    TypeResult Ty;
1486d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (Tok.is(tok::identifier)) {
1487d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      // FIXME: check whether the next token is '<', first!
148823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
14891a15dae8be2b28e02b6639aa92b832465c5be420Douglas Gregor                                     *Tok.getIdentifierInfo(),
1490d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     Tok.getLocation());
14911734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else if (Tok.is(tok::annot_template_id)) {
149225a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
14931734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      if (TemplateId->Kind == TNK_Function_template) {
14941734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        Diag(Tok, diag::err_typename_refers_to_non_type_template)
14951734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor          << Tok.getAnnotationRange();
14969ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall        return true;
14971734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      }
1498d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
14995354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer      ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1500a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                         TemplateId->NumArgs);
150166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara
1502a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
150366581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                                     TemplateId->TemplateKWLoc,
1504a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->Template,
1505a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->TemplateNameLoc,
1506a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->LAngleLoc,
150766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                                     TemplateArgsPtr,
1508a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor                                     TemplateId->RAngleLoc);
15091734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    } else {
15101734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Diag(Tok, diag::err_expected_type_name_after_typename)
15111734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        << SS.getRange();
15129ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
15131734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
15141734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
151539d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    SourceLocation EndLoc = Tok.getLastLoc();
15161734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setKind(tok::annot_typename);
1517b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
151839d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl    Tok.setAnnotationEndLoc(EndLoc);
15191734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    Tok.setLocation(TypenameLoc);
15201734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    PP.AnnotateCachedTokens(Tok);
15219ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1522d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1523d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1524ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall  // Remembers whether the token was originally a scope annotation.
15250576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1526ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall
1527eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  CXXScopeSpec SS;
15284e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
1529b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
15309ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return true;
1531eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
15320576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  return TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, NeedType,
15330576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                   SS, !WasScopeAnnotation);
15340576681bac125be07f77f66b02a3dba2c3a24557Richard Smith}
15350576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
15360576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \brief Try to annotate a type or scope token, having already parsed an
15370576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// optional scope specifier. \p IsNewScope should be \c true unless the scope
15380576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// specifier was extracted from an existing tok::annot_cxxscope annotation.
15390576681bac125be07f77f66b02a3dba2c3a24557Richard Smithbool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(bool EnteringContext,
15400576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       bool NeedType,
15410576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       CXXScopeSpec &SS,
15420576681bac125be07f77f66b02a3dba2c3a24557Richard Smith                                                       bool IsNewScope) {
1543eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis  if (Tok.is(tok::identifier)) {
1544fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain    IdentifierInfo *CorrectedII = 0;
1545608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner    // Determine whether the identifier is a type name.
1546b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
1547b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            Tok.getLocation(), getCurScope(),
15481e52dfc648ce0b25ef57ae29ef1b4337d80011efFariborz Jahanian                                            &SS, false,
15499e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            NextToken().is(tok::period),
15509e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor                                            ParsedType(),
1551fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara                                            /*IsCtorOrDtorName=*/false,
1552fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain                                            /*NonTrivialTypeSourceInfo*/true,
1553fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain                                            NeedType ? &CorrectedII : NULL)) {
1554fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain      // A FixIt was applied as a result of typo correction
1555fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain      if (CorrectedII)
1556fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain        Tok.setIdentifierInfo(CorrectedII);
1557608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // This is a typename. Replace the current token in-place with an
1558608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // annotation type token.
1559b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner      Tok.setKind(tok::annot_typename);
1560b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      setTypeAnnotation(Tok, Ty);
1561608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      Tok.setAnnotationEndLoc(Tok.getLocation());
1562608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      if (SS.isNotEmpty()) // it was a C++ qualified type name.
1563608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner        Tok.setLocation(SS.getBeginLoc());
15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1565608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // In case the tokens were cached, have Preprocessor replace
1566608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them with the annotation token.
1567608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      PP.AnnotateCachedTokens(Tok);
15689ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
15691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
157039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
15714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus) {
1572608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // If we're in C, we can't have :: tokens at all (the lexer won't return
1573608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      // them).  If the identifier is not a type, then it can't be scope either,
15741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // just early exit.
1575608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner      return false;
1576eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis    }
15771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
157839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // If this is a template-id, annotate with a template-id or type token.
157955f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    if (NextToken().is(tok::less)) {
15807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor      TemplateTy Template;
1581014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      UnqualifiedId TemplateName;
1582014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
15831fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor      bool MemberOfUnknownSpecialization;
15841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (TemplateNameKind TNK
15857c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara          = Actions.isTemplateName(getCurScope(), SS,
15867c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   /*hasTemplateKeyword=*/false, TemplateName,
1587b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   /*ObjectType=*/ ParsedType(),
1588b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   EnteringContext,
15897c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara                                   Template, MemberOfUnknownSpecialization)) {
1590ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        // Consume the identifier.
1591ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor        ConsumeToken();
1592e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara        if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1593e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    TemplateName)) {
1594c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // If an unrecoverable error occurred, we need to return true here,
1595c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // because the token stream is in a damaged state.  We may not return
1596c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner          // a valid identifier.
15979ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall          return true;
1598c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner        }
1599ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor      }
160055f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    }
1601d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
160239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // The current token, which is either an identifier or a
160339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // template-id, is not part of the annotation. Fall through to
160439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // push that token back into the stream and complete the C++ scope
160539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    // specifier annotation.
16061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1607eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
160839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  if (Tok.is(tok::annot_template_id)) {
160925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1610c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor    if (TemplateId->Kind == TNK_Type_template) {
161139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // A template-id that refers to a type was parsed into a
161239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // template-id annotation in a context where we weren't allowed
161339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // to produce a type annotation token. Update the template-id
161439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      // annotation token to a type annotation token now.
1615059101f922de6eb765601459925f4c8914420b23Douglas Gregor      AnnotateTemplateIdTokenAsType();
16169ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall      return false;
161739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    }
161839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
1619d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor
16206ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  if (SS.isEmpty())
16219ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
16221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16236ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner  // A C++ scope specifier that isn't followed by a typename.
16240576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  AnnotateScopeToken(SS, IsNewScope);
16259ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1626eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
1627eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
1628eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
162939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor/// annotates C++ scope specifiers and template-ids.  This returns
163083a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith/// true if there was an error that could not be recovered from.
16311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
163255a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete
163355a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid.
1634495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
16354e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus &&
16366ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner         "Call sites of this function should be guarded by checking for C++");
16373b887354b1b667c97d070ddc67b5354353c4c07bDouglas Gregor  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
163842d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie          (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
163942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie         Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
1640eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
16414bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis  CXXScopeSpec SS;
1642b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
16439ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return true;
1644edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin  if (SS.isEmpty())
16459ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall    return false;
1646eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis
16470576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  AnnotateScopeToken(SS, true);
16489ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall  return false;
1649eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis}
16506c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall
1651fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieubool Parser::isTokenEqualOrEqualTypo() {
1652fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  tok::TokenKind Kind = Tok.getKind();
1653fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  switch (Kind) {
1654fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  default:
1655d6c7c67313634b317a0d63c32be0511a121bb33dRichard Trieu    return false;
1656fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::ampequal:            // &=
1657fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::starequal:           // *=
1658fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::plusequal:           // +=
1659fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::minusequal:          // -=
1660fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::exclaimequal:        // !=
1661fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::slashequal:          // /=
1662fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::percentequal:        // %=
1663fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::lessequal:           // <=
1664fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::lesslessequal:       // <<=
1665fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::greaterequal:        // >=
1666fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::greatergreaterequal: // >>=
1667fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::caretequal:          // ^=
1668fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::pipeequal:           // |=
1669fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::equalequal:          // ==
1670fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu    Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
1671fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu      << getTokenSimpleSpelling(Kind)
1672fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu      << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
1673fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  case tok::equal:
1674fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu    return true;
1675fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu  }
1676a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis}
1677a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis
16787d100872341f233c81e1d7b72b40457e62c36862Argyrios KyrtzidisSourceLocation Parser::handleUnexpectedCodeCompletionToken() {
16797d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  assert(Tok.is(tok::code_completion));
16807d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  PrevTokLocation = Tok.getLocation();
16817d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
168223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
1683dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::FnScope) {
1684f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
16857d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
16867d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return PrevTokLocation;
1687dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1688dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1689dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    if (S->getFlags() & Scope::ClassScope) {
1690f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
16917d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
16927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return PrevTokLocation;
1693dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    }
1694dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor  }
1695dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1696f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
16977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  cutOffParsing();
16987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  return PrevTokLocation;
1699dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor}
1700dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
17016c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// Anchor the Parser::FieldCallback vtable to this translation unit.
17026c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// We use a spurious method instead of the destructor because
17036c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// destroying FieldCallbacks can actually be slightly
17046c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// performance-sensitive.
17056c94a6d77f456f23ecd4c2061e6413786b5e6571John McCallvoid Parser::FieldCallback::_anchor() {
17066c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall}
1707f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1708f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor// Code-completion pass-through functions
1709f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1710f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteDirective(bool InConditional) {
1711f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorDirective(InConditional);
1712f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
1713f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1714f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteInConditionalExclusion() {
1715f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
1716f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor}
17171fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor
17181fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregorvoid Parser::CodeCompleteMacroName(bool IsDefinition) {
1719f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1720f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1721f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1722f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompletePreprocessorExpression() {
1723f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorExpression();
1724f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor}
1725f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor
1726f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1727f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       MacroInfo *MacroInfo,
1728f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                       unsigned ArgumentIndex) {
1729f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor  Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1730f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor                                                ArgumentIndex);
17311fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor}
173255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor
173355817afdf9d453a443262a733f6caf6692dca118Douglas Gregorvoid Parser::CodeCompleteNaturalLanguage() {
173455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  Actions.CodeCompleteNaturalLanguage();
173555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor}
1736f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17373896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregorbool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
1738f986038beed360c031de8654cfba43a5d3184605Francois Pichet  assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
1739f986038beed360c031de8654cfba43a5d3184605Francois Pichet         "Expected '__if_exists' or '__if_not_exists'");
17403896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Result.IsIfExists = Tok.is(tok::kw___if_exists);
17413896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Result.KeywordLoc = ConsumeToken();
1742f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17434a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
17444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  if (T.consumeOpen()) {
17453896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Diag(Tok, diag::err_expected_lparen_after)
17463896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor      << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
1747f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1748f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1749f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1750f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Parse nested-name-specifier.
1751efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor  ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(),
1752efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor                                 /*EnteringContext=*/false);
1753f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1754f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check nested-name specifier.
17553896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (Result.SS.isInvalid()) {
17563896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    T.skipToEnd();
1757f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1758f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1759f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1760e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  // Parse the unqualified-id.
1761e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
1762e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(),
1763e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         TemplateKWLoc, Result.Name)) {
17643896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    T.skipToEnd();
1765f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
1766f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1767f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17683896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (T.consumeClose())
1769f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return true;
17703896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
1771f986038beed360c031de8654cfba43a5d3184605Francois Pichet  // Check if the symbol exists.
177265019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
177365019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor                                               Result.IsIfExists, Result.SS,
17743896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor                                               Result.Name)) {
17753896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_Exists:
17763896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
17773896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
1778f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17793896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_DoesNotExist:
17803896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
17813896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
17823896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
17833896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case Sema::IER_Dependent:
17843896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Result.Behavior = IEB_Dependent;
17853896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
178665019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor
178765019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  case Sema::IER_Error:
178865019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor    return true;
17893896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  }
1790f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1791f986038beed360c031de8654cfba43a5d3184605Francois Pichet  return false;
1792f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
1793f986038beed360c031de8654cfba43a5d3184605Francois Pichet
1794563a645de82231a55e221fe655b7188bf8369662Francois Pichetvoid Parser::ParseMicrosoftIfExistsExternalDeclaration() {
17953896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  IfExistsCondition Result;
1796f986038beed360c031de8654cfba43a5d3184605Francois Pichet  if (ParseMicrosoftIfExistsCondition(Result))
1797f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1798f986038beed360c031de8654cfba43a5d3184605Francois Pichet
17993896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  BalancedDelimiterTracker Braces(*this, tok::l_brace);
18003896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (Braces.consumeOpen()) {
1801f986038beed360c031de8654cfba43a5d3184605Francois Pichet    Diag(Tok, diag::err_expected_lbrace);
1802f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1803f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1804f986038beed360c031de8654cfba43a5d3184605Francois Pichet
18053896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  switch (Result.Behavior) {
18063896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Parse:
18073896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    // Parse declarations below.
18083896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    break;
18093896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
18103896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Dependent:
18113896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    llvm_unreachable("Cannot have a dependent external declaration");
18123896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
18133896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case IEB_Skip:
18143896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    Braces.skipToEnd();
1815f986038beed360c031de8654cfba43a5d3184605Francois Pichet    return;
1816f986038beed360c031de8654cfba43a5d3184605Francois Pichet  }
1817f986038beed360c031de8654cfba43a5d3184605Francois Pichet
18183896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  // Parse the declarations.
18193896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1820f986038beed360c031de8654cfba43a5d3184605Francois Pichet    ParsedAttributesWithRange attrs(AttrFactory);
18214e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    MaybeParseCXX11Attributes(attrs);
1822f986038beed360c031de8654cfba43a5d3184605Francois Pichet    MaybeParseMicrosoftAttributes(attrs);
1823f986038beed360c031de8654cfba43a5d3184605Francois Pichet    DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
1824f986038beed360c031de8654cfba43a5d3184605Francois Pichet    if (Result && !getCurScope()->getParent())
1825f986038beed360c031de8654cfba43a5d3184605Francois Pichet      Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
18263896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  }
18273896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  Braces.consumeClose();
1828f986038beed360c031de8654cfba43a5d3184605Francois Pichet}
18296aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
18305948ae1021122164b22f74353bb7fe325a64f616Douglas GregorParser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) {
18311b257afbae854c6817f26b7d61c4fed8ff7aebadDouglas Gregor  assert(Tok.isObjCAtKeyword(tok::objc_import) &&
183265030af6526748ce11534e92f0ccefc44091ba13Douglas Gregor         "Improper start to module import");
18336aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  SourceLocation ImportLoc = ConsumeToken();
18346aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
1835cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
18363d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18373d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  // Parse the module path.
18383d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  do {
18393d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    if (!Tok.is(tok::identifier)) {
1840c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor      if (Tok.is(tok::code_completion)) {
1841c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        Actions.CodeCompleteModuleImport(ImportLoc, Path);
1842c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        ConsumeCodeCompletionToken();
1843c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        SkipUntil(tok::semi);
1844c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor        return DeclGroupPtrTy();
1845c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor      }
1846c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor
18473d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      Diag(Tok, diag::err_module_expected_ident);
18483d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      SkipUntil(tok::semi);
18493d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      return DeclGroupPtrTy();
18503d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    }
18513d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18523d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    // Record this part of the module path.
18533d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
18543d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    ConsumeToken();
18553d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18563d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    if (Tok.is(tok::period)) {
18573d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      ConsumeToken();
18583d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor      continue;
18593d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    }
18603d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor
18613d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor    break;
18623d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor  } while (true);
18636aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
18645948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor  DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path);
18656aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  ExpectAndConsumeSemi(diag::err_module_expected_semi);
18666aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  if (Import.isInvalid())
18676aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    return DeclGroupPtrTy();
18686aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
18696aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  return Actions.ConvertDeclToDeclGroup(Import.get());
18706aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor}
18714a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1872c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::diagnoseOverflow() {
1873d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
1874d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.SkipUntil(tok::eof);
1875d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  return true;
18764a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
18774a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1878c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
18794a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            const char *Msg,
18804a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                            tok::TokenKind SkipToToc ) {
18814a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  LOpen = P.Tok.getLocation();
1882d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc))
1883d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor    return true;
1884d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
1885d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  if (getDepth() < MaxDepth)
1886d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor    return false;
1887d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
1888d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  return diagnoseOverflow();
18894a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
18904a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1891c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::diagnoseMissingClose() {
1892d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
1893d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor
1894d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  const char *LHSName = "unknown";
1895b031eab1c07fa2c5bd74c7e92f7c938bf3304729David Blaikie  diag::kind DID;
1896d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  switch (Close) {
1897b031eab1c07fa2c5bd74c7e92f7c938bf3304729David Blaikie  default: llvm_unreachable("Unexpected balanced token");
1898d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
1899d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
1900d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
19014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  }
1902d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(P.Tok, DID);
1903d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  P.Diag(LOpen, diag::note_matching) << LHSName;
1904b578aee665aad5ed1a46a26217c730fdfbfc8c2eDavid Blaikie  if (P.SkipUntil(Close, /*StopAtSemi*/ true, /*DontConsume*/ true))
1905b578aee665aad5ed1a46a26217c730fdfbfc8c2eDavid Blaikie    LClose = P.ConsumeAnyToken();
19064a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return true;
19074a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor}
19083896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
1909c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorvoid BalancedDelimiterTracker::skipToEnd() {
19103896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  P.SkipUntil(Close, false);
19113896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor}
1912