Parser.cpp revision f0cc19f43d5e05dbd22d00faca8c093b7005be3f
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Parser.cpp - C Language Family Parser ----------------------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements the Parser interfaces. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h" 1555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "ParsePragma.h" 1655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "RAIIObjectsForParser.h" 1755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/ASTConsumer.h" 1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/DeclTemplate.h" 19500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h" 2019510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h" 2119510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/ParsedTemplate.h" 2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Scope.h" 230102c30896c83f70cf6b6519fd5c674cb981c0b5Chris Lattner#include "llvm/Support/raw_ostream.h" 245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2668de140b5d1df4c21cbf5ef1be2abcbfbc835cb5Mahesha S 2769b5e952c56f95673064ad1815a240e0fb595865Benjamin Kramernamespace { 28aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief A comment handler that passes comments found by the preprocessor 29aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// to the parser action. 30aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass ActionCommentHandler : public CommentHandler { 31aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko Sema &S; 32aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 33aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkopublic: 34aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko explicit ActionCommentHandler(Sema &S) : S(S) { } 35aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 36aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) { 37aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko S.ActOnComment(Comment); 38aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko return false; 39aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko } 40aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}; 4169b5e952c56f95673064ad1815a240e0fb595865Benjamin Kramer} // end anonymous namespace 42aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 43b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas GregorIdentifierInfo *Parser::getSEHExceptKeyword() { 44b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor // __except is accepted as a (contextual) keyword 454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland)) 46b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor Ident__except = PP.getIdentifierInfo("__except"); 47b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor 48b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor return Ident__except; 49b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor} 50b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor 5125893e0930c1241c3fdafc6f1b8661443bb19692Argyrios KyrtzidisParser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies) 52614f96a7cf94805c2d336639300b62dc2f54e9e0Ted Kremenek : PP(pp), Actions(actions), Diags(PP.getDiagnostics()), 530fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor GreaterThanIsOperator(true), ColonIsSacred(false), 546a91d385618ea4d28236c496f540a26877c95525Erik Verbruggen InMessageExpression(false), TemplateParameterDepth(0), 5525893e0930c1241c3fdafc6f1b8661443bb19692Argyrios Kyrtzidis ParsingInObjCContainer(false) { 5625893e0930c1241c3fdafc6f1b8661443bb19692Argyrios Kyrtzidis SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies; 572b970e9d70768802984f2cf1885e73643c17e0c2Chris Lattner Tok.startToken(); 585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Tok.setKind(tok::eof); 5923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CurScope = 0; 609e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner NumCachedScopes = 0; 615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParenCount = BracketCount = BraceCount = 0; 62849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis CurParsedObjCImpl = 0; 63fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar 64fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar // Add #pragma handlers. These are removed and destroyed in the 65fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar // destructor. 669595c7e2533c836537dc300e75d059c29feb7094Eli Friedman AlignHandler.reset(new PragmaAlignHandler()); 67cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar PP.AddPragmaHandler(AlignHandler.get()); 68cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar 699595c7e2533c836537dc300e75d059c29feb7094Eli Friedman GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler()); 70aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get()); 71aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman 729595c7e2533c836537dc300e75d059c29feb7094Eli Friedman OptionsHandler.reset(new PragmaOptionsHandler()); 739b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis PP.AddPragmaHandler(OptionsHandler.get()); 749b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis 759595c7e2533c836537dc300e75d059c29feb7094Eli Friedman PackHandler.reset(new PragmaPackHandler()); 769b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis PP.AddPragmaHandler(PackHandler.get()); 7762c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian 789595c7e2533c836537dc300e75d059c29feb7094Eli Friedman MSStructHandler.reset(new PragmaMSStructHandler()); 7962c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian PP.AddPragmaHandler(MSStructHandler.get()); 809b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis 819595c7e2533c836537dc300e75d059c29feb7094Eli Friedman UnusedHandler.reset(new PragmaUnusedHandler()); 829b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis PP.AddPragmaHandler(UnusedHandler.get()); 839b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis 849595c7e2533c836537dc300e75d059c29feb7094Eli Friedman WeakHandler.reset(new PragmaWeakHandler()); 859b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis PP.AddPragmaHandler(WeakHandler.get()); 86321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne 879595c7e2533c836537dc300e75d059c29feb7094Eli Friedman RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler()); 885f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall PP.AddPragmaHandler(RedefineExtnameHandler.get()); 895f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall 909595c7e2533c836537dc300e75d059c29feb7094Eli Friedman FPContractHandler.reset(new PragmaFPContractHandler()); 91321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne PP.AddPragmaHandler("STDC", FPContractHandler.get()); 92f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne 934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().OpenCL) { 949595c7e2533c836537dc300e75d059c29feb7094Eli Friedman OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler()); 95f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get()); 96f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne 97f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne PP.AddPragmaHandler("OPENCL", FPContractHandler.get()); 98f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne } 99c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev if (getLangOpts().OpenMP) 100c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev OpenMPHandler.reset(new PragmaOpenMPHandler()); 101c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev else 102c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev OpenMPHandler.reset(new PragmaNoOpenMPHandler()); 103c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev PP.AddPragmaHandler(OpenMPHandler.get()); 104aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 1057adf79a620cb7fbde0608e21727425930676b7dbReid Kleckner if (getLangOpts().MicrosoftExt) { 1063190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner MSCommentHandler.reset(new PragmaCommentHandler(actions)); 1077adf79a620cb7fbde0608e21727425930676b7dbReid Kleckner PP.AddPragmaHandler(MSCommentHandler.get()); 108a7ff62f9443efa3b13a28a1e566d4625b15b8553Aaron Ballman MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(actions)); 109a7ff62f9443efa3b13a28a1e566d4625b15b8553Aaron Ballman PP.AddPragmaHandler(MSDetectMismatchHandler.get()); 1107adf79a620cb7fbde0608e21727425930676b7dbReid Kleckner } 1117adf79a620cb7fbde0608e21727425930676b7dbReid Kleckner 112056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko CommentSemaHandler.reset(new ActionCommentHandler(actions)); 113056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko PP.addCommentHandler(CommentSemaHandler.get()); 114aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 115f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor PP.setCodeCompletionHandler(*this); 1165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1183cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) { 11933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis return Diags.Report(Loc, DiagID); 1201ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner} 1211ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner 1223cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) { 1231ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner return Diag(Tok.getLocation(), DiagID); 1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1264b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \brief Emits a diagnostic suggesting parentheses surrounding a 1274b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// given range. 1284b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// 1294b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param Loc The location where we'll emit the diagnostic. 13070517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko/// \param DK The kind of diagnostic to emit. 1314b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor/// \param ParenRange Source range enclosing code that should be parenthesized. 1324b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregorvoid Parser::SuggestParentheses(SourceLocation Loc, unsigned DK, 1334b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor SourceRange ParenRange) { 134b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd()); 135b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { 1364b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor // We can't display the parentheses, so just dig the 1374b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor // warning/error and return. 1384b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor Diag(Loc, DK); 1394b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor return; 1404b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor } 1411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(Loc, DK) 143849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") 144849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor << FixItHint::CreateInsertion(EndLoc, ")"); 1454b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor} 1464b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor 147837b1a37116cf4e64f8bb7db34982dee1fba7647John McCallstatic bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) { 148837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall switch (ExpectedTok) { 1494b0824229b96d024a96f3c7dd75ab70652c05c5bRichard Smith case tok::semi: 1504b0824229b96d024a96f3c7dd75ab70652c05c5bRichard Smith return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ; 151837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall default: return false; 152837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall } 153837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall} 154837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall 1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the 1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// input. If so, it is consumed and false is returned. 1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If the input is malformed, this emits the specified diagnostic. Next, if 1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is 1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// returned. 1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID, 1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *Msg, tok::TokenKind SkipToTok) { 163dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) { 1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeAnyToken(); 1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 167a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 168837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall // Detect common single-character typos and resume. 169837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall if (IsCommonTypo(ExpectedTok, Tok)) { 170837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall SourceLocation Loc = Tok.getLocation(); 171837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall Diag(Loc, DiagID) 172837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall << Msg 173837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall << FixItHint::CreateReplacement(SourceRange(Loc), 174837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall getTokenSimpleSpelling(ExpectedTok)); 175837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall ConsumeAnyToken(); 176837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall 177837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall // Pretend there wasn't a problem. 178837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall return false; 179837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall } 180837b1a37116cf4e64f8bb7db34982dee1fba7647John McCall 1814b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor const char *Spelling = 0; 182b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (EndLoc.isValid() && 184b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) { 1854b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor // Show what code to insert to fix this problem. 1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(EndLoc, DiagID) 1874b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor << Msg 188849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor << FixItHint::CreateInsertion(EndLoc, Spelling); 1894b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor } else 1904b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor Diag(Tok, DiagID) << Msg; 1914b2d3f7bcc4df31157df443af1b80bcaa9b58bbaDouglas Gregor 1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (SkipToTok != tok::unknown) 1938fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev SkipUntil(SkipToTok, StopAtSemi); 1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1979ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregorbool Parser::ExpectAndConsumeSemi(unsigned DiagID) { 1989ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) { 199fb5825dca4e95fee463fdeaddb8b729294fb4d10Douglas Gregor ConsumeToken(); 2009ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor return false; 2019ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor } 2029ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor 2039ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) && 2049ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor NextToken().is(tok::semi)) { 2059ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor Diag(Tok, diag::err_extraneous_token_before_semi) 2069ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor << PP.getSpelling(Tok) 2079ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor << FixItHint::CreateRemoval(Tok.getLocation()); 2089ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor ConsumeAnyToken(); // The ')' or ']'. 2099ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor ConsumeToken(); // The ';'. 2109ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor return false; 2119ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor } 2129ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor 2139ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor return ExpectAndConsume(tok::semi, DiagID); 2149ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor} 2159ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor 216eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smithvoid Parser::ConsumeExtraSemi(ExtraSemiKind Kind, unsigned TST) { 2174b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu if (!Tok.is(tok::semi)) return; 2184b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu 219eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith bool HadMultipleSemis = false; 2204b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu SourceLocation StartLoc = Tok.getLocation(); 2214b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu SourceLocation EndLoc = Tok.getLocation(); 2224b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu ConsumeToken(); 2234b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu 2244b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) { 225eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith HadMultipleSemis = true; 2264b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu EndLoc = Tok.getLocation(); 2274b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu ConsumeToken(); 2284b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu } 2294b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu 230eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith // C++11 allows extra semicolons at namespace scope, but not in any of the 231eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith // other contexts. 232eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith if (Kind == OutsideFunction && getLangOpts().CPlusPlus) { 23380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (getLangOpts().CPlusPlus11) 234eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi) 235eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 236eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith else 237eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith Diag(StartLoc, diag::ext_extra_semi_cxx11) 238eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 2394b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu return; 2404b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu } 2414b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu 242eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis) 243eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith Diag(StartLoc, diag::ext_extra_semi) 244eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith << Kind << DeclSpec::getSpecifierName((DeclSpec::TST)TST) 245eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 246eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith else 247eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith // A single semicolon is valid after a member function definition. 248eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def) 249eab9d6f9065b042d39fbaf9842c9d8cc968dd6d0Richard Smith << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 2504b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu} 2514b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu 2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Error recovery. 2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 256f2b87508c744d6581200fa6521f0924b7f3c0e9fBenjamin Kramerstatic bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R) { 2578fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0; 2588fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev} 2598fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev 2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipUntil - Read tokens until we get to the specified token, then consume 2618fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev/// it (unless no flag StopBeforeMatch). Because we cannot guarantee that the 2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// token will ever occur, this skips to the next token, or to some likely 2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// good stopping point. If StopAtSemi is true, skipping will stop at a ';' 2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character. 265a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// 2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// If SkipUntil finds the specified token, it returns true, otherwise it 267a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// returns false. 2688fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataevbool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, SkipUntilFlags Flags) { 2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We always want this function to skip at least one token if the first token 2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // isn't T and if not at EOF. 2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isFirstTokenSkipped = true; 2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we found one of the tokens, stop and return true. 274eb52f86a62db523e3c993686b3ed92c55d59d53cDavid Blaikie for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) { 275000732226610650837478cba97843d19b75f648eChris Lattner if (Tok.is(Toks[i])) { 2768fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev if (HasFlagsSet(Flags, StopBeforeMatch)) { 2775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Noop, don't consume the token. 2785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeAnyToken(); 2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 2825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 284a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 285d66bf94f527c6bd24089615ee3a0b74a2ec35310Rafael Espindola // Important special case: The caller has given up and just wants us to 286d66bf94f527c6bd24089615ee3a0b74a2ec35310Rafael Espindola // skip the rest of the file. Do this without recursing, since we can 287d66bf94f527c6bd24089615ee3a0b74a2ec35310Rafael Espindola // get here precisely because the caller detected too much recursion. 2888fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev if (Toks.size() == 1 && Toks[0] == tok::eof && 2898fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev !HasFlagsSet(Flags, StopAtSemi) && 2908fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev !HasFlagsSet(Flags, StopAtCodeCompletion)) { 291d66bf94f527c6bd24089615ee3a0b74a2ec35310Rafael Espindola while (Tok.getKind() != tok::eof) 292d66bf94f527c6bd24089615ee3a0b74a2ec35310Rafael Espindola ConsumeAnyToken(); 293d66bf94f527c6bd24089615ee3a0b74a2ec35310Rafael Espindola return true; 294d66bf94f527c6bd24089615ee3a0b74a2ec35310Rafael Espindola } 295d66bf94f527c6bd24089615ee3a0b74a2ec35310Rafael Espindola 2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Tok.getKind()) { 2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::eof: 2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Ran out of tokens. 2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 300dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor 301dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor case tok::code_completion: 3028fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev if (!HasFlagsSet(Flags, StopAtCodeCompletion)) 3033437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis ConsumeToken(); 304dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor return false; 305dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor 3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::l_paren: 3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Recursively skip properly-nested parens. 3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeParen(); 3098fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev if (HasFlagsSet(Flags, StopAtCodeCompletion)) 3108fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev SkipUntil(tok::r_paren, StopAtCodeCompletion); 3118fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev else 3128fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev SkipUntil(tok::r_paren); 3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::l_square: 3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Recursively skip properly-nested square brackets. 3165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeBracket(); 3178fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev if (HasFlagsSet(Flags, StopAtCodeCompletion)) 3188fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev SkipUntil(tok::r_square, StopAtCodeCompletion); 3198fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev else 3208fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev SkipUntil(tok::r_square); 3215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::l_brace: 3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Recursively skip properly-nested braces. 3245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeBrace(); 3258fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev if (HasFlagsSet(Flags, StopAtCodeCompletion)) 3268fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev SkipUntil(tok::r_brace, StopAtCodeCompletion); 3278fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev else 3288fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev SkipUntil(tok::r_brace); 3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 330a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found a ']' or '}' or ')', which we think should be balanced. 3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Since the user wasn't looking for this token (if they were, it would 3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // already be handled), this isn't balanced. If there is a LHS token at a 3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // higher level, we will assume that this matches the unbalanced token 3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // and return it. Otherwise, this is a spurious RHS token, which we skip. 3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::r_paren: 3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParenCount && !isFirstTokenSkipped) 3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; // Matches something. 3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeParen(); 3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::r_square: 3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (BracketCount && !isFirstTokenSkipped) 3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; // Matches something. 3445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeBracket(); 3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::r_brace: 3475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (BraceCount && !isFirstTokenSkipped) 3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; // Matches something. 3495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeBrace(); 3505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 351a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::string_literal: 3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::wide_string_literal: 3545cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor case tok::utf8_string_literal: 3555cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor case tok::utf16_string_literal: 3565cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor case tok::utf32_string_literal: 3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeStringToken(); 3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 35955edca9d7d6a50cbda6f036b05a0cb8d42f5a010Fariborz Jahanian 3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::semi: 3618fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev if (HasFlagsSet(Flags, StopAtSemi)) 3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH. 3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 3655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip this token. 3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeToken(); 3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer isFirstTokenSkipped = false; 370a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump } 3715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Scope manipulation 3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EnterScope - Start a new scope. 3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::EnterScope(unsigned ScopeFlags) { 3799e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner if (NumCachedScopes) { 3809e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner Scope *N = ScopeCache[--NumCachedScopes]; 38123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor N->Init(getCurScope(), ScopeFlags); 38223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CurScope = N; 3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 3849c4eb1f3438370355f51dc8c62f2ca4803e3338dArgyrios Kyrtzidis Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags); 3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ExitScope - Pop a scope off the scope stack. 3895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ExitScope() { 39023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor assert(getCurScope() && "Scope imbalance!"); 3915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 39290ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner // Inform the actions module that this scope is going away if there are any 39390ae68aae98f12fe1950c63e2f6bd0fabce6cb1eChris Lattner // decls in it. 39423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor if (!getCurScope()->decl_empty()) 39523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnPopScope(Tok.getLocation(), getCurScope()); 396a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 39723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Scope *OldScope = getCurScope(); 39823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CurScope = OldScope->getParent(); 399a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 4009e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner if (NumCachedScopes == ScopeCacheSize) 4019e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner delete OldScope; 4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer else 4039e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner ScopeCache[NumCachedScopes++] = OldScope; 4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4067a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Set the flags for the current scope to ScopeFlags. If ManageFlags is false, 4077a614d8380297fcd2bc23986241905d97222948cRichard Smith/// this object does nothing. 4087a614d8380297fcd2bc23986241905d97222948cRichard SmithParser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags, 4097a614d8380297fcd2bc23986241905d97222948cRichard Smith bool ManageFlags) 4107a614d8380297fcd2bc23986241905d97222948cRichard Smith : CurScope(ManageFlags ? Self->getCurScope() : 0) { 4117a614d8380297fcd2bc23986241905d97222948cRichard Smith if (CurScope) { 4127a614d8380297fcd2bc23986241905d97222948cRichard Smith OldFlags = CurScope->getFlags(); 4137a614d8380297fcd2bc23986241905d97222948cRichard Smith CurScope->setFlags(ScopeFlags); 4147a614d8380297fcd2bc23986241905d97222948cRichard Smith } 4157a614d8380297fcd2bc23986241905d97222948cRichard Smith} 4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4177a614d8380297fcd2bc23986241905d97222948cRichard Smith/// Restore the flags for the current scope to what they were before this 4187a614d8380297fcd2bc23986241905d97222948cRichard Smith/// object overrode them. 4197a614d8380297fcd2bc23986241905d97222948cRichard SmithParser::ParseScopeFlags::~ParseScopeFlags() { 4207a614d8380297fcd2bc23986241905d97222948cRichard Smith if (CurScope) 4217a614d8380297fcd2bc23986241905d97222948cRichard Smith CurScope->setFlags(OldFlags); 4227a614d8380297fcd2bc23986241905d97222948cRichard Smith} 4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 4265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// C99 6.9: External Definitions. 4275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerParser::~Parser() { 4305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we still have scopes active, delete the scope tree. 43123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor delete getCurScope(); 43223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CurScope = 0; 43323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor 4345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Free the scope cache. 4359e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner for (unsigned i = 0, e = NumCachedScopes; i != e; ++i) 4369e344c65b1e8b83e1d3ada507cf653526ff2c005Chris Lattner delete ScopeCache[i]; 437fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar 438fcdd8fe26de3eee44927600bf1853e21bd90dd84Daniel Dunbar // Remove the pragma handlers we installed. 439cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar PP.RemovePragmaHandler(AlignHandler.get()); 440cbb98edd530787c2ac019e437e7c599df8004ba7Daniel Dunbar AlignHandler.reset(); 441aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get()); 442aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman GCCVisibilityHandler.reset(); 4439b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis PP.RemovePragmaHandler(OptionsHandler.get()); 444861800c676004eabed5927f0552620d06c80a40aDaniel Dunbar OptionsHandler.reset(); 4459b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis PP.RemovePragmaHandler(PackHandler.get()); 4464726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek PackHandler.reset(); 44762c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian PP.RemovePragmaHandler(MSStructHandler.get()); 44862c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian MSStructHandler.reset(); 4499b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis PP.RemovePragmaHandler(UnusedHandler.get()); 4504726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek UnusedHandler.reset(); 4519b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis PP.RemovePragmaHandler(WeakHandler.get()); 4529991479ad5dde617168cc1e4b18425cdbbfd9fa9Eli Friedman WeakHandler.reset(); 4535f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall PP.RemovePragmaHandler(RedefineExtnameHandler.get()); 4545f3c163b7b19a0c7e02509a0984ee1256bca890dDavid Chisnall RedefineExtnameHandler.reset(); 455f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne 4564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().OpenCL) { 457f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get()); 458f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne OpenCLExtensionHandler.reset(); 459f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne PP.RemovePragmaHandler("OPENCL", FPContractHandler.get()); 460f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne } 461c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev PP.RemovePragmaHandler(OpenMPHandler.get()); 462c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev OpenMPHandler.reset(); 463f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne 4647adf79a620cb7fbde0608e21727425930676b7dbReid Kleckner if (getLangOpts().MicrosoftExt) { 4657adf79a620cb7fbde0608e21727425930676b7dbReid Kleckner PP.RemovePragmaHandler(MSCommentHandler.get()); 4667adf79a620cb7fbde0608e21727425930676b7dbReid Kleckner MSCommentHandler.reset(); 467a7ff62f9443efa3b13a28a1e566d4625b15b8553Aaron Ballman PP.RemovePragmaHandler(MSDetectMismatchHandler.get()); 468a7ff62f9443efa3b13a28a1e566d4625b15b8553Aaron Ballman MSDetectMismatchHandler.reset(); 4697adf79a620cb7fbde0608e21727425930676b7dbReid Kleckner } 4707adf79a620cb7fbde0608e21727425930676b7dbReid Kleckner 471321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne PP.RemovePragmaHandler("STDC", FPContractHandler.get()); 472321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne FPContractHandler.reset(); 473aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 474056e2c30050a94141150ba561268d90b4d18e378Dmitri Gribenko PP.removeCommentHandler(CommentSemaHandler.get()); 475aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 476f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor PP.clearCodeCompletionHandler(); 47713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer 47813bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer assert(TemplateIds.empty() && "Still alive TemplateIdAnnotations around?"); 4795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Initialize - Warm up the parser. 4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 4835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::Initialize() { 48431e057270232c1c37602579cb6461c2704175672Chris Lattner // Create the translation unit scope. Install it as the current scope. 48523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor assert(getCurScope() == 0 && "A scope is already active?"); 48631e057270232c1c37602579cb6461c2704175672Chris Lattner EnterScope(Scope::DeclScope); 487c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor Actions.ActOnTranslationUnitScope(getCurScope()); 488c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregor 48934870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner // Initialization for Objective-C context sensitive keywords recognition. 490a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek // Referenced in Parser::ParseObjCTypeQualifierList. 4914e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjC1) { 492a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in"); 493a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out"); 494a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout"); 495a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway"); 496a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy"); 497a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref"); 49834870da70fa42b0391b79627ebd0cfc6eb22213bChris Lattner } 499662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar 500e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor Ident_instancetype = 0; 5017eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson Ident_final = 0; 5027121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer Ident_sealed = 0; 5037eeb4ec11043d4860361348f2b19299d957d47a9Anders Carlsson Ident_override = 0; 5041f3b6fdabbb10779a473d6315154d7325ce20aeaAnders Carlsson 505662e8b5647adbb1bc9eeceece7b64600cfa87471Daniel Dunbar Ident_super = &PP.getIdentifierTable().get("super"); 50682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson 5074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().AltiVec) { 50882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson Ident_vector = &PP.getIdentifierTable().get("vector"); 50982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson Ident_pixel = &PP.getIdentifierTable().get("pixel"); 5103e3d20b2b26a885fcae855bb0b02dbc42d7c5739Bill Schmidt Ident_bool = &PP.getIdentifierTable().get("bool"); 51182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson } 5120a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor 5130a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor Ident_introduced = 0; 5140a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor Ident_deprecated = 0; 5150a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor Ident_obsoleted = 0; 516b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor Ident_unavailable = 0; 51728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 518b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor Ident__except = 0; 519b57791e5b40afa6691063c83d0e95c416fb19fdeDouglas Gregor 52028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0; 52128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0; 52228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0; 52328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 5244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if(getLangOpts().Borland) { 52528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident__exception_info = PP.getIdentifierInfo("_exception_info"); 52628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident___exception_info = PP.getIdentifierInfo("__exception_info"); 52728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation"); 52828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident__exception_code = PP.getIdentifierInfo("_exception_code"); 52928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident___exception_code = PP.getIdentifierInfo("__exception_code"); 53028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode"); 53128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination"); 53228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination"); 53328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination"); 53428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 53528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block); 53628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block); 53728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block); 53828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter); 53928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter); 54028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter); 54128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block); 54228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block); 54328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block); 54428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley } 545c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor 546c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor Actions.Initialize(); 547c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor 548c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor // Prime the lexer look-ahead. 549c7be10245e78bf38694b26f289880edefb9f16e9Douglas Gregor ConsumeToken(); 5505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 55213bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramernamespace { 55313bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer /// \brief RAIIObject to destroy the contents of a SmallVector of 55413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer /// TemplateIdAnnotation pointers and clear the vector. 55513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer class DestroyTemplateIdAnnotationsRAIIObj { 55613bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer SmallVectorImpl<TemplateIdAnnotation *> &Container; 55713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer public: 55813bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer DestroyTemplateIdAnnotationsRAIIObj(SmallVectorImpl<TemplateIdAnnotation *> 55913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer &Container) 56013bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer : Container(Container) {} 56113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer 56213bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer ~DestroyTemplateIdAnnotationsRAIIObj() { 56313bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer for (SmallVectorImpl<TemplateIdAnnotation *>::iterator I = 56413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer Container.begin(), E = Container.end(); 56513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer I != E; ++I) 56613bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer (*I)->Destroy(); 56713bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer Container.clear(); 56813bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer } 56913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer }; 57013bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer} 57113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer 5725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the 5735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// action tells us to. This returns true if the EOF was encountered. 574682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattnerbool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) { 57513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds); 576b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis 577e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann // Skip over the EOF token, flagging end of previous input for incremental 578e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann // processing 579e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof)) 580e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann ConsumeToken(); 581e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann 58226297f57634994b4ae47a0774c372d6944265bb2Richard Smith Result = DeclGroupPtrTy(); 58326297f57634994b4ae47a0774c372d6944265bb2Richard Smith switch (Tok.getKind()) { 58426297f57634994b4ae47a0774c372d6944265bb2Richard Smith case tok::annot_pragma_unused: 585b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis HandlePragmaUnused(); 58626297f57634994b4ae47a0774c372d6944265bb2Richard Smith return false; 587b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis 58826297f57634994b4ae47a0774c372d6944265bb2Richard Smith case tok::annot_module_include: 58926297f57634994b4ae47a0774c372d6944265bb2Richard Smith Actions.ActOnModuleInclude(Tok.getLocation(), 59026297f57634994b4ae47a0774c372d6944265bb2Richard Smith reinterpret_cast<Module *>( 59126297f57634994b4ae47a0774c372d6944265bb2Richard Smith Tok.getAnnotationValue())); 59226297f57634994b4ae47a0774c372d6944265bb2Richard Smith ConsumeToken(); 59326297f57634994b4ae47a0774c372d6944265bb2Richard Smith return false; 59426297f57634994b4ae47a0774c372d6944265bb2Richard Smith 59526297f57634994b4ae47a0774c372d6944265bb2Richard Smith case tok::eof: 5968387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet // Late template parsing can begin. 5974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().DelayedTemplateParsing) 5988387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet Actions.SetLateTemplateParser(LateTemplateParserCallback, this); 599e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann if (!PP.isIncrementalProcessingEnabled()) 600e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann Actions.ActOnEndOfTranslationUnit(); 601e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann //else don't tell Sema that we ended parsing: more input might come. 6029299f3fa85796613cc787a2062c9562d07c8613eChris Lattner return true; 60326297f57634994b4ae47a0774c372d6944265bb2Richard Smith 60426297f57634994b4ae47a0774c372d6944265bb2Richard Smith default: 60526297f57634994b4ae47a0774c372d6944265bb2Richard Smith break; 6069299f3fa85796613cc787a2062c9562d07c8613eChris Lattner } 607a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 6080b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall ParsedAttributesWithRange attrs(AttrFactory); 6094e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith MaybeParseCXX11Attributes(attrs); 6107f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall MaybeParseMicrosoftAttributes(attrs); 611e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann 6127f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall Result = ParseExternalDeclaration(attrs); 6135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 6145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseExternalDeclaration: 61790b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner/// 618c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor/// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl] 619c3018153a11afe91849748a93d920040a571b76cChris Lattner/// function-definition 620c3018153a11afe91849748a93d920040a571b76cChris Lattner/// declaration 6215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-definition 622c3018153a11afe91849748a93d920040a571b76cChris Lattner/// [GNU] __extension__ external-declaration 6235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC] objc-class-definition 6245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC] objc-class-declaration 6255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC] objc-alias-declaration 6265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC] objc-protocol-definition 6275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC] objc-method-definition 6285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC] @end 629c19923dda3d28f67aab4726cd40bb07032758383Douglas Gregor/// [C++] linkage-specification 6305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-definition: 6315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// simple-asm-expr ';' 6326b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith/// [C++11] empty-declaration 6336b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith/// [C++11] attribute-declaration 6345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 6356b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith/// [C++11] empty-declaration: 636a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// ';' 637a1d71aea847a50b3acbd187d2ae9e5c1ead0f4e2Douglas Gregor/// 63845f965581935791a018df829a14dff53c1dd8f47Douglas Gregor/// [C++0x/GNU] 'extern' 'template' declaration 6397f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::DeclGroupPtrTy 6407f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallParser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs, 6417f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall ParsingDeclSpec *DS) { 64213bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds); 64336d36806f1972f7ec1d2a3f59155187278c56508Argyrios Kyrtzidis ParenBraceBracketBalancer BalancerRAIIObj(*this); 6447d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 6457d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (PP.isCodeCompletionReached()) { 6467d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffParsing(); 6477d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return DeclGroupPtrTy(); 6487d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 6497d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 650d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *SingleDecl = 0; 6515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Tok.getKind()) { 652426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola case tok::annot_pragma_vis: 653426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola HandlePragmaVisibility(); 654426fc94ed3bce15b55c43692537e3833388f0352Rafael Espindola return DeclGroupPtrTy(); 655aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman case tok::annot_pragma_pack: 656aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman HandlePragmaPack(); 657aa5ab26ed93382b812147f532dcbf4afb5494040Eli Friedman return DeclGroupPtrTy(); 6589595c7e2533c836537dc300e75d059c29feb7094Eli Friedman case tok::annot_pragma_msstruct: 6599595c7e2533c836537dc300e75d059c29feb7094Eli Friedman HandlePragmaMSStruct(); 6609595c7e2533c836537dc300e75d059c29feb7094Eli Friedman return DeclGroupPtrTy(); 6619595c7e2533c836537dc300e75d059c29feb7094Eli Friedman case tok::annot_pragma_align: 6629595c7e2533c836537dc300e75d059c29feb7094Eli Friedman HandlePragmaAlign(); 6639595c7e2533c836537dc300e75d059c29feb7094Eli Friedman return DeclGroupPtrTy(); 6649595c7e2533c836537dc300e75d059c29feb7094Eli Friedman case tok::annot_pragma_weak: 6659595c7e2533c836537dc300e75d059c29feb7094Eli Friedman HandlePragmaWeak(); 6669595c7e2533c836537dc300e75d059c29feb7094Eli Friedman return DeclGroupPtrTy(); 6679595c7e2533c836537dc300e75d059c29feb7094Eli Friedman case tok::annot_pragma_weakalias: 6689595c7e2533c836537dc300e75d059c29feb7094Eli Friedman HandlePragmaWeakAlias(); 6699595c7e2533c836537dc300e75d059c29feb7094Eli Friedman return DeclGroupPtrTy(); 6709595c7e2533c836537dc300e75d059c29feb7094Eli Friedman case tok::annot_pragma_redefine_extname: 6719595c7e2533c836537dc300e75d059c29feb7094Eli Friedman HandlePragmaRedefineExtname(); 6729595c7e2533c836537dc300e75d059c29feb7094Eli Friedman return DeclGroupPtrTy(); 6739595c7e2533c836537dc300e75d059c29feb7094Eli Friedman case tok::annot_pragma_fp_contract: 6749595c7e2533c836537dc300e75d059c29feb7094Eli Friedman HandlePragmaFPContract(); 6759595c7e2533c836537dc300e75d059c29feb7094Eli Friedman return DeclGroupPtrTy(); 6769595c7e2533c836537dc300e75d059c29feb7094Eli Friedman case tok::annot_pragma_opencl_extension: 6779595c7e2533c836537dc300e75d059c29feb7094Eli Friedman HandlePragmaOpenCLExtension(); 6789595c7e2533c836537dc300e75d059c29feb7094Eli Friedman return DeclGroupPtrTy(); 679c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev case tok::annot_pragma_openmp: 680c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev ParseOpenMPDeclarativeDirective(); 681c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev return DeclGroupPtrTy(); 6825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::semi: 6836b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith // Either a C++11 empty-declaration or attribute-declaration. 684684aa73192d92850a926870be62a1787eb5b7ed9Michael Han SingleDecl = Actions.ActOnEmptyDeclaration(getCurScope(), 685684aa73192d92850a926870be62a1787eb5b7ed9Michael Han attrs.getList(), 686684aa73192d92850a926870be62a1787eb5b7ed9Michael Han Tok.getLocation()); 6874b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu ConsumeExtraSemi(OutsideFunction); 688684aa73192d92850a926870be62a1787eb5b7ed9Michael Han break; 68990b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner case tok::r_brace: 690883692ebd421c40b44e2c2665e5f54dade5621bcNico Weber Diag(Tok, diag::err_extraneous_closing_brace); 69190b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner ConsumeBrace(); 692682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return DeclGroupPtrTy(); 69390b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner case tok::eof: 69490b93d6f6961edd8f17e089253d655892adc1ef7Chris Lattner Diag(Tok, diag::err_expected_external_declaration); 695682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return DeclGroupPtrTy(); 696c3018153a11afe91849748a93d920040a571b76cChris Lattner case tok::kw___extension__: { 697c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner // __extension__ silences extension warnings in the subexpression. 698c46d1a1f8af67a87689d7db9eaf96027282ccaeaChris Lattner ExtensionRAIIObject O(Diags); // Use RAII to do this. 69939146d6497ad5e7ca8ef639221e7b3e15d07c888Chris Lattner ConsumeToken(); 7007f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall return ParseExternalDeclaration(attrs); 701c3018153a11afe91849748a93d920040a571b76cChris Lattner } 702dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson case tok::kw_asm: { 7037f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall ProhibitAttributes(attrs); 704bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 70521e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara SourceLocation StartLoc = Tok.getLocation(); 70621e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara SourceLocation EndLoc; 70721e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara ExprResult Result(ParseSimpleAsm(&EndLoc)); 708a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 7093f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 7103f9424f3206b834b5dd0e7c403348651ab6fafbbAnders Carlsson "top-level asm block"); 711dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson 712682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner if (Result.isInvalid()) 713682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return DeclGroupPtrTy(); 71421e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc); 715682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner break; 716dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson } 7175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::at: 71895ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian return ParseObjCAtDirectives(); 7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::minus: 7205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::plus: 7214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().ObjC1) { 722682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner Diag(Tok, diag::err_expected_external_declaration); 723682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner ConsumeToken(); 724682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return DeclGroupPtrTy(); 725682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner } 726682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner SingleDecl = ParseObjCMethodDefinition(); 727682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner break; 728791215b7a24666912c0b71175d2ca5ba082f666eDouglas Gregor case tok::code_completion: 72923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.CodeCompleteOrdinaryName(getCurScope(), 730849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis CurParsedObjCImpl? Sema::PCC_ObjCImplementation 731f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall : Sema::PCC_Namespace); 7327d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffParsing(); 7337d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return DeclGroupPtrTy(); 734f780abc21c39cd4731b9e38f2d2d9f7d1510bd7bDouglas Gregor case tok::kw_using: 7358f08cb7d0b97786b17ef05e05caa55aad4d6bd39Chris Lattner case tok::kw_namespace: 7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case tok::kw_typedef: 737adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor case tok::kw_template: 738adcac8824a9cff13f1ef61a69e38c1041cba12eeDouglas Gregor case tok::kw_export: // As in 'export template' 739511d7aba3b12853fdb88729a0313b80a60eab8adAnders Carlsson case tok::kw_static_assert: 740c6eb44b321c543c5bcf28727228a0cceced57e2ePeter Collingbourne case tok::kw__Static_assert: 74126d6023cb0d343bf8fc8836f97d39709bbd4afa0Chad Rosier // A function definition cannot start with any of these keywords. 74297144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner { 74397144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner SourceLocation DeclEnd; 7444e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer StmtVector Stmts; 7457f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 74697144fc41a9419bf6d74fc9450e8ef3f6e11f7e0Chris Lattner } 747d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl 7487306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor case tok::kw_static: 7497306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor // Parse (then ignore) 'static' prior to a template instantiation. This is 7507306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor // a GCC extension that we intentionally do not support. 7514e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) { 7527306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored) 7537306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor << 0; 754d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl SourceLocation DeclEnd; 7554e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer StmtVector Stmts; 7567f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 7577306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor } 7587306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor goto dont_know; 7597306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor 7607306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor case tok::kw_inline: 7614e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) { 7627306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor tok::TokenKind NextKind = NextToken().getKind(); 7637306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor 7647306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor // Inline namespaces. Allowed as an extension even in C++03. 7657306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor if (NextKind == tok::kw_namespace) { 7667306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor SourceLocation DeclEnd; 7674e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer StmtVector Stmts; 7687f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 7697306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor } 7707306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor 7717306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor // Parse (then ignore) 'inline' prior to a template instantiation. This is 7727306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor // a GCC extension that we intentionally do not support. 7737306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor if (NextKind == tok::kw_template) { 7747306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored) 7757306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor << 1; 7767306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor SourceLocation DeclEnd; 7774e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer StmtVector Stmts; 7787f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 7797306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor } 780d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl } 781d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl goto dont_know; 782d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl 78345f965581935791a018df829a14dff53c1dd8f47Douglas Gregor case tok::kw_extern: 7844e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) { 78545f965581935791a018df829a14dff53c1dd8f47Douglas Gregor // Extern templates 78645f965581935791a018df829a14dff53c1dd8f47Douglas Gregor SourceLocation ExternLoc = ConsumeToken(); 78745f965581935791a018df829a14dff53c1dd8f47Douglas Gregor SourceLocation TemplateLoc = ConsumeToken(); 78880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith Diag(ExternLoc, getLangOpts().CPlusPlus11 ? 7899324583ad2afd09db8c9967cd05c4fa44bac9555Richard Smith diag::warn_cxx98_compat_extern_template : 7909324583ad2afd09db8c9967cd05c4fa44bac9555Richard Smith diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc); 79145f965581935791a018df829a14dff53c1dd8f47Douglas Gregor SourceLocation DeclEnd; 79245f965581935791a018df829a14dff53c1dd8f47Douglas Gregor return Actions.ConvertDeclToDeclGroup( 7939241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis ParseExplicitInstantiation(Declarator::FileContext, 7949241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis ExternLoc, TemplateLoc, DeclEnd)); 79545f965581935791a018df829a14dff53c1dd8f47Douglas Gregor } 79645f965581935791a018df829a14dff53c1dd8f47Douglas Gregor // FIXME: Detect C++ linkage specifications here? 797d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl goto dont_know; 7981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 799f986038beed360c031de8654cfba43a5d3184605Francois Pichet case tok::kw___if_exists: 800f986038beed360c031de8654cfba43a5d3184605Francois Pichet case tok::kw___if_not_exists: 801563a645de82231a55e221fe655b7188bf8369662Francois Pichet ParseMicrosoftIfExistsExternalDeclaration(); 802f986038beed360c031de8654cfba43a5d3184605Francois Pichet return DeclGroupPtrTy(); 8036aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 8045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 805d078e641450bbc5a20df8d3b54f87b27e398acb3Sebastian Redl dont_know: 8065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We can't tell whether this is a function-definition or declaration yet. 80720af49a7c5bdb6cca5f4d6586106ef1ce8579311Rafael Espindola return ParseDeclarationOrFunctionDefinition(attrs, DS); 8085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 810682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner // This routine returns a DeclGroup, if the thing we parsed only contains a 811682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner // single decl, convert it now. 812682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return Actions.ConvertDeclToDeclGroup(SingleDecl); 8135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8151426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a 8161426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, continues a declaration or declaration list. 817e4246a633b13197634225971b25df0cbdcec0c5dSean Huntbool Parser::isDeclarationAfterDeclarator() { 818e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt // Check for '= delete' or '= default' 8194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) { 820e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt const Token &KW = NextToken(); 821e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt if (KW.is(tok::kw_default) || KW.is(tok::kw_delete)) 822e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt return false; 823e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt } 8246c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian 8251426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor return Tok.is(tok::equal) || // int X()= -> not a function def 8261426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor Tok.is(tok::comma) || // int X(), -> not a function def 8271426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor Tok.is(tok::semi) || // int X(); -> not a function def 8281426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def 8291426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def 8304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie (getLangOpts().CPlusPlus && 83139700f81c5b42e6be93be10275602915f872fc86Fariborz Jahanian Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++] 8321426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor} 8331426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor 8341426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// \brief Determine whether the current token, if it occurs after a 8351426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor/// declarator, indicates the start of a function definition. 836004659a56916f2f81ede507c12516c146d6c0df3Chris Lattnerbool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) { 837075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator"); 8385d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner if (Tok.is(tok::l_brace)) // int X() {} 8395d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner return true; 8405d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner 841004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner // Handle K&R C argument lists: int X(f) int f; {} 8424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().CPlusPlus && 843075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara Declarator.getFunctionTypeInfo().isKNRPrototype()) 844004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner return isDeclarationSpecifier(); 845e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt 8464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) { 847e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt const Token &KW = NextToken(); 848e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt return KW.is(tok::kw_default) || KW.is(tok::kw_delete); 849e4246a633b13197634225971b25df0cbdcec0c5dSean Hunt } 850004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner 8515d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner return Tok.is(tok::colon) || // X() : Base() {} (used for ctors) 8525d1c6198cfe55f8de025902c621c0721b640ff60Chris Lattner Tok.is(tok::kw_try); // X() try { ... } 8531426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor} 8541426e534b4fca6a05b1120d634aae46be79ca17cDouglas Gregor 8555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or 8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// a declaration. We can't tell which we have until we read up to the 857c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// compound-statement in function-definition. TemplateParams, if 858c4b4e7b8f6ca9b036824e048af49cd2a52b57cdfDouglas Gregor/// non-NULL, provides the template parameters when we're parsing a 8591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ template-declaration. 8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// function-definition: [C99 6.9.1] 862a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// decl-specs declarator declaration-list[opt] compound-statement 863a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result 864a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement 865a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// 8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// declaration: [C99 6.7] 867697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner/// declaration-specifiers init-declarator-list[opt] ';' 868697e15f2a028f8997cccb24ecd05099988cfb1a9Chris Lattner/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode] 8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OMP] threadprivate-directive [TODO] 8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 871682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerParser::DeclGroupPtrTy 8722edf0a2520313cde900799b1eb9bd11c9c776afeSean HuntParser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs, 8732edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt ParsingDeclSpec &DS, 8742edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt AccessSpecifier AS) { 8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Parse the common declaration-specifiers piece. 8760efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level); 877a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 878f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling // If we had a free-standing type definition with a missing semicolon, we 879f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling // may get this far before the problem becomes obvious. 880f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling if (DS.hasTagDefinition() && 881f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_top_level)) 882f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling return DeclGroupPtrTy(); 883f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling 8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" 8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // declaration-specifiers init-declarator-list[opt] ';' 886000732226610650837478cba97843d19b75f648eChris Lattner if (Tok.is(tok::semi)) { 8872edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt ProhibitAttributes(attrs); 8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeToken(); 889d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); 89054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall DS.complete(TheDecl); 891682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return Actions.ConvertDeclToDeclGroup(TheDecl); 8925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 893a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 8942edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt DS.takeAttributesFrom(attrs); 8952edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 896246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar // ObjC2 allows prefix attributes on class interfaces and protocols. 897246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar // FIXME: This still needs better diagnostics. We should only accept 898246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar // attributes here, no types, etc. 8994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjC2 && Tok.is(tok::at)) { 900dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff SourceLocation AtLoc = ConsumeToken(); // the "@" 9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (!Tok.isObjCAtKeyword(tok::objc_interface) && 902246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar !Tok.isObjCAtKeyword(tok::objc_protocol)) { 903246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar Diag(Tok, diag::err_objc_unexpected_attr); 904cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner SkipUntil(tok::semi); // FIXME: better skip? 905682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return DeclGroupPtrTy(); 906cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner } 907d8ac05753dc4506224d445ff98399c01da3136e5John McCall 90854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall DS.abort(); 90954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall 9100de2ae28c603322f05e2d9200c7d457c8b928983Fariborz Jahanian const char *PrevSpec = 0; 911fec54013fcd0eb72642741584ca04c1bc292bef8John McCall unsigned DiagID; 912fec54013fcd0eb72642741584ca04c1bc292bef8John McCall if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID)) 913fec54013fcd0eb72642741584ca04c1bc292bef8John McCall Diag(AtLoc, DiagID) << PrevSpec; 9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 915246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar if (Tok.isObjCAtKeyword(tok::objc_protocol)) 916bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes()); 917bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor 918bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor return Actions.ConvertDeclToDeclGroup( 919bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes())); 920dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff } 921a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 922c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner // If the declspec consisted only of 'extern' and we have a string 923c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner // literal following it, this must be a C++ linkage specifier like 924c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner // 'extern "C"'. 9254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Tok.is(tok::string_literal) && getLangOpts().CPlusPlus && 926c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner DS.getStorageClassSpec() == DeclSpec::SCS_extern && 927682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) { 928d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext); 929682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner return Actions.ConvertDeclToDeclGroup(TheDecl); 930682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner } 931c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 932d8ac05753dc4506224d445ff98399c01da3136e5John McCall return ParseDeclGroup(DS, Declarator::FileContext, true); 9335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 9345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9353acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz JahanianParser::DeclGroupPtrTy 9362edf0a2520313cde900799b1eb9bd11c9c776afeSean HuntParser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs, 9372edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt ParsingDeclSpec *DS, 9383acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian AccessSpecifier AS) { 9392edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt if (DS) { 9402edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt return ParseDeclOrFunctionDefInternal(attrs, *DS, AS); 9412edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt } else { 9422edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt ParsingDeclSpec PDS(*this); 9432edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt // Must temporarily exit the objective-c container scope for 9442edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt // parsing c constructs and re-enter objc container scope 9452edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt // afterwards. 9462edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt ObjCDeclContextSwitch ObjCDC(*this); 9472edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 9482edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt return ParseDeclOrFunctionDefInternal(attrs, PDS, AS); 9492edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt } 9503acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian} 9513acd9aaa4ddd14afecb4f1c02ca6f585a6d51849Fariborz Jahanian 95232b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali 95332b5ca012c42d418c9ff0d4175b17f174459e187Faisal Valistatic inline bool isFunctionDeclaratorRequiringReturnTypeDeduction( 95432b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali const Declarator &D) { 95532b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali if (!D.isFunctionDeclarator() || !D.getDeclSpec().containsPlaceholderType()) 95632b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali return false; 95732b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) { 95832b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali unsigned chunkIndex = E - I - 1; 95932b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali const DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex); 96032b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali if (DeclType.Kind == DeclaratorChunk::Function) { 96132b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; 96232b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali if (!FTI.hasTrailingReturnType()) 96332b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali return true; 96432b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali QualType TrailingRetType = FTI.getTrailingReturnType().get(); 96532b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali return TrailingRetType->getCanonicalTypeInternal() 96632b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali ->getContainedAutoType(); 96732b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali } 96832b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali } 96932b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali return false; 97032b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali} 97132b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali 9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseFunctionDefinition - We parsed and verified that the specified 9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Declarator is well formed. If this is a K&R-style function, read the 9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// parameters declaration-list, then start the compound-statement. 9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 976a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// function-definition: [C99 6.9.1] 977a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// decl-specs declarator declaration-list[opt] compound-statement 978a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner/// [C90] function-definition: [C99 6.7.1] - implicit int result 979a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement 9807ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4] 98123c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner/// decl-specifier-seq[opt] declarator ctor-initializer[opt] 98223c4b1883b13dc17484b7214091b73f3ba29096eChris Lattner/// function-body 9837ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor/// [C++] function-definition: [C++ 8.4] 984d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl/// decl-specifier-seq[opt] declarator function-try-block 9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 986d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseFunctionDefinition(ParsingDeclarator &D, 987c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins const ParsedTemplateInfo &TemplateInfo, 988c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins LateParsedAttrList *LateParsedAttrs) { 98928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley // Poison the SEH identifiers so they are flagged as illegal in function bodies 99028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); 991075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); 992a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 993a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner // If this is C90 and the declspecs were completely missing, fudge in an 994a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner // implicit int. We do this here because this is the only place where 995a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner // declaration-specifiers are completely optional in the grammar. 9964e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) { 997a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner const char *PrevSpec; 998fec54013fcd0eb72642741584ca04c1bc292bef8John McCall unsigned DiagID; 99931c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int, 100031c286803c60c59d314525e047d0e72f9a1cb55bChris Lattner D.getIdentifierLoc(), 1001fec54013fcd0eb72642741584ca04c1bc292bef8John McCall PrevSpec, DiagID); 1002ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin()); 1003a798ebc82627ea9cb7a00da07d2b60f9f2114f69Chris Lattner } 1004a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this declaration was formed with a K&R-style identifier list for the 10065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // arguments, parse declarations for all of the args next. 10075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // int foo(a,b) int a; float b; {} 1008004659a56916f2f81ede507c12516c146d6c0df3Chris Lattner if (FTI.isKNRPrototype()) 10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParseKNRParamDeclarations(D); 10105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10117ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor // We should have either an opening brace or, in a C++ constructor, 10127ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor // we may have a colon. 1013758afbcc86ef15f8d433f5f87db1495e50effeb3Douglas Gregor if (Tok.isNot(tok::l_brace) && 10144e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie (!getLangOpts().CPlusPlus || 1015cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) && 1016cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt Tok.isNot(tok::equal)))) { 10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Diag(Tok, diag::err_expected_fn_body); 10185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over garbage, until we get to '{'. Don't eat the '{'. 10208fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch); 1021a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we didn't find the '{', bail out. 1023000732226610650837478cba97843d19b75f648eChris Lattner if (Tok.isNot(tok::l_brace)) 1024d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall return 0; 10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1026a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 1027c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins // Check to make sure that any normal attributes are allowed to be on 1028c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins // a definition. Late parsed attributes are checked at the end. 1029c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins if (Tok.isNot(tok::equal)) { 1030c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins AttributeList *DtorAttrs = D.getAttributes(); 1031c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins while (DtorAttrs) { 1032cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith if (!IsThreadSafetyAttribute(DtorAttrs->getName()->getName()) && 1033cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith !DtorAttrs->isCXX11Attribute()) { 1034c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition) 1035c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins << DtorAttrs->getName()->getName(); 1036c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins } 1037c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins DtorAttrs = DtorAttrs->getNext(); 1038c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins } 1039c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins } 1040c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins 10418387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet // In delayed template parsing mode, for function template we consume the 10428387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet // tokens and store them for late parsing at the end of the translation unit. 1043645526c3e42170e356f792b1bc0ac2acb65c26c4David Majnemer if (getLangOpts().DelayedTemplateParsing && Tok.isNot(tok::equal) && 1044645526c3e42170e356f792b1bc0ac2acb65c26c4David Majnemer TemplateInfo.Kind == ParsedTemplateInfo::Template && 104532b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali !D.getDeclSpec().isConstexprSpecified() && 104632b5ca012c42d418c9ff0d4175b17f174459e187Faisal Vali !isFunctionDeclaratorRequiringReturnTypeDeduction(D)) { 10475354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams); 10488387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet 10498387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); 10508387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet Scope *ParentScope = getCurScope()->getParent(); 10518387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet 105245fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor D.setFunctionDefinitionKind(FDK_Definition); 10538387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet Decl *DP = Actions.HandleDeclarator(ParentScope, D, 10543fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer TemplateParameterLists); 10558387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet D.complete(DP); 10568387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet D.getMutableDeclSpec().abort(); 10578387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet 1058ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith CachedTokens Toks; 1059ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith LexTemplateFunctionForLateParsing(Toks); 10608387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet 1061ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith if (DP) { 10628387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet FunctionDecl *FnD = 0; 10638387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP)) 10648387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet FnD = FunTmpl->getTemplatedDecl(); 10658387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet else 10668387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet FnD = cast<FunctionDecl>(DP); 10678387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet 1068ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith Actions.CheckForFunctionRedefinition(FnD); 1069ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith Actions.MarkAsLateParsedTemplate(FnD, DP, Toks); 10708387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet } 10718387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet return DP; 10728387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet } 10732eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian else if (CurParsedObjCImpl && 10749e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian !TemplateInfo.TemplateParams && 10759e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) || 10769e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian Tok.is(tok::colon)) && 1077be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian Actions.CurContext->isTranslationUnit()) { 1078be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); 1079be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian Scope *ParentScope = getCurScope()->getParent(); 1080be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian 1081be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian D.setFunctionDefinitionKind(FDK_Definition); 1082be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D, 10835354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer MultiTemplateParamsArg()); 1084be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian D.complete(FuncDecl); 1085be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian D.getMutableDeclSpec().abort(); 1086be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian if (FuncDecl) { 1087be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian // Consume the tokens and store them for later parsing. 1088be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian StashAwayMethodOrFunctionBodyTokens(FuncDecl); 1089be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian CurParsedObjCImpl->HasCFunction = true; 1090be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian return FuncDecl; 1091be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian } 1092be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian } 1093be1d4ecb6885872f9d4e02d3afafdc9532eeb350Fariborz Jahanian 1094b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner // Enter a scope for the function body. 10958935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); 1096a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 1097b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner // Tell the actions module that we have entered a function definition with the 1098b652cea7d7b70ebe3744fb6d72c9ad9cf3c95429Chris Lattner // specified Declarator for the function. 1099d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *Res = TemplateInfo.TemplateParams? 110023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnStartOfFunctionTemplateDef(getCurScope(), 11015354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer *TemplateInfo.TemplateParams, D) 110223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor : Actions.ActOnStartOfFunctionDef(getCurScope(), D); 1103a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 110454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall // Break out of the ParsingDeclarator context before we parse the body. 110554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall D.complete(Res); 110654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall 110754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall // Break out of the ParsingDeclSpec context, too. This const_cast is 110854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall // safe because we're always the sole owner. 110954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall D.getMutableDeclSpec().abort(); 111054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall 1111cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt if (Tok.is(tok::equal)) { 11124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='"); 1113cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt ConsumeToken(); 1114cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt 1115cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt Actions.ActOnFinishFunctionBody(Res, 0, false); 1116cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt 1117cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt bool Delete = false; 1118cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt SourceLocation KWLoc; 1119cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt if (Tok.is(tok::kw_delete)) { 112080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith Diag(Tok, getLangOpts().CPlusPlus11 ? 11217fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith diag::warn_cxx98_compat_deleted_function : 1122d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith diag::ext_deleted_function); 1123cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt 1124cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt KWLoc = ConsumeToken(); 1125cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt Actions.SetDeclDeleted(Res, KWLoc); 1126cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt Delete = true; 1127cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt } else if (Tok.is(tok::kw_default)) { 112880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith Diag(Tok, getLangOpts().CPlusPlus11 ? 11297fe6208c3fa91f835813bb78236ef5c2bbf81053Richard Smith diag::warn_cxx98_compat_defaulted_function : 1130d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith diag::ext_defaulted_function); 1131cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt 1132cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt KWLoc = ConsumeToken(); 1133cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt Actions.SetDeclDefaulted(Res, KWLoc); 1134cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt } else { 1135cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt llvm_unreachable("function definition after = not 'delete' or 'default'"); 1136cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt } 1137cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt 1138cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt if (Tok.is(tok::comma)) { 1139cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) 1140cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt << Delete; 1141cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt SkipUntil(tok::semi); 1142cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt } else { 1143cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 1144cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt Delete ? "delete" : "default", tok::semi); 1145cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt } 1146cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt 1147cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt return Res; 1148cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt } 1149cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt 1150d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl if (Tok.is(tok::kw_try)) 1151c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor return ParseFunctionTryBlock(Res, BodyScope); 1152d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl 11537ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor // If we have a colon, then we're probably parsing a C++ 11547ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor // ctor-initializer. 1155d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall if (Tok.is(tok::colon)) { 11567ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor ParseConstructorInitializer(Res); 1157d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall 1158d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall // Recover from error. 1159d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall if (!Tok.is(tok::l_brace)) { 1160c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor BodyScope.Exit(); 11619ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Actions.ActOnFinishFunctionBody(Res, 0); 1162d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall return Res; 1163d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall } 1164d6ca8da0f5a4115813055729faaa5128e994806dJohn McCall } else 1165393612e6c7727f1fee50039254d9f434364cc0b2Fariborz Jahanian Actions.ActOnDefaultCtorInitializers(Res); 11667ad8390f7992ab7f19b1460c5f0b9d96f165c4e9Douglas Gregor 1167c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins // Late attributes are parsed in the same scope as the function body. 1168c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins if (LateParsedAttrs) 1169c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins ParseLexedAttributeList(*LateParsedAttrs, Res, false, true); 1170c24a2335677f3d1bd2cab1019ac445d650f52123DeLesley Hutchins 1171c9977d09a2de7f7d2245973413d4caf86c736640Douglas Gregor return ParseFunctionStatementBody(Res, BodyScope); 11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides 11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// types for a function with a K&R-style identifier list for arguments. 11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid Parser::ParseKNRParamDeclarations(Declarator &D) { 11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We know that the top-level of this declarator is a function. 1178075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); 11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 118004421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner // Enter function-declaration scope, limiting any declarators to the 118104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner // function prototype scope, including parameter declarators. 11823a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | 11833a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith Scope::FunctionDeclarationScope | Scope::DeclScope); 118404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner 11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read all the argument declarations. 11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isDeclarationSpecifier()) { 11875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation DSStart = Tok.getLocation(); 1188a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Parse the common declaration-specifiers piece. 11900b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall DeclSpec DS(AttrFactory); 11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParseDeclarationSpecifiers(DS); 1192a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.9.1p6: 'each declaration in the declaration list shall have at 11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // least one declarator'. 11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // NOTE: GCC just makes this an ext-warn. It's not clear what it does with 11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the declarations though. It's trivial to ignore them, really hard to do 11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // anything else with them. 1198000732226610650837478cba97843d19b75f648eChris Lattner if (Tok.is(tok::semi)) { 11995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Diag(DSStart, diag::err_declaration_does_not_declare_param); 12005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeToken(); 12015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer continue; 12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1203a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 12045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other 12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // than register. 12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && 12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer DS.getStorageClassSpec() != DeclSpec::SCS_register) { 12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Diag(DS.getStorageClassSpecLoc(), 12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer diag::err_invalid_storage_class_in_func_decl); 12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer DS.ClearStorageClassSpecs(); 12115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1212ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith if (DS.getThreadStorageClassSpec() != DeclSpec::TSCS_unspecified) { 1213ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith Diag(DS.getThreadStorageClassSpecLoc(), 12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer diag::err_invalid_storage_class_in_func_decl); 12155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer DS.ClearStorageClassSpecs(); 12165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1217a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Parse the first declarator attached to this declspec. 12195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext); 12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParseDeclarator(ParmDeclarator); 12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Handle the full declarator list. 12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If attributes are present, parse them. 12257f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall MaybeParseGNUAttributes(ParmDeclarator); 1226a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Ask the actions module to compute the type for this declarator. 1228d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *Param = 122923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); 1230152b4e4652baedfceba1cd8115515629225e713fManuel Klimek 1231a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump if (Param && 12325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // A missing identifier has already been diagnosed. 12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParmDeclarator.getIdentifier()) { 12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan the argument list looking for the correct param to apply this 12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // type. 12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0; ; ++i) { 12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.9.1p6: those declarators shall declare only identifiers from 12395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the identifier list. 12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (i == FTI.NumArgs) { 12411ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param) 12426898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner << ParmDeclarator.getIdentifier(); 12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1245a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) { 12475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Reject redefinitions of parameters. 124804421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner if (FTI.ArgInfo[i].Param) { 12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Diag(ParmDeclarator.getIdentifierLoc(), 12501ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner diag::err_param_redefinition) 12516898e33d0b28346a4dbe9a666e0e4188fea80460Chris Lattner << ParmDeclarator.getIdentifier(); 12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 125304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner FTI.ArgInfo[i].Param = Param; 12545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 12565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we don't have a comma, it is either the end of the list (a ';') or 12615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // an error, bail out. 1262000732226610650837478cba97843d19b75f648eChris Lattner if (Tok.isNot(tok::comma)) 12635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 1264a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 12657984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith ParmDeclarator.clear(); 12667984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith 12675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Consume the comma. 12687984de35644701c0d94336da7f2215d4c26d9f5bRichard Smith ParmDeclarator.setCommaLoc(ConsumeToken()); 1269a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 12705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Parse the next declarator. 12715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParseDeclarator(ParmDeclarator); 12725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1273a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 12748bb21d32e9ccc9d9c221506dff26acafa8724ccaChris Lattner if (ExpectAndConsumeSemi(diag::err_expected_semi_declaration)) { 12755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip to end of block or statement 12768fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev SkipUntil(tok::semi); 1277000732226610650837478cba97843d19b75f648eChris Lattner if (Tok.is(tok::semi)) 12785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConsumeToken(); 12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1281a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The actions module must verify that all arguments were declared. 128323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation()); 12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseAsmStringLiteral - This is just a normal string-literal, but is not 12885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// allowed to be a wide string, and is not subject to character translation. 12895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 12905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] asm-string-literal: 12915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// string-literal 12925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 129360d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseAsmStringLiteral() { 12947f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek switch (Tok.getKind()) { 12957f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek case tok::string_literal: 12967f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek break; 129799831e4677a7e2e051af636221694d60ba31fcdbRichard Smith case tok::utf8_string_literal: 129899831e4677a7e2e051af636221694d60ba31fcdbRichard Smith case tok::utf16_string_literal: 129999831e4677a7e2e051af636221694d60ba31fcdbRichard Smith case tok::utf32_string_literal: 13007f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek case tok::wide_string_literal: { 13017f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek SourceLocation L = Tok.getLocation(); 13027f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek Diag(Tok, diag::err_asm_operand_wide_string_literal) 130399831e4677a7e2e051af636221694d60ba31fcdbRichard Smith << (Tok.getKind() == tok::wide_string_literal) 13047f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek << SourceRange(L, L); 13057f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek return ExprError(); 13067f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek } 13077f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek default: 130897f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs Diag(Tok, diag::err_expected_string_literal) 130997f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs << /*Source='in...'*/0 << "'asm'"; 13107f422287a2ee7e515beb715f1f8915e9331469eeTed Kremenek return ExprError(); 13115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1312a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 131399831e4677a7e2e051af636221694d60ba31fcdbRichard Smith return ParseStringLiteralExpression(); 13145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseSimpleAsm 13175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 13185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [GNU] simple-asm-expr: 13195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 'asm' '(' asm-string-literal ')' 13205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 132160d7b3a319d84d688752be3870615ac0f111fb16John McCallParser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) { 1322000732226610650837478cba97843d19b75f648eChris Lattner assert(Tok.is(tok::kw_asm) && "Not an asm!"); 1323dfab6cb59a703f2ce4d58371652ce879f4c85a09Anders Carlsson SourceLocation Loc = ConsumeToken(); 1324a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 13257a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall if (Tok.is(tok::kw_volatile)) { 1326841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall // Remove from the end of 'asm' to the end of 'volatile'. 1327841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall SourceRange RemovalRange(PP.getLocForEndOfToken(Loc), 1328841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall PP.getLocForEndOfToken(Tok.getLocation())); 1329841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall 1330841d5e607f81bf5627e47d0c62ead29f28b5b0c2John McCall Diag(Tok, diag::warn_file_asm_volatile) 1331849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor << FixItHint::CreateRemoval(RemovalRange); 13327a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall ConsumeToken(); 13337a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall } 13347a6ae743b2ecfdfadadf7df53b569a9a3871a8fdJohn McCall 13354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor BalancedDelimiterTracker T(*this, tok::l_paren); 13364a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor if (T.consumeOpen()) { 13371ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner Diag(Tok, diag::err_expected_lparen_after) << "asm"; 133861364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl return ExprError(); 13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1340a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 134160d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult Result(ParseAsmStringLiteral()); 1342a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 1343ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl if (Result.isInvalid()) { 13448fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); 1345ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl if (EndLoc) 1346ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl *EndLoc = Tok.getLocation(); 1347ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl ConsumeAnyToken(); 1348ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl } else { 13494a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor // Close the paren and get the location of the end bracket 13504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor T.consumeClose(); 1351ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl if (EndLoc) 13524a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor *EndLoc = T.getCloseLocation(); 1353ab197baec16bacade82325fb274cf6b992ac5d8aSebastian Redl } 1354a6f0177a864ac478eb9890319f2e8ea03695e91dMike Stump 13553fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer return Result; 13565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 135825a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// \brief Get the TemplateIdAnnotation from the token and put it in the 135925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// cleanup pool so that it gets destroyed when parsing the current top level 136025a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis/// declaration is finished. 136125a767651d14db87aa03dd5fe3e011d877dd4100Argyrios KyrtzidisTemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) { 136225a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis assert(tok.is(tok::annot_template_id) && "Expected template-id token"); 136325a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis TemplateIdAnnotation * 136425a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue()); 136525a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis return Id; 136625a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis} 136725a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis 13680576681bac125be07f77f66b02a3dba2c3a24557Richard Smithvoid Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) { 13690576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // Push the current token back into the token stream (or revert it if it is 13700576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // cached) and use an annotation scope token for current token. 13710576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (PP.isBacktrackEnabled()) 13720576681bac125be07f77f66b02a3dba2c3a24557Richard Smith PP.RevertCachedTokens(1); 13730576681bac125be07f77f66b02a3dba2c3a24557Richard Smith else 13740576681bac125be07f77f66b02a3dba2c3a24557Richard Smith PP.EnterToken(Tok); 13750576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setKind(tok::annot_cxxscope); 13760576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS)); 13770576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setAnnotationRange(SS.getRange()); 13780576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 13790576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // In case the tokens were cached, have Preprocessor replace them 13800576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // with the annotation token. We don't need to do this if we've 13810576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // just reverted back to a prior state. 13820576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (IsNewAnnotation) 13830576681bac125be07f77f66b02a3dba2c3a24557Richard Smith PP.AnnotateCachedTokens(Tok); 13840576681bac125be07f77f66b02a3dba2c3a24557Richard Smith} 13850576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 13860576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \brief Attempt to classify the name at the current token position. This may 13870576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// form a type, scope or primary expression annotation, or replace the token 13880576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// with a typo-corrected keyword. This is only appropriate when the current 13890576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// name must refer to an entity which has already been declared. 13900576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// 13910576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \param IsAddressOfOperand Must be \c true if the name is preceded by an '&' 13920576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// and might possibly have a dependent nested name specifier. 13930576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \param CCC Indicates how to perform typo-correction for this name. If NULL, 13940576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// no typo correction will be performed. 13950576681bac125be07f77f66b02a3dba2c3a24557Richard SmithParser::AnnotatedNameKind 13960576681bac125be07f77f66b02a3dba2c3a24557Richard SmithParser::TryAnnotateName(bool IsAddressOfOperand, 13970576681bac125be07f77f66b02a3dba2c3a24557Richard Smith CorrectionCandidateCallback *CCC) { 13980576681bac125be07f77f66b02a3dba2c3a24557Richard Smith assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope)); 13990576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14000576681bac125be07f77f66b02a3dba2c3a24557Richard Smith const bool EnteringContext = false; 14010576681bac125be07f77f66b02a3dba2c3a24557Richard Smith const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); 14020576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14030576681bac125be07f77f66b02a3dba2c3a24557Richard Smith CXXScopeSpec SS; 14040576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (getLangOpts().CPlusPlus && 14050576681bac125be07f77f66b02a3dba2c3a24557Richard Smith ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) 14060576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Error; 14070576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14080576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (Tok.isNot(tok::identifier) || SS.isInvalid()) { 14090576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS, 14100576681bac125be07f77f66b02a3dba2c3a24557Richard Smith !WasScopeAnnotation)) 14110576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Error; 14120576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Unresolved; 14130576681bac125be07f77f66b02a3dba2c3a24557Richard Smith } 14140576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14150576681bac125be07f77f66b02a3dba2c3a24557Richard Smith IdentifierInfo *Name = Tok.getIdentifierInfo(); 14160576681bac125be07f77f66b02a3dba2c3a24557Richard Smith SourceLocation NameLoc = Tok.getLocation(); 14170576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14180576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // FIXME: Move the tentative declaration logic into ClassifyName so we can 14190576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // typo-correct to tentatively-declared identifiers. 14200576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (isTentativelyDeclared(Name)) { 14210576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // Identifier has been tentatively declared, and thus cannot be resolved as 14220576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // an expression. Fall back to annotating it as a type. 14230576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS, 14240576681bac125be07f77f66b02a3dba2c3a24557Richard Smith !WasScopeAnnotation)) 14250576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Error; 14260576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl; 14270576681bac125be07f77f66b02a3dba2c3a24557Richard Smith } 14280576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14290576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Token Next = NextToken(); 14300576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14310576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // Look up and classify the identifier. We don't perform any typo-correction 14320576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // after a scope specifier, because in general we can't recover from typos 14330576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // there (eg, after correcting 'A::tempalte B<X>::C', we would need to jump 14340576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // back into scope specifier parsing). 14350576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Sema::NameClassification Classification 14360576681bac125be07f77f66b02a3dba2c3a24557Richard Smith = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next, 14370576681bac125be07f77f66b02a3dba2c3a24557Richard Smith IsAddressOfOperand, SS.isEmpty() ? CCC : 0); 14380576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14390576681bac125be07f77f66b02a3dba2c3a24557Richard Smith switch (Classification.getKind()) { 14400576681bac125be07f77f66b02a3dba2c3a24557Richard Smith case Sema::NC_Error: 14410576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Error; 14420576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14430576681bac125be07f77f66b02a3dba2c3a24557Richard Smith case Sema::NC_Keyword: 14440576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // The identifier was typo-corrected to a keyword. 14450576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setIdentifierInfo(Name); 14460576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setKind(Name->getTokenID()); 14470576681bac125be07f77f66b02a3dba2c3a24557Richard Smith PP.TypoCorrectToken(Tok); 14480576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (SS.isNotEmpty()) 14490576681bac125be07f77f66b02a3dba2c3a24557Richard Smith AnnotateScopeToken(SS, !WasScopeAnnotation); 14500576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // We've "annotated" this as a keyword. 14510576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Success; 14520576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14530576681bac125be07f77f66b02a3dba2c3a24557Richard Smith case Sema::NC_Unknown: 14540576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // It's not something we know about. Leave it unannotated. 14550576681bac125be07f77f66b02a3dba2c3a24557Richard Smith break; 14560576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14570576681bac125be07f77f66b02a3dba2c3a24557Richard Smith case Sema::NC_Type: 14580576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setKind(tok::annot_typename); 14590576681bac125be07f77f66b02a3dba2c3a24557Richard Smith setTypeAnnotation(Tok, Classification.getType()); 14600576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setAnnotationEndLoc(NameLoc); 14610576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (SS.isNotEmpty()) 14620576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setLocation(SS.getBeginLoc()); 14630576681bac125be07f77f66b02a3dba2c3a24557Richard Smith PP.AnnotateCachedTokens(Tok); 14640576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Success; 14650576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14660576681bac125be07f77f66b02a3dba2c3a24557Richard Smith case Sema::NC_Expression: 14670576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setKind(tok::annot_primary_expr); 14680576681bac125be07f77f66b02a3dba2c3a24557Richard Smith setExprAnnotation(Tok, Classification.getExpression()); 14690576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setAnnotationEndLoc(NameLoc); 14700576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (SS.isNotEmpty()) 14710576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Tok.setLocation(SS.getBeginLoc()); 14720576681bac125be07f77f66b02a3dba2c3a24557Richard Smith PP.AnnotateCachedTokens(Tok); 14730576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Success; 14740576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14750576681bac125be07f77f66b02a3dba2c3a24557Richard Smith case Sema::NC_TypeTemplate: 14760576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (Next.isNot(tok::less)) { 14770576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // This may be a type template being used as a template template argument. 14780576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (SS.isNotEmpty()) 14790576681bac125be07f77f66b02a3dba2c3a24557Richard Smith AnnotateScopeToken(SS, !WasScopeAnnotation); 14800576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_TemplateName; 14810576681bac125be07f77f66b02a3dba2c3a24557Richard Smith } 14820576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // Fall through. 1483ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo case Sema::NC_VarTemplate: 14840576681bac125be07f77f66b02a3dba2c3a24557Richard Smith case Sema::NC_FunctionTemplate: { 1485ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo // We have a type, variable or function template followed by '<'. 14860576681bac125be07f77f66b02a3dba2c3a24557Richard Smith ConsumeToken(); 14870576681bac125be07f77f66b02a3dba2c3a24557Richard Smith UnqualifiedId Id; 14880576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Id.setIdentifier(Name, NameLoc); 14890576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (AnnotateTemplateIdToken( 14900576681bac125be07f77f66b02a3dba2c3a24557Richard Smith TemplateTy::make(Classification.getTemplateName()), 14910576681bac125be07f77f66b02a3dba2c3a24557Richard Smith Classification.getTemplateNameKind(), SS, SourceLocation(), Id)) 14920576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Error; 14930576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Success; 14940576681bac125be07f77f66b02a3dba2c3a24557Richard Smith } 14950576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 14960576681bac125be07f77f66b02a3dba2c3a24557Richard Smith case Sema::NC_NestedNameSpecifier: 14970576681bac125be07f77f66b02a3dba2c3a24557Richard Smith llvm_unreachable("already parsed nested name specifier"); 14980576681bac125be07f77f66b02a3dba2c3a24557Richard Smith } 14990576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 15000576681bac125be07f77f66b02a3dba2c3a24557Richard Smith // Unable to classify the name, but maybe we can annotate a scope specifier. 15010576681bac125be07f77f66b02a3dba2c3a24557Richard Smith if (SS.isNotEmpty()) 15020576681bac125be07f77f66b02a3dba2c3a24557Richard Smith AnnotateScopeToken(SS, !WasScopeAnnotation); 15030576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return ANK_Unresolved; 15040576681bac125be07f77f66b02a3dba2c3a24557Richard Smith} 15050576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 1506eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateTypeOrScopeToken - If the current token position is on a 1507eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typename (possibly qualified in C++) or a C++ scope specifier not followed 1508eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens 1509eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// with a single annotation token representing the typename or C++ scope 1510eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// respectively. 1511eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// This simplifies handling of C++ scope specifiers and allows efficient 1512eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// backtracking without the need to re-parse and resolve nested-names and 1513eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// typenames. 151444802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// It will mainly be called when we expect to treat identifiers as typenames 151544802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// (if they are typenames). For example, in C we do not expect identifiers 151644802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// inside expressions to be treated as typenames so it will not be called 151744802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// for expressions in C. 151844802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// The benefit for C/ObjC is that a typename will be annotated and 1519b43a50ff1b0b171ece84425b0ad83a9a31f038faSteve Naroff/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName 152044802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// will not be called twice, once to check whether we have a declaration 152144802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// specifier, and another one to get the actual type inside 152244802cc435d5122701e4f1a9354381cff4b171c0Argyrios Kyrtzidis/// ParseDeclarationSpecifiers). 1523a7bc7c880f86bc180684ef032d06df51bcae7a23Chris Lattner/// 15249ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall/// This returns true if an error occurred. 15251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 152655a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete 152755a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid. 1528fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrainbool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) { 15291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) 153042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope) 153123756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith || Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) 153223756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith && "Cannot be a type or scope token!"); 15331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1534d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor if (Tok.is(tok::kw_typename)) { 153567ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer // MSVC lets you do stuff like: 153667ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer // typename typedef T_::D D; 153767ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer // 153867ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer // We will consume the typedef token here and put it back after we have 153967ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer // parsed the first identifier, transforming it into something more like: 154067ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer // typename T_::D typedef D; 154167ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer if (getLangOpts().MicrosoftMode && NextToken().is(tok::kw_typedef)) { 154267ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer Token TypedefToken; 154367ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer PP.Lex(TypedefToken); 154467ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer bool Result = TryAnnotateTypeOrScopeToken(EnteringContext, NeedType); 154567ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer PP.EnterToken(Tok); 154667ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer Tok = TypedefToken; 154767ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer if (!Result) 154867ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename); 154967ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer return Result; 155067ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer } 155167ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer 1552d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // Parse a C++ typename-specifier, e.g., "typename T::type". 1553d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // 1554d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // typename-specifier: 1555d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // 'typename' '::' [opt] nested-name-specifier identifier 15561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // 'typename' '::' [opt] nested-name-specifier template [opt] 15571734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor // simple-template-id 1558d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor SourceLocation TypenameLoc = ConsumeToken(); 1559d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor CXXScopeSpec SS; 1560efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(), 1561efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor /*EnteringContext=*/false, 15624147d307086cf024a40a080e2bf379e9725f6f41Francois Pichet 0, /*IsTypename*/true)) 15639ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return true; 15649ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall if (!SS.isSet()) { 1565b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) || 1566b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet Tok.is(tok::annot_decltype)) { 156723756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith // Attempt to recover by skipping the invalid 'typename' 1568b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet if (Tok.is(tok::annot_decltype) || 1569b67e7fc607671ef3df64de63c38545197e9992b2Francois Pichet (!TryAnnotateTypeOrScopeToken(EnteringContext, NeedType) && 157067ac9989e3ec53174b524ab8bc7d08185346caceDavid Majnemer Tok.isAnnotation())) { 157123756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith unsigned DiagID = diag::err_expected_qualified_after_typename; 157223756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith // MS compatibility: MSVC permits using known types with typename. 157323756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith // e.g. "typedef typename T* pointer_type" 157423756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith if (getLangOpts().MicrosoftExt) 157523756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith DiagID = diag::warn_expected_qualified_after_typename; 157623756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith Diag(Tok.getLocation(), DiagID); 157723756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith return false; 157823756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith } 157923756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith } 158023756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith 158123756776eadfd8bbddf5d120d4c191ef9e50d209Richard Smith Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename); 15829ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return true; 1583d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor } 1584d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 1585d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor TypeResult Ty; 1586d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor if (Tok.is(tok::identifier)) { 1587d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // FIXME: check whether the next token is '<', first! 158823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS, 15891a15dae8be2b28e02b6639aa92b832465c5be420Douglas Gregor *Tok.getIdentifierInfo(), 1590d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor Tok.getLocation()); 15911734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor } else if (Tok.is(tok::annot_template_id)) { 159225a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 15931734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor if (TemplateId->Kind == TNK_Function_template) { 15941734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor Diag(Tok, diag::err_typename_refers_to_non_type_template) 15951734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor << Tok.getAnnotationRange(); 15969ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return true; 15971734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor } 1598d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 15995354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), 1600a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor TemplateId->NumArgs); 160166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara 1602a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS, 160366581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara TemplateId->TemplateKWLoc, 1604a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor TemplateId->Template, 1605a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor TemplateId->TemplateNameLoc, 1606a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor TemplateId->LAngleLoc, 160766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara TemplateArgsPtr, 1608a02411e4d58b1730bea2a990822858ecc31e8eb1Douglas Gregor TemplateId->RAngleLoc); 16091734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor } else { 16101734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor Diag(Tok, diag::err_expected_type_name_after_typename) 16111734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor << SS.getRange(); 16129ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return true; 16131734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor } 16141734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 161539d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl SourceLocation EndLoc = Tok.getLastLoc(); 16161734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor Tok.setKind(tok::annot_typename); 1617b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get()); 161839d67117f896c6e2faa727671ef64b3c04b0e3feSebastian Redl Tok.setAnnotationEndLoc(EndLoc); 16191734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor Tok.setLocation(TypenameLoc); 16201734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor PP.AnnotateCachedTokens(Tok); 16219ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 1622d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor } 1623d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 1624ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall // Remembers whether the token was originally a scope annotation. 16250576681bac125be07f77f66b02a3dba2c3a24557Richard Smith bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); 1626ae03cb5a84d13c7a0d4b21865bd63aabd18120d2John McCall 1627eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis CXXScopeSpec SS; 16284e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) 1629b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) 16309ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return true; 1631eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 16320576681bac125be07f77f66b02a3dba2c3a24557Richard Smith return TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, NeedType, 16330576681bac125be07f77f66b02a3dba2c3a24557Richard Smith SS, !WasScopeAnnotation); 16340576681bac125be07f77f66b02a3dba2c3a24557Richard Smith} 16350576681bac125be07f77f66b02a3dba2c3a24557Richard Smith 16360576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// \brief Try to annotate a type or scope token, having already parsed an 16370576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// optional scope specifier. \p IsNewScope should be \c true unless the scope 16380576681bac125be07f77f66b02a3dba2c3a24557Richard Smith/// specifier was extracted from an existing tok::annot_cxxscope annotation. 16390576681bac125be07f77f66b02a3dba2c3a24557Richard Smithbool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(bool EnteringContext, 16400576681bac125be07f77f66b02a3dba2c3a24557Richard Smith bool NeedType, 16410576681bac125be07f77f66b02a3dba2c3a24557Richard Smith CXXScopeSpec &SS, 16420576681bac125be07f77f66b02a3dba2c3a24557Richard Smith bool IsNewScope) { 1643eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis if (Tok.is(tok::identifier)) { 1644fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain IdentifierInfo *CorrectedII = 0; 1645608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner // Determine whether the identifier is a type name. 1646b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(), 1647b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall Tok.getLocation(), getCurScope(), 16481e52dfc648ce0b25ef57ae29ef1b4337d80011efFariborz Jahanian &SS, false, 16499e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor NextToken().is(tok::period), 16509e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor ParsedType(), 1651fad03b75e0297546c5d12ec420b5b79d5b7baa2aAbramo Bagnara /*IsCtorOrDtorName=*/false, 1652fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain /*NonTrivialTypeSourceInfo*/true, 1653fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain NeedType ? &CorrectedII : NULL)) { 1654fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain // A FixIt was applied as a result of typo correction 1655fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain if (CorrectedII) 1656fac9467d1676dc05761e12e41e13e01a3a3da52bKaelyn Uhrain Tok.setIdentifierInfo(CorrectedII); 1657608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner // This is a typename. Replace the current token in-place with an 1658608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner // annotation type token. 1659b31757b68afe06ba442a05775d08fe7aa0f6f889Chris Lattner Tok.setKind(tok::annot_typename); 1660b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall setTypeAnnotation(Tok, Ty); 1661608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner Tok.setAnnotationEndLoc(Tok.getLocation()); 1662608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner if (SS.isNotEmpty()) // it was a C++ qualified type name. 1663608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner Tok.setLocation(SS.getBeginLoc()); 16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1665608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner // In case the tokens were cached, have Preprocessor replace 1666608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner // them with the annotation token. 1667608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner PP.AnnotateCachedTokens(Tok); 16689ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 167039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor 16714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().CPlusPlus) { 1672608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner // If we're in C, we can't have :: tokens at all (the lexer won't return 1673608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner // them). If the identifier is not a type, then it can't be scope either, 16741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // just early exit. 1675608d1fc9c4db3e3769f03a4f989d7692aefbf073Chris Lattner return false; 1676eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis } 16771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 167839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // If this is a template-id, annotate with a template-id or type token. 167955f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor if (NextToken().is(tok::less)) { 16807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor TemplateTy Template; 1681014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor UnqualifiedId TemplateName; 1682014e88d94ff83e3aad4e33b16413a2d1817ec208Douglas Gregor TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 16831fd6d44d7ca97631497551bbf98866263143d706Douglas Gregor bool MemberOfUnknownSpecialization; 16841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (TemplateNameKind TNK 16857c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara = Actions.isTemplateName(getCurScope(), SS, 16867c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara /*hasTemplateKeyword=*/false, TemplateName, 1687b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall /*ObjectType=*/ ParsedType(), 1688b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall EnteringContext, 16897c15353ccaed24f2df932571166bf305c1b98b6dAbramo Bagnara Template, MemberOfUnknownSpecialization)) { 1690ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor // Consume the identifier. 1691ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor ConsumeToken(); 1692e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), 1693e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara TemplateName)) { 1694c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner // If an unrecoverable error occurred, we need to return true here, 1695c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner // because the token stream is in a damaged state. We may not return 1696c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner // a valid identifier. 16979ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return true; 1698c8e27cc402043ec86c1698c09e4ee9e415b16207Chris Lattner } 1699ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor } 170055f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor } 1701d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor 170239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // The current token, which is either an identifier or a 170339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // template-id, is not part of the annotation. Fall through to 170439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // push that token back into the stream and complete the C++ scope 170539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // specifier annotation. 17061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 1707eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 170839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor if (Tok.is(tok::annot_template_id)) { 170925a767651d14db87aa03dd5fe3e011d877dd4100Argyrios Kyrtzidis TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1710c45c232440dfafedca1a3773b904fb42609b1b19Douglas Gregor if (TemplateId->Kind == TNK_Type_template) { 171139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // A template-id that refers to a type was parsed into a 171239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // template-id annotation in a context where we weren't allowed 171339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // to produce a type annotation token. Update the template-id 171439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor // annotation token to a type annotation token now. 1715059101f922de6eb765601459925f4c8914420b23Douglas Gregor AnnotateTemplateIdTokenAsType(); 17169ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 1717ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo } else if (TemplateId->Kind == TNK_Var_template) 1718ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo return false; 171939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor } 1720d6fb7ef028d9aa0b3e8943b7bc049c524437b407Douglas Gregor 17216ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner if (SS.isEmpty()) 17229ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 17231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17246ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner // A C++ scope specifier that isn't followed by a typename. 17250576681bac125be07f77f66b02a3dba2c3a24557Richard Smith AnnotateScopeToken(SS, IsNewScope); 17269ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 1727eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis} 1728eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 1729eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only 173039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor/// annotates C++ scope specifiers and template-ids. This returns 173183a22ecbf52c06b4ee364f3fadcdb0abaf2dabf6Richard Smith/// true if there was an error that could not be recovered from. 17321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 173355a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// Note that this routine emits an error if you call it with ::new or ::delete 173455a7cefc846765ac7d142a63f773747a20518d71Chris Lattner/// as the current tokens, so only call it in contexts where these are invalid. 1735495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregorbool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) { 17364e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().CPlusPlus && 17376ec76d45bd3111013c357f16e08720407c2f9ae8Chris Lattner "Call sites of this function should be guarded by checking for C++"); 17383b887354b1b667c97d070ddc67b5354353c4c07bDouglas Gregor assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || 173942d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) || 174042d6d0c91ab089cb252ab2f91c16d4557f458a2cDavid Blaikie Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!"); 1741eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 17424bdd91c09fd59e0c154d759288beff300e31e1d0Argyrios Kyrtzidis CXXScopeSpec SS; 1743b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) 17449ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return true; 1745edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin if (SS.isEmpty()) 17469ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 1747eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis 17480576681bac125be07f77f66b02a3dba2c3a24557Richard Smith AnnotateScopeToken(SS, true); 17499ba6166f4a78722e7df8ffbd64eb788bfdf2764aJohn McCall return false; 1750eb83ecde1a822b1c38cd060a85a08c1ac9f82cf8Argyrios Kyrtzidis} 17516c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall 1752fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieubool Parser::isTokenEqualOrEqualTypo() { 1753fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu tok::TokenKind Kind = Tok.getKind(); 1754fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu switch (Kind) { 1755fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu default: 1756d6c7c67313634b317a0d63c32be0511a121bb33dRichard Trieu return false; 1757fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::ampequal: // &= 1758fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::starequal: // *= 1759fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::plusequal: // += 1760fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::minusequal: // -= 1761fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::exclaimequal: // != 1762fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::slashequal: // /= 1763fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::percentequal: // %= 1764fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::lessequal: // <= 1765fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::lesslessequal: // <<= 1766fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::greaterequal: // >= 1767fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::greatergreaterequal: // >>= 1768fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::caretequal: // ^= 1769fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::pipeequal: // |= 1770fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::equalequal: // == 1771fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal) 1772fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu << getTokenSimpleSpelling(Kind) 1773fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "="); 1774fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu case tok::equal: 1775fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu return true; 1776fcaf27e185695bdf755e202aeba9632e0a8ef3c6Richard Trieu } 1777a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis} 1778a6eb5f81d13bacac01faff70a947047725b4413fArgyrios Kyrtzidis 17797d100872341f233c81e1d7b72b40457e62c36862Argyrios KyrtzidisSourceLocation Parser::handleUnexpectedCodeCompletionToken() { 17807d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis assert(Tok.is(tok::code_completion)); 17817d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PrevTokLocation = Tok.getLocation(); 17827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 178323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor for (Scope *S = getCurScope(); S; S = S->getParent()) { 1784dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor if (S->getFlags() & Scope::FnScope) { 1785f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction); 17867d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffParsing(); 17877d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return PrevTokLocation; 1788dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor } 1789dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor 1790dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor if (S->getFlags() & Scope::ClassScope) { 1791f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class); 17927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffParsing(); 17937d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return PrevTokLocation; 1794dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor } 1795dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor } 1796dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor 1797f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace); 17987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffParsing(); 17997d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return PrevTokLocation; 1800dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor} 1801dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor 18026c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// Anchor the Parser::FieldCallback vtable to this translation unit. 18036c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// We use a spurious method instead of the destructor because 18046c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// destroying FieldCallbacks can actually be slightly 18056c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall// performance-sensitive. 18066c94a6d77f456f23ecd4c2061e6413786b5e6571John McCallvoid Parser::FieldCallback::_anchor() { 18076c94a6d77f456f23ecd4c2061e6413786b5e6571John McCall} 1808f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor 1809f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor// Code-completion pass-through functions 1810f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor 1811f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteDirective(bool InConditional) { 1812f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor Actions.CodeCompletePreprocessorDirective(InConditional); 1813f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor} 1814f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor 1815f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorvoid Parser::CodeCompleteInConditionalExclusion() { 1816f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope()); 1817f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor} 18181fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor 18191fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregorvoid Parser::CodeCompleteMacroName(bool IsDefinition) { 1820f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor Actions.CodeCompletePreprocessorMacroName(IsDefinition); 1821f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor} 1822f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor 1823f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompletePreprocessorExpression() { 1824f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor Actions.CodeCompletePreprocessorExpression(); 1825f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor} 1826f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor 1827f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregorvoid Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro, 1828f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor MacroInfo *MacroInfo, 1829f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor unsigned ArgumentIndex) { 1830f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo, 1831f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor ArgumentIndex); 18321fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor} 183355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor 183455817afdf9d453a443262a733f6caf6692dca118Douglas Gregorvoid Parser::CodeCompleteNaturalLanguage() { 183555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor Actions.CodeCompleteNaturalLanguage(); 183655817afdf9d453a443262a733f6caf6692dca118Douglas Gregor} 1837f986038beed360c031de8654cfba43a5d3184605Francois Pichet 18383896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregorbool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) { 1839f986038beed360c031de8654cfba43a5d3184605Francois Pichet assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) && 1840f986038beed360c031de8654cfba43a5d3184605Francois Pichet "Expected '__if_exists' or '__if_not_exists'"); 18413896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor Result.IsIfExists = Tok.is(tok::kw___if_exists); 18423896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor Result.KeywordLoc = ConsumeToken(); 1843f986038beed360c031de8654cfba43a5d3184605Francois Pichet 18444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor BalancedDelimiterTracker T(*this, tok::l_paren); 18454a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor if (T.consumeOpen()) { 18463896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor Diag(Tok, diag::err_expected_lparen_after) 18473896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor << (Result.IsIfExists? "__if_exists" : "__if_not_exists"); 1848f986038beed360c031de8654cfba43a5d3184605Francois Pichet return true; 1849f986038beed360c031de8654cfba43a5d3184605Francois Pichet } 1850f986038beed360c031de8654cfba43a5d3184605Francois Pichet 1851f986038beed360c031de8654cfba43a5d3184605Francois Pichet // Parse nested-name-specifier. 1852efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(), 1853efaa93aaa2653f4eb40e6a22e504a448da94aaf8Douglas Gregor /*EnteringContext=*/false); 1854f986038beed360c031de8654cfba43a5d3184605Francois Pichet 1855f986038beed360c031de8654cfba43a5d3184605Francois Pichet // Check nested-name specifier. 18563896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor if (Result.SS.isInvalid()) { 18573896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor T.skipToEnd(); 1858f986038beed360c031de8654cfba43a5d3184605Francois Pichet return true; 1859f986038beed360c031de8654cfba43a5d3184605Francois Pichet } 1860f986038beed360c031de8654cfba43a5d3184605Francois Pichet 1861e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara // Parse the unqualified-id. 1862e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara SourceLocation TemplateKWLoc; // FIXME: parsed, but unused. 1863e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(), 1864e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara TemplateKWLoc, Result.Name)) { 18653896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor T.skipToEnd(); 1866f986038beed360c031de8654cfba43a5d3184605Francois Pichet return true; 1867f986038beed360c031de8654cfba43a5d3184605Francois Pichet } 1868f986038beed360c031de8654cfba43a5d3184605Francois Pichet 18693896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor if (T.consumeClose()) 1870f986038beed360c031de8654cfba43a5d3184605Francois Pichet return true; 18713896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor 1872f986038beed360c031de8654cfba43a5d3184605Francois Pichet // Check if the symbol exists. 187365019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc, 187465019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor Result.IsIfExists, Result.SS, 18753896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor Result.Name)) { 18763896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor case Sema::IER_Exists: 18773896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip; 18783896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor break; 1879f986038beed360c031de8654cfba43a5d3184605Francois Pichet 18803896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor case Sema::IER_DoesNotExist: 18813896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip; 18823896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor break; 18833896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor 18843896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor case Sema::IER_Dependent: 18853896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor Result.Behavior = IEB_Dependent; 18863896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor break; 188765019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor 188865019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor case Sema::IER_Error: 188965019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor return true; 18903896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor } 1891f986038beed360c031de8654cfba43a5d3184605Francois Pichet 1892f986038beed360c031de8654cfba43a5d3184605Francois Pichet return false; 1893f986038beed360c031de8654cfba43a5d3184605Francois Pichet} 1894f986038beed360c031de8654cfba43a5d3184605Francois Pichet 1895563a645de82231a55e221fe655b7188bf8369662Francois Pichetvoid Parser::ParseMicrosoftIfExistsExternalDeclaration() { 18963896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor IfExistsCondition Result; 1897f986038beed360c031de8654cfba43a5d3184605Francois Pichet if (ParseMicrosoftIfExistsCondition(Result)) 1898f986038beed360c031de8654cfba43a5d3184605Francois Pichet return; 1899f986038beed360c031de8654cfba43a5d3184605Francois Pichet 19003896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor BalancedDelimiterTracker Braces(*this, tok::l_brace); 19013896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor if (Braces.consumeOpen()) { 1902f986038beed360c031de8654cfba43a5d3184605Francois Pichet Diag(Tok, diag::err_expected_lbrace); 1903f986038beed360c031de8654cfba43a5d3184605Francois Pichet return; 1904f986038beed360c031de8654cfba43a5d3184605Francois Pichet } 1905f986038beed360c031de8654cfba43a5d3184605Francois Pichet 19063896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor switch (Result.Behavior) { 19073896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor case IEB_Parse: 19083896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor // Parse declarations below. 19093896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor break; 19103896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor 19113896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor case IEB_Dependent: 19123896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor llvm_unreachable("Cannot have a dependent external declaration"); 19133896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor 19143896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor case IEB_Skip: 19153896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor Braces.skipToEnd(); 1916f986038beed360c031de8654cfba43a5d3184605Francois Pichet return; 1917f986038beed360c031de8654cfba43a5d3184605Francois Pichet } 1918f986038beed360c031de8654cfba43a5d3184605Francois Pichet 19193896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor // Parse the declarations. 19203896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 1921f986038beed360c031de8654cfba43a5d3184605Francois Pichet ParsedAttributesWithRange attrs(AttrFactory); 19224e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith MaybeParseCXX11Attributes(attrs); 1923f986038beed360c031de8654cfba43a5d3184605Francois Pichet MaybeParseMicrosoftAttributes(attrs); 1924f986038beed360c031de8654cfba43a5d3184605Francois Pichet DeclGroupPtrTy Result = ParseExternalDeclaration(attrs); 1925f986038beed360c031de8654cfba43a5d3184605Francois Pichet if (Result && !getCurScope()->getParent()) 1926f986038beed360c031de8654cfba43a5d3184605Francois Pichet Actions.getASTConsumer().HandleTopLevelDecl(Result.get()); 19273896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor } 19283896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor Braces.consumeClose(); 1929f986038beed360c031de8654cfba43a5d3184605Francois Pichet} 19306aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 19315948ae1021122164b22f74353bb7fe325a64f616Douglas GregorParser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) { 19321b257afbae854c6817f26b7d61c4fed8ff7aebadDouglas Gregor assert(Tok.isObjCAtKeyword(tok::objc_import) && 193365030af6526748ce11534e92f0ccefc44091ba13Douglas Gregor "Improper start to module import"); 19346aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor SourceLocation ImportLoc = ConsumeToken(); 19356aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 1936cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; 19373d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor 19383d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor // Parse the module path. 19393d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor do { 19403d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor if (!Tok.is(tok::identifier)) { 1941c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor if (Tok.is(tok::code_completion)) { 1942c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor Actions.CodeCompleteModuleImport(ImportLoc, Path); 1943c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor ConsumeCodeCompletionToken(); 1944c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor SkipUntil(tok::semi); 1945c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor return DeclGroupPtrTy(); 1946c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor } 1947c5b2e58840748145d1706c1d1481369d1863fabfDouglas Gregor 19483d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor Diag(Tok, diag::err_module_expected_ident); 19493d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor SkipUntil(tok::semi); 19503d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor return DeclGroupPtrTy(); 19513d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor } 19523d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor 19533d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor // Record this part of the module path. 19543d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation())); 19553d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor ConsumeToken(); 19563d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor 19573d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor if (Tok.is(tok::period)) { 19583d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor ConsumeToken(); 19593d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor continue; 19603d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor } 19613d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor 19623d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor break; 19633d3589db579f7695667b913c5043dd264ebe546fDouglas Gregor } while (true); 19643b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis 19653b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis if (PP.hadModuleLoaderFatalFailure()) { 19663b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis // With a fatal failure in the module loader, we abort parsing. 19673b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis cutOffParsing(); 19683b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis return DeclGroupPtrTy(); 19693b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis } 19703b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis 19715948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path); 19726aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor ExpectAndConsumeSemi(diag::err_module_expected_semi); 19736aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor if (Import.isInvalid()) 19746aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor return DeclGroupPtrTy(); 19756aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 19766aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor return Actions.ConvertDeclToDeclGroup(Import.get()); 19776aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor} 19784a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor 1979c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::diagnoseOverflow() { 19809e738cc9d4b4655c44dadeb22f3a314daf43b995Richard Smith P.Diag(P.Tok, diag::err_bracket_depth_exceeded) 19819e738cc9d4b4655c44dadeb22f3a314daf43b995Richard Smith << P.getLangOpts().BracketDepth; 19829e738cc9d4b4655c44dadeb22f3a314daf43b995Richard Smith P.Diag(P.Tok, diag::note_bracket_depth); 19838fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev P.SkipUntil(tok::eof); 1984d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor return true; 19854a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor} 19864a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor 1987c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID, 19884a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor const char *Msg, 19894a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor tok::TokenKind SkipToToc ) { 19904a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor LOpen = P.Tok.getLocation(); 1991d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc)) 1992d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor return true; 1993d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor 1994d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor if (getDepth() < MaxDepth) 1995d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor return false; 1996d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor 1997d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor return diagnoseOverflow(); 19984a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor} 19994a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor 2000c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorbool BalancedDelimiterTracker::diagnoseMissingClose() { 2001d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor assert(!P.Tok.is(Close) && "Should have consumed closing delimiter"); 2002d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor 2003d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor const char *LHSName = "unknown"; 2004b031eab1c07fa2c5bd74c7e92f7c938bf3304729David Blaikie diag::kind DID; 2005d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor switch (Close) { 2006b031eab1c07fa2c5bd74c7e92f7c938bf3304729David Blaikie default: llvm_unreachable("Unexpected balanced token"); 2007d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break; 2008d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break; 2009d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break; 20104a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor } 2011d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor P.Diag(P.Tok, DID); 2012d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor P.Diag(LOpen, diag::note_matching) << LHSName; 20135eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith 20145eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith // If we're not already at some kind of closing bracket, skip to our closing 20155eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith // token. 20165eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) && 20175eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith P.Tok.isNot(tok::r_square) && 20188fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev P.SkipUntil(Close, FinalToken, 20198fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev Parser::StopAtSemi | Parser::StopBeforeMatch) && 20205eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith P.Tok.is(Close)) 2021b578aee665aad5ed1a46a26217c730fdfbfc8c2eDavid Blaikie LClose = P.ConsumeAnyToken(); 20224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor return true; 20234a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor} 20243896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor 2025c86c40b912e53fb11ff8f745ed616035b8b7259cDouglas Gregorvoid BalancedDelimiterTracker::skipToEnd() { 20268fe2475a4b4c00475709c13d43eb9a57cce87cbcAlexey Bataev P.SkipUntil(Close, Parser::StopBeforeMatch); 20275eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith consumeClose(); 20283896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor} 2029